C practice. I'm not sure how to do the 3rd sentence. Write a program that will prompt the user to input ten integer values. The program will display the smallest and greatest of those values. It also displays the value that occur the most.
This what I have so far: #include<stdio.h> #include<stdlib.h> int main(){ int a[10]; int i; //Inputting ten integer values for(i=0; i<10; i++ ) { printf("Enter integer number %d: ", i+1); fflush(stdout); scanf("%d", &a[i]); } //Display the MAX and MIN int max; int min; max=a[0]; min=a[0]; int e=0; while(e<10){ if(a[e]>max) { max=a[e]; } else if(a[e]<min) { min=a[e]; } e++; } printf("Maximum is: %d\n ", max); printf("Minimum is: %d \n", min); return 0; }
This really needs refining: max = 0; /* index of max matches */ int x; int previous = 0; /* previous greatest num of occurences */ for( i = 0; i < 10; i++ ) /* check all the numbers */ { x = 0; /* counter for matches */ for( e = i; e < 10; e++ ) /* against the rest */ { if( a[ i ] == a[e] ) x++; } if ( x > previous ) { max = i; previous = x; } } printf( "%d occured %d times.\n", a[ max ], x ); This isn't tested code. It's just to get you started. It's first draft code.
yes i got it thank you! but for the last line: `printf( "%d occured %d times.\n", a[ max ], x );` x should be changed to previous. sorry for the late response :)
Join our real-time social learning platform and learn together with your friends!