Point classmates
Based on the assignment we can determine this is the problem of finding the largest number in the array (had stated in article find the largest number).
To do this, we first need to enter is the list points – ie import array. Want to enter the array we need to know the number of array elements. Then find the largest element is finished. However attention of every person is a real number, So we used types float.
/* * Input list of socre of students. Out put max score of list */ #include <stdio.h> int main() { int n; // number student - size of array printf("Enter number student: "); scanf("%d", &n); float s[n], max; // array socre int i; // input array score for(i = 0; i < n; i++) { printf("Enter socre of student %d : ", (i + 1) ); scanf("%f", &s[i]); } // find max max = s[0]; for(i = 1; i < n; i++) { if( max < s[i] ) { max = s[i]; } } printf("Max of list score is %.2f\n", max); return 0; }
In the code above to your attention you assign initial values for variables max before search. Why not assign max = 0 or by -1 or some other which are s[0] – The first element in the array? Do you think if assign max = 0 or by -1 This is all okay, because of everyone from 0 What should older will also find the right style. But in all other, arrays that are negative, finding the max not necessarily true. Assuming initially assign max = 0 there are -1. When entering the full array of smaller negative number -1 there are 0 the finding would be wrong, max will always be the.
With this it's all over all previous upgrades bit. Besides we find the greatest, we also need to find people (position the) have the same highest score. May have 1, 2, 3 or more people with the largest point.
/* * Input list of socre of students. Out put max score of list */ #include <stdio.h> int main() { int n; // number student - size of array printf("Enter number student: "); scanf("%d", &n); float s[n], max; // array socre int i; // input array score for(i = 0; i < n; i++) { printf("Enter socre of student %d : ", (i + 1) ); scanf("%f", &s[i]); } // find max max = s[0]; for(i = 1; i < n; i++) { if( max < s[i] ) { max = s[i]; } } printf("Max of list score is %.2f\n", max); // find all student have max score printf("List index of student have max score:\n"); for(i = 0; i < n; i++) { if(s[i] == max) { printf("%d ", (i + 1) ); } } return 0; }
After this article, hope you can apply to the actual idea of the array of applications, of simple lessons in their daily work.
Recent Comments