I am doing C programming after a long time and stuck into few basic things using struct and array. Though I've two requirements but the sample input and output as follows:
Inputs:1) Id Score
1002 100
1004 200
2) Id Score
1002 100
1004 100
Outputs:
1) Highest Scorer: 1004 200
2) Highest Scorer: 1002 100
Highest Scorer: 1004 100 //As both have the same score
The two requirements are - 1) Two loops will be there, not nested 2) The first loop should store the highest score, then the second loop should check which matches the highest score. Pretty simple, so I did the following:
Code:
#include <stdio.h>
#include <stdlib.h>
struct Student
{
int id;
float score;
} student1[2], dest[100];
int
main ()
{
int i, j, n, highest = 0;
n = sizeof (student1) / sizeof (*student1);
for (i = 0; i < n; ++i)
{
scanf ("%d %f", &student1[i].id, &student1[i].score);
}
for (i = 0; i < n; i++)
{
dest[i] = student1[i]; //Keeping the copy of array student1
}
//Tried to store the highest score here
for (i = 0; i < n; i++)
{
if (dest[highest].score < dest[i].score)
highest = i;
}
for (i = 0; i < n; i++)
{
if (dest[i].score = dest[highest].score) //Finally checking the highest score here
printf ("Highest Scorer: %d %f\n", dest[highest].id,
dest[highest].score);
}
return 0;
}
I've two things that doesn't match my criteria. The above code doesn't get me the second output given in the sample input two. When both the id has same score, it doesn't give any output. Secondly, Whenever I've the highest score, unfortunately the result is the following:
Highest Scorer: 1004 200
Highest Scorer: 1004 200