Friday 19 February 2016

NPTEL(WEEK-5.1)

Programming Assignment 5.1: 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.




Program:

#include<stdio.h>
#include<string.h>
#define N 100
#define K 10

struct Student {
  char name[10];
  float marks[10];
};

char * getTopper(struct Student [], int nStudents, int subjectIndex, int nSubjects);
/* return name of topper of a subject*/
float getAvg(struct Student , int nSubjects);
/* return avg mark of student s*/
void setValues(struct Student* , const char * name, float marks[], int nSubjects);

/* set values in struct Student s */


Coding :

char * getTopper(struct Student s[], int nStudents, int subjectIndex, int nSubjects)
{
    int i=0,id=0;
    float max=s[0].marks[subjectIndex],temp;
    for(i=0;i<nStudents;i++)
    {
        if(s[i].marks[subjectIndex]>max)
        {
            id = i;
            max = s[i].marks[subjectIndex];
        }
    }
    return (s[id].name);
}

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

Program :

int main()
{
  struct Student students[N];
  int nStudents, nSubjects;
  int i, j;
  char name[10];
  float marks[K];
  /*Read Student data*/
  scanf("%d %d", &nStudents, &nSubjects);
  for (i = 0; i < nStudents; ++i) {
scanf("%s", name);
for (j = 0; j < nSubjects; ++j) {
  scanf("%f", &marks[j]);
}
setValues(&(students[i]), name, marks, nSubjects);
  }
  for (i = 0; i < nStudents; ++i) {
printf("%f\n", getAvg(students[i], nSubjects));
  }
  for (j = 0; j < nSubjects; ++j) {
if( j == nSubjects -1)
    printf("%s", getTopper(students, nStudents, j, nSubjects));
else
     printf("%s\n", getTopper(students, nStudents, j, nSubjects));
  }
  return 0;

}

7 comments:

  1. Nice brother , You are like angel In heaven

    ReplyDelete
  2. You had a good skill in programming

    ReplyDelete
  3. smit Kadvani Thank you so much...can you submit Programming Assignment 5.4: Number in a position please

    ReplyDelete
    Replies
    1. /* Function to load the numbers onto the linked list
      Return a pointer to the head of the list */
      Node loadNum()
      {
      Node head,temp;
      int t;
      head=(struct node *)malloc(sizeof(struct node));
      scanf("%d",&t);
      head->data=t;
      head->next=NULL;
      temp=head;
      scanf("%d",&t);
      do
      {
      temp=head;
      Node var=(struct node *)malloc(sizeof(struct node));
      var->data=t;
      while(temp->next!=NULL)
      {
      temp=temp->next;
      }
      var->next=NULL;
      temp->next=var;
      scanf("%d",&t);

      }while(t!=-1);
      return head;
      }

      /* Function to release the memory after every iteration */
      void releaseMem(Node head)
      {

      }

      Delete

How to install google-chrome in redhat without redhat subscription

Install google-chrome in redhat  Download the .rpm file of chrome https://www.google.com/chrome/thank-you.html?installdataindex=empty&st...