+11 votes

The average will give an incorrect answer pls help

import java.util.Scanner;

public class Act7 {

public static void main(String args[])

{

Scanner input = new Scanner(System.in);

System.out.print("Enter the number of sections: ");

//inputting the number into numSec

int numSec = input.nextInt();

//inputting the number of students per section

//creating an Array called numStu to create a series of containers to store the number of students

int[] numStu = new int[numSec];

for (int i = 0; i < numSec; i++){

System.out.print("Enter the number of students in section " + (i+1) + " : ");

numStu[i] = input.nextInt();

}

// new 2d Array to put the exam scores of students per section

int[][] scores = new int[numSec][];

for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++){

scores[sectionIndex] = new int[numStu[sectionIndex]];

for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++){

System.out.print("Enter the exam score of student " + (studentIndex+1) + " of section " + (sectionIndex+1) + " : ");

scores[sectionIndex][studentIndex] = input.nextInt();

}

}

double[] average = new double[numSec];

for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++)

{

int sum = 0;

for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++)

{

sum += scores[sectionIndex][studentIndex];

}

average[sectionIndex] = sum /= numStu[sectionIndex];

System.out.println("The average score in section " + (sectionIndex+1) + " : " + average[sectionIndex]);

}

}

}

import java.util.Scanner;

public class Act7 {

public static void main(String args[])

{

Scanner input = new Scanner(System.in);

System.out.print("Enter the number of sections: ");

//inputting the number into numSec

int numSec = input.nextInt();

//inputting the number of students per section

//creating an Array called numStu to create a series of containers to store the number of students

int[] numStu = new int[numSec];

for (int i = 0; i < numSec; i++){

System.out.print("Enter the number of students in section " + (i+1) + " : ");

numStu[i] = input.nextInt();

}

// new 2d Array to put the exam scores of students per section

int[][] scores = new int[numSec][];

for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++){

scores[sectionIndex] = new int[numStu[sectionIndex]];

for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++){

System.out.print("Enter the exam score of student " + (studentIndex+1) + " of section " + (sectionIndex+1) + " : ");

scores[sectionIndex][studentIndex] = input.nextInt();

}

}

double[] average = new double[numSec];

for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++)

{

int sum = 0;

for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++)

{

sum += scores[sectionIndex][studentIndex];

}

average[sectionIndex] = sum /= numStu[sectionIndex];

System.out.println("The average score in section " + (sectionIndex+1) + " : " + average[sectionIndex]);

}

}

}

closed with the note:
answered

+1 vote

Best answer

Your code works almost fine, except for the last step in the average calculation. The problem is that if you divide an integral number (sum) with another integral number (number of students in a given section) the result will be an integral number, i.e., the fractions are ignored.

To have the fractions included, you need to use a floating-point number, such as double.

To fix this, the easiest solution is to turn sum into a double instead of int.

You don't need to update sum's value to be the average (/=) so you can just use simple division (/) instead of assignemnt with division (/=). It does not cause any actual problem, the result would be the same. It's just pointless to change sum in the final step so why do it? :)

Last, but not least, the class having the main() function should be called Main.

import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner input = new Scanner(System.in); System.out.print("Enter the number of sections: "); //inputting the number into numSec int numSec = input.nextInt(); //inputting the number of students per section //creating an Array called numStu to create a series of containers to store the number of students int[] numStu = new int[numSec]; for (int i = 0; i < numSec; i++){ System.out.print("Enter the number of students in section " + (i+1) + " : "); numStu[i] = input.nextInt(); } // new 2d Array to put the exam scores of students per section int[][] scores = new int[numSec][]; for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++){ scores[sectionIndex] = new int[numStu[sectionIndex]]; for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++){ System.out.print("Enter the exam score of student " + (studentIndex+1) + " of section " + (sectionIndex+1) + " : "); scores[sectionIndex][studentIndex] = input.nextInt(); } } double[] average = new double[numSec]; for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++) { //System.out.print("Section " + (sectionIndex + 1) + ": "); double sum = 0.0; for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++) { //System.out.print(scores[sectionIndex][studentIndex] + ", "); sum += scores[sectionIndex][studentIndex]; } //System.out.println(); average[sectionIndex] = sum / numStu[sectionIndex]; System.out.println("The average score in section " + (sectionIndex+1) + " : " + average[sectionIndex]); } } }

The original description (see here) has incorrect results. Feel free to check things with a calculator. For the inputs provided in that example input the correct averages are:

85.33

89.0

86.4

Let's consider what we have for calculating the averages per section:

double[] average = new double[numSec];

for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++)

{

double sum = 0.0;

for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++)

{

sum += scores[sectionIndex][studentIndex];

}

average[sectionIndex] = sum / numStu[sectionIndex];

System.out.println("The average score in section " + (sectionIndex+1) + " : " + average[sectionIndex]);

}

Calculating a total average can be done via a similarly structured loop. However, the sum should be kept incremented throughout the whole iteration and only set to 0 once when the whole iteration begins.

Similarly, to get the total average, we should only divide once by the total number of students (summarize the numStu for every section).

(There are other solutions too, but this is one of the easiest to understand.)

I hope this helps. You should be able to write the code based on this.

Share your attempt if it still doesn't work quite well.

double[] average = new double[numSec];

for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++)

{

double sum = 0.0;

for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++)

{

sum += scores[sectionIndex][studentIndex];

}

average[sectionIndex] = sum / numStu[sectionIndex];

System.out.println("The average score in section " + (sectionIndex+1) + " : " + average[sectionIndex]);

}

Calculating a total average can be done via a similarly structured loop. However, the sum should be kept incremented throughout the whole iteration and only set to 0 once when the whole iteration begins.

Similarly, to get the total average, we should only divide once by the total number of students (summarize the numStu for every section).

(There are other solutions too, but this is one of the easiest to understand.)

I hope this helps. You should be able to write the code based on this.

Share your attempt if it still doesn't work quite well.

Sir, I think I did it. I created another variable called allsum then made this, I know it is not good but correct me if it feels wrong.

double[] average = new double[numSec];

for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++)

{

double sum = 0;

for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++)

{

sum += scores[sectionIndex][studentIndex];

}

average[sectionIndex] = sum / numStu[sectionIndex];

allsum = allsum + sum;

System.out.println("The average score in section " + (sectionIndex+1) + " : " + String.format("%.2f", average[sectionIndex]));

}

allsum = allsum / (2 * numSec);

System.out.println("The average score in all sections : " + String.format("%.2f", allsum));

double[] average = new double[numSec];

for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++)

{

double sum = 0;

for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++)

{

sum += scores[sectionIndex][studentIndex];

}

average[sectionIndex] = sum / numStu[sectionIndex];

allsum = allsum + sum;

System.out.println("The average score in section " + (sectionIndex+1) + " : " + String.format("%.2f", average[sectionIndex]));

}

allsum = allsum / (2 * numSec);

System.out.println("The average score in all sections : " + String.format("%.2f", allsum));

allsum = allsum / (2 * numSec);

Why divide it by 2 * numSec? That's not the total number of students in all the sections.

Why divide it by 2 * numSec? That's not the total number of students in all the sections.

how can I get the maximum and the minimum score of the exam scores of the students?

double max = 0, min = 0;

for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++)

{

for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++)

{

if (max > scores[sectionIndex][studentIndex])

{

max = scores[sectionIndex][studentIndex];

}

if (min > scores[sectionIndex][studentIndex])

{

min = scores[sectionIndex][studentIndex];

}

}

}

double max = 0, min = 0;

for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++)

{

for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++)

{

if (max > scores[sectionIndex][studentIndex])

{

max = scores[sectionIndex][studentIndex];

}

if (min > scores[sectionIndex][studentIndex])

{

min = scores[sectionIndex][studentIndex];

}

}

}

I mean I want to get the average of all the sections using the average per section, but i think it will lessen the burden if I add all the exam scores then divide it by the product of number of sections and 2 so that It can get the average of the examscores in all sections

I also tried using import java.util.Array thingy that i found on the internet and tried to apply it into the code

double[] average = new double[numSec];

double allsum = 0, min = 0, max = 0;

for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++)

{

double sum = 0;

for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++)

{

sum += scores[sectionIndex][studentIndex];

int a[] = scores[sectionIndex];

Arrays.sort(a);

min = a[0];

max = a[a.length-1];

}

average[sectionIndex] = sum / numStu[sectionIndex];

allsum = allsum + sum;

System.out.println("The average score in section " + (sectionIndex+1) + " : " + String.format("%.2f", average[sectionIndex]));

}

allsum = allsum / (2 * numSec);

System.out.println("The average score in all sections : " + String.format("%.2f", allsum));

System.out.println("The highest score among all section : " + String.format("%.2f", max));

System.out.println("The lowest score among all section : " + String.format("%.2f", min));

double[] average = new double[numSec];

double allsum = 0, min = 0, max = 0;

for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++)

{

double sum = 0;

for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++)

{

sum += scores[sectionIndex][studentIndex];

int a[] = scores[sectionIndex];

Arrays.sort(a);

min = a[0];

max = a[a.length-1];

}

average[sectionIndex] = sum / numStu[sectionIndex];

allsum = allsum + sum;

System.out.println("The average score in section " + (sectionIndex+1) + " : " + String.format("%.2f", average[sectionIndex]));

}

allsum = allsum / (2 * numSec);

System.out.println("The average score in all sections : " + String.format("%.2f", allsum));

System.out.println("The highest score among all section : " + String.format("%.2f", max));

System.out.println("The lowest score among all section : " + String.format("%.2f", min));

Using Array.sort() and picking the first and last element is a good solution to find the minimum and maximum of an array. So kudos for that.

However, this kind of solution is not always available or optimal. You should be able to find the minimum and maximum without sorting your inputs.

A more generic solution is based on your previous attempts:

double max = 0, min = 0;

for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++)

{

for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++)

{

if (max > scores[sectionIndex][studentIndex])

{

max = scores[sectionIndex][studentIndex];

}

if (min > scores[sectionIndex][studentIndex])

{

min = scores[sectionIndex][studentIndex];

}

}

}

There are a few problems with the above code.

1. The minimum and maximum logic is exactly the same: (___ > scores[sectionIndex][studentIndex]). It's easy to see that you cannot apply the same logic for both minimum and maximum. (min > scores[sectionIndex][studentIndex]) is the correct logic as it says, if we find a score that is less than our minimum (so far), then it should be the new minimum. For calculating the maximum, it should be the other way around: if we find a score that is larger than our currently found maximum, it should be the new maximum.

2. The initial values of minimum and maximum are not the best choices. (max = 0) may be OK for non-negative inputs, as it assumes the largest number so far is the smallest possible number in our range, which in turn will trigger the (max < scores[sectionIndex][studentIndex]) condition and set the maximum to a valid value from our scores input.

For the minimum, we could apply the same logic: let's pick a number that is large enough that all the inputs will be smaller. You could use 100 for instance, if your scores range from [0, 100], but you can write a code that does not care about the business logic but works as generic code for any inputs and takes the largest number possible on the range of double: Double.MAX_VALUE. Similarly, maximum could have taken Double.MIN_VALUE.

Another solution is to initialize both minimum and maximum to the first score, if you know the scores enumeration is not empty.

"I think it will lessen the burden if I add all the exam scores then divide it by the product of number of sections and 2 so that It can get the average of the examscores in all sections"

I don't think that would work. Assume you have only one section with 10 students (scores) in it. You should divide the total sum (of all 10 scores) by the total number of scores (10), but you'd divide it by 2 only.

Count how many scores are there in total, and divide by this number.

However, this kind of solution is not always available or optimal. You should be able to find the minimum and maximum without sorting your inputs.

A more generic solution is based on your previous attempts:

double max = 0, min = 0;

for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++)

{

for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++)

{

if (max > scores[sectionIndex][studentIndex])

{

max = scores[sectionIndex][studentIndex];

}

if (min > scores[sectionIndex][studentIndex])

{

min = scores[sectionIndex][studentIndex];

}

}

}

There are a few problems with the above code.

1. The minimum and maximum logic is exactly the same: (___ > scores[sectionIndex][studentIndex]). It's easy to see that you cannot apply the same logic for both minimum and maximum. (min > scores[sectionIndex][studentIndex]) is the correct logic as it says, if we find a score that is less than our minimum (so far), then it should be the new minimum. For calculating the maximum, it should be the other way around: if we find a score that is larger than our currently found maximum, it should be the new maximum.

2. The initial values of minimum and maximum are not the best choices. (max = 0) may be OK for non-negative inputs, as it assumes the largest number so far is the smallest possible number in our range, which in turn will trigger the (max < scores[sectionIndex][studentIndex]) condition and set the maximum to a valid value from our scores input.

For the minimum, we could apply the same logic: let's pick a number that is large enough that all the inputs will be smaller. You could use 100 for instance, if your scores range from [0, 100], but you can write a code that does not care about the business logic but works as generic code for any inputs and takes the largest number possible on the range of double: Double.MAX_VALUE. Similarly, maximum could have taken Double.MIN_VALUE.

Another solution is to initialize both minimum and maximum to the first score, if you know the scores enumeration is not empty.

"I think it will lessen the burden if I add all the exam scores then divide it by the product of number of sections and 2 so that It can get the average of the examscores in all sections"

I don't think that would work. Assume you have only one section with 10 students (scores) in it. You should divide the total sum (of all 10 scores) by the total number of scores (10), but you'd divide it by 2 only.

Count how many scores are there in total, and divide by this number.

+1 vote

import java.util.*;

public class Main {

public static void main(String args[])

{

Scanner input = new Scanner(System.in);

System.out.print("Enter the number of sections: ");

//inputting the number into numSec

int numSec = input.nextInt();

//inputting the number of students per section

//creating an Array called numStu to create a series of containers to store the number of students

int[] numStu = new int[numSec];

for (int i = 0; i < numSec; i++){

System.out.print("Enter the number of students in section " + (i+1) + " : ");

numStu[i] = input.nextInt();

}

// new 2d Array to put the exam scores of students per section

float [][] scores = new float [numSec][];

for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++){

scores[sectionIndex] = new float [numStu[sectionIndex]];

for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++){

System.out.print("Enter the exam score of student " + (studentIndex+1) + " of section " + (sectionIndex+1) + " : ");

scores[sectionIndex][studentIndex] = input.nextFloat();

}

}

double[] average = new double[numSec];

for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++)

{

float sum = 0;

for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++)

{

sum += scores[sectionIndex][studentIndex];

}

average[sectionIndex] = sum /= numStu[sectionIndex];

System.out.println("The average score in section " + (sectionIndex+1) + " : " + String.format("%.2f", average[sectionIndex]));

}

}

}

public class Main {

public static void main(String args[])

{

Scanner input = new Scanner(System.in);

System.out.print("Enter the number of sections: ");

//inputting the number into numSec

int numSec = input.nextInt();

//inputting the number of students per section

//creating an Array called numStu to create a series of containers to store the number of students

int[] numStu = new int[numSec];

for (int i = 0; i < numSec; i++){

System.out.print("Enter the number of students in section " + (i+1) + " : ");

numStu[i] = input.nextInt();

}

// new 2d Array to put the exam scores of students per section

float [][] scores = new float [numSec][];

for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++){

scores[sectionIndex] = new float [numStu[sectionIndex]];

for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++){

System.out.print("Enter the exam score of student " + (studentIndex+1) + " of section " + (sectionIndex+1) + " : ");

scores[sectionIndex][studentIndex] = input.nextFloat();

}

}

double[] average = new double[numSec];

for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++)

{

float sum = 0;

for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++)

{

sum += scores[sectionIndex][studentIndex];

}

average[sectionIndex] = sum /= numStu[sectionIndex];

System.out.println("The average score in section " + (sectionIndex+1) + " : " + String.format("%.2f", average[sectionIndex]));

}

}

}

...

edited May 21, 2022 by Lexus Guevara

double[] average = new double[numSec];

for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++)

{

float sum = 0;

for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++)

{

sum += scores[sectionIndex][studentIndex];

}

allsum = sum + sum;

average[sectionIndex] = sum / numStu[sectionIndex];

System.out.println("The average score in section " + (sectionIndex+1) + " : " + String.format("%.2f", average[sectionIndex]));

average[sectionIndex] = average[sectionIndex] + average[sectionIndex];

allsum = average[sectionIndex] / numSec;

}

allsum = allsum / numSec;

System.out.println("The average score in all sections : " + allsum);