Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Imagine that I decided to give you all a pop quiz (Perish the thought!). The gra

ID: 645382 • Letter: I

Question

Imagine that I decided to give you all a pop quiz (Perish the thought!). The grades would be integers from 0 to 10. I want to write a program that reads in each of the grades, and then prints out a histogram of what the grades are. For example, If I entered the following grades (entering a -1 to terminate the input): 8 2 8 6 3 2 9 6 7 8 4 0 10 8 7 -1, here is the output I would expect:

Enter the following program, modify it create a histogram, and run it:



Now, modify the above program to create a function to go through the grade_list to find and return what the average grade is (and return it to the main -- do not print it in the function), and another function to determine which grade occurred most frequently.

Explanation / Answer

import java.util.Scanner;

class HelloWorld{

final static int GRADERANGE = 11; // allow for scores from 0 - 10

  

public static void main ( String[] args ) {

int grade_list[] = new int[GRADERANGE];

int grade, average_grade, freq;

Scanner in = new Scanner( System.in );

initialize_array( grade_list, GRADERANGE );

  

// Now read in the input

System.out.println( "Enter a grade (-1 to terminate)" );

grade = in.nextInt();

  

//while ( (grade != -1) && (grade >=0 && grade<=10 ) ) {

while ( (grade != -1) ) {

/* modify the grade_list array to account for the grade

how do you do this??? */

  

grade_list[grade] = grade_list[grade] +1;

//System.out.println("Value at grade"+grade+ " is "+grade_list[grade]);

// now read in another grade

System.out.println("Enter a grade (-1 to terminate)");

grade = in.nextInt();

}

// now print out the histogram

print_histogram( grade_list, GRADERANGE );

  

  

freq = freq_grade( grade_list, GRADERANGE );

System.out.println("Freq grade is "+freq);

  

average_grade = avg_grade( grade_list, GRADERANGE );

System.out.println("Avg grade is "+average_grade);

return;

} // end main

static void initialize_array ( int A[], int length ) {

// how do I initialize array A?????

  

// No need. The array, by default, is initialized to zero

//System.out.println("Value at grade0 is "+A[0]);

  

return;

}

/****************************************************************

function print_histogram

This function draws a histogram of the values passed in by

an array

Input: an array of ints

how many elements are in the array

Output: None, but the histogram is drawn

***************************************************************/

static void print_histogram ( int list[], int length ) {

  

int index;

for (index = 0; index < length; index ++) {

System.out.print( index + " |" );

Drawrow( list[index] );

}

return;

}

/*****************************************************************

function Drawrow

This function draws a row of stars, whose length is specified

by the caller

Input: the length of the row of stars to be drawn (an int)

the output object

Output: None, but a row of stars is drawn

****************************************************************/

static void Drawrow ( int numstars ) {

int count;

for (count = 1; count <= numstars; count ++) {

System.out.print( "*" );

}

System.out.println();

return;

}

static int avg_grade ( int A[], int length ) {

int avg = 0;

int count, total = 0;

  

for (count = 0; count < length; count ++) {

avg = avg + A[count]*count;

total = total + A[count];

}

  

return (int)avg/total;

}

  

  

static int freq_grade ( int a[], int length ) {

int count = 1, tempCount;

int popular = a[0];

int temp = 0;

for (int i = 0; i < (length - 1); i++)

{

temp = a[i];

tempCount = 0;

for (int j = 1; j < length; j++)

{

if (temp == a[j])

tempCount++;

}

if (tempCount > count)

{

popular = temp;

count = tempCount;

}

}

return popular;

}

  

  

} // end class