Given a list of student names and their marks in K subjects, find the average mark for every student. Also, print a list of toppers in each subject. (Assume there is only one topper).
Use the given structure Student (in C language). You should the implement following functions which are called in main():
char * getTopper(struct Student s[], int nStudents, int subjectIndex, int nSubjects);
/*
Return name of topper of a subject.
Arguments :
array of Student records.
number of Students (nStudents).
index of subject for which function should find toper (subjectIndex).
number of Subjects (nSubjects)
*/
float getAvg(struct Student s, int nSubjects);
/*
return avg mark of a student.
Arguments :
a Student record s.
number of Subjects (nSubjects)
*/
void setValues(struct Student* s, const char * name, float marks[], int nSubjects);
/*
set values in structure Student
Arguments :
a pointer to a Student record (s).
name of Student (name).
mark of K subjects (marks).
number of Subjects (nSubjects)
*/
Input:
Given a list of student names and their marks in K subjects, find the average mark for every student. Also, print a list of toppers in each subject. (Assume there is only one topper).
The first line contains the number of students (N) and the number of subjects (K). Following N lines contains name of student and the marks of K subjects.
Output
The first N lines of output should contain average marks of each student (Print 6 decimal places) . Following K lines should contain the name of the topper of each subject in the order in which the subjects are given.
Constraints
1 <= N <=100
1 <= K <=10
Assume that Name does not contain spaces and the maximum length of names of the students is 10 (excluding the NULL character at the end).
0 <= Mark <=100 and the marks are real numbers.
Solution-:
After deadline is over although you can ask the logic
Use the given structure Student (in C language). You should the implement following functions which are called in main():
char * getTopper(struct Student s[], int nStudents, int subjectIndex, int nSubjects);
/*
Return name of topper of a subject.
Arguments :
array of Student records.
number of Students (nStudents).
index of subject for which function should find toper (subjectIndex).
number of Subjects (nSubjects)
*/
float getAvg(struct Student s, int nSubjects);
/*
return avg mark of a student.
Arguments :
a Student record s.
number of Subjects (nSubjects)
*/
void setValues(struct Student* s, const char * name, float marks[], int nSubjects);
/*
set values in structure Student
Arguments :
a pointer to a Student record (s).
name of Student (name).
mark of K subjects (marks).
number of Subjects (nSubjects)
*/
Input:
Given a list of student names and their marks in K subjects, find the average mark for every student. Also, print a list of toppers in each subject. (Assume there is only one topper).
The first line contains the number of students (N) and the number of subjects (K). Following N lines contains name of student and the marks of K subjects.
Output
The first N lines of output should contain average marks of each student (Print 6 decimal places) . Following K lines should contain the name of the topper of each subject in the order in which the subjects are given.
Constraints
1 <= N <=100
1 <= K <=10
Assume that Name does not contain spaces and the maximum length of names of the students is 10 (excluding the NULL character at the end).
0 <= Mark <=100 and the marks are real numbers.
Solution-:
After deadline is over although you can ask the logic
how to set values i am unable to do anything
ReplyDeleteTHE PURPOSE OF SET VALUE FUNCTION IS TO ASSIGNING THE ELEMENTS OF THE STRUCTURE.NAME AND MARKS ARE ENTERED AS SEPARATE VARIABLES AND THEY NEED TO BE ASSIGNED WITHIN THE STRUCTURE TO ITS ELEMENTS
ReplyDeleteHERE IS THE SAMPLE CODE FOR SET VALUE FUNCTION
ReplyDeletevoid setValues(struct Student* s, const char * name, float marks[], int nSubjects)
ReplyDelete{
int i;
strcpy(s->name, name);
for (i = 0; i < nSubjects; ++i)
{
s->marks[i]=marks[i];
}
}
anyone help me in printing the average values. i tried a lot. but not getting
ReplyDeletefloat getAvg(struct Student s, int nSubjects)
ReplyDelete{
float sum=0,avg;
int i;
for(i=0;i<nSubjects;i++)
{
sum=sum+s.marks[i];
}
avg=sum/nSubjects;
return(avg);
}
how to code the gettopper function?? help please
ReplyDeleteHelp me rectify the following code for getTopper function:
ReplyDeletechar * getTopper(struct Student s[], int nStudents, int subjectIndex, int nSubjects)
{
int i, j, max=0;
char *topper;
struct Student *p;
for(i=0;imarks[subjectIndex]>max)
{
max=p->marks[subjectIndex];
strcpy(topper,p->name);
}
}
}
return topper;
}
The code was this actually and its giving a runtime error:
Deletechar * getTopper(struct Student s[], int nStudents, int subjectIndex, int nSubjects)
{
int i, j, max=0;
char *topper;
struct Student *p;
for(i=0;imarks[subjectIndex]>max)
{
max=p->marks[subjectIndex];
strcpy(topper,p->name);
}
}
}
return topper;
}
char * getTopper(struct Student s[], int nStudents, int subjectIndex, int nSubjects)
ReplyDelete{
int i, j, max=0;
char *topper;
struct Student *p;
for(i=0;imarks[subjectIndex]>max)
{
max=p->marks[subjectIndex];
strcpy(topper,p->name);
}
}
}
return topper;
}
plz correct this sort of code
I think No one is getting the right output for this problem.
ReplyDeleteIf anyone is , then he must point out the mistake in this code or post the correct code.
Anyone plz post the code ... do it plz ... last minutes left .
ReplyDelete