Commented_code_for_part_2[1]

# Commented_code_for_part_2[1] - Let us see it with an...

This preview shows pages 1–2. Sign up to view the full content.

Commented Version #include <iostream> #include <fstream> using namespace std ; void function( int x[]); int main () { //Not required from the exercise int x[ 10 ] = { 70 , 90 , 100 , 100 , 90 , 50 , 60 , 80 , 90 , 90 }; // This array is just for showing purposes, the function requires a 150 element array function (x); return 0 ; } void function( int x[]){ ofstream file; //Declaring a file to write to file. open ( "report.txt" ); //Associating it with real file report.txt float avg= 0 ; // variable used for sum and average int count= 0 ; // counter of how many students failed int array[ 101 ]; // array where the appearances of every score are kept for ( int i= 0 ;i< 101 ;i++) array[i]= 0 ; // initialization as zero. // This is an important step, otherwise you cannot use it as a counter for ( int i= 0 ;i< 10 ;i++){ avg+=x[i]; // adding the scores to the avg in order to find the sum if (x[i]< 60 ) count++; // if the score is less than 60 add 1 to the counter array[x[i]]++; // This is a tricky part. . // What we are trying to do is find the element of the array where // the appearances are kept and add one to that score.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: // Let us see it with an example, in the beginning all elements are zero. // The first element we are accessing is x[0]=70. So, we find the element // array[70] and increase it to 1 (from 0). Doing that for all scores, we // keep track of all the times that a score has appeared. } int max=array[ ]; // Initializing score 0 (array[0]) as the score with most appearances. int k= ; for ( int i= 1 ; i< 101 ;i++){ if (array[i]>max){ // if the score i has appeared more times than the current max score max=array[i]; // substitute the max to the new number of appearances k=i; // and substitute score k (initially zero) to the score i that appears // the most. }} file << "The average is: " << avg/ 10 << endl; // Don't forget divide by 10 for the average! It should be 150. file << "The number of failed students is: " << count << endl; file << "The value that appears the most: " << k << endl; }...
View Full Document

{[ snackBarMessage ]}

### Page1 / 2

Commented_code_for_part_2[1] - Let us see it with an...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online