Friday 19 February 2016

Topper and Average

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

12 comments:

  1. how to set values i am unable to do anything

    ReplyDelete
  2. THE 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

    ReplyDelete
  3. HERE IS THE SAMPLE CODE FOR SET VALUE FUNCTION

    ReplyDelete
  4. void setValues(struct Student* s, const char * name, float marks[], int nSubjects)
    {
    int i;
    strcpy(s->name, name);
    for (i = 0; i < nSubjects; ++i)
    {
    s->marks[i]=marks[i];
    }

    }

    ReplyDelete
  5. anyone help me in printing the average values. i tried a lot. but not getting

    ReplyDelete
  6. float getAvg(struct Student s, int nSubjects)
    {
    float sum=0,avg;
    int i;
    for(i=0;i<nSubjects;i++)
    {
    sum=sum+s.marks[i];
    }
    avg=sum/nSubjects;
    return(avg);

    }

    ReplyDelete
  7. how to code the gettopper function?? help please

    ReplyDelete
  8. Help me rectify the following code for getTopper function:

    char * 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;
    }

    ReplyDelete
    Replies
    1. The code was this actually and its giving a runtime error:

      char * 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;
      }

      Delete
  9. char * 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;
    }


    plz correct this sort of code

    ReplyDelete
  10. I think No one is getting the right output for this problem.
    If anyone is , then he must point out the mistake in this code or post the correct code.

    ReplyDelete
  11. Anyone plz post the code ... do it plz ... last minutes left .

    ReplyDelete