Maximum average, minimum average, and average - java

I am unsure on how to get the maximum average, minimum average, and average of everything in total. I already set variables with values for them in my code, however I am not sure how to input the averages in my code. Any help would be great! Thanks.
import java.util.Scanner;
public class Averages {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int max = 0;
int min = 9000;
int score = 0;
int n;
int c;
int rowMaxNumber = 0;
int rowMinNumber = 0;
int columnMax = 0;
int columnMin = 0;
int averageMin = 0;
int averageMax = 0;
double average = 0;
System.out.println("How many rows?");
n = scan.nextInt();
for (int row = 1; row <= n; row++) {
System.out.println("How many student in row " + row + "?");
c = scan.nextInt();
for (int column = 1; column <= c; column++) {
System.out.println("Score for student " + column + " in row "
+ row + "?");
score = scan.nextInt();
rowMaxNumber = row;
columnMax = column;
rowMinNumber = row;
columnMin = column;
}
if (score >= max) {
max = score;
}
if (score < min) {
min = score;
}
}
System.out.println("Student " + columnMax + " of row " + rowMaxNumber
+ " was highest with " + max);
System.out.println("Student " + columnMin + " of row " + rowMinNumber
+ " was lowest with " + min);
System.out.println("Row " + rowMaxNumber + " had highest average with "
+ averageMax);
System.out.println("Row " + rowMinNumber + " had lowest average with "
+ averageMax);
System.out.println("Class average is " + average);
}
}

public class Averages {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int max = 0;
int min = 9000;
int score = 0;
int n;
int c;
int rowsum = 0;
int rowmaxnumber = 0;
int rowminnumber = 0;
int columnmax = 0;
int columnmin = 0;
int averagemin = 9999;
int averagemax = 0;
double average = 0;
System.out.println("How many rows?");
n = scan.nextInt();
for (int row = 1; row <= n; row++) {
System.out.println("How many student in row " + row + "?");
c = scan.nextInt();
rowsum = 0;
for (int column = 1; column <= c; column++) {
System.out.println("Score for student " + column + " in row "
+ row + "?");
score = scan.nextInt();
rowsum += score;
rowmaxnumber = row;
columnmax = column;
rowminnumber = row;
columnmin = column;
if (score >= max) {
max = score;
}
if (score < min) {
min = score;
}
}
int avg=rowsum/c;
if( avg >= averagemax){
averagemax=avg;
}
if( avg <= averagemin){
averagemin=avg;
}
}
System.out.println("Student " + columnmax + " of row " + rowmaxnumber
+ " was highest with " + max);
System.out.println("Student " + columnmin + " of row " + rowminnumber
+ " was lowest with " + min);
System.out.println("Row " + rowmaxnumber + " had highest average with "
+ averagemax);
System.out.println("Row " + rowminnumber + " had lowest average with "
+ averagemin);
System.out.println("Class average is " + average);
}
}
I have restructured some code to calculate sum of scores as we see them in a row, and calculate the average on end of scanning row, also avg will give the average by dividing rowsum by no. of students in row, and then avg is compared with the previous highest and previous lowest averages. Just like you do it with score! Good luck.

I figured out how to get the maximum average, minimum average, and total average. Thanks for anybody that helped!
import java.util.Scanner;
public class Averages
{
public static void main ( String[] args )
{
Scanner scan = new Scanner(System.in);
int max = -9999;
int min = 9999;
int score = 0;
int n;
int c;
int rowsum = 0;
int rowMaxNumber = 0;
int rowMinNumber = 0;
int columnMax = 0;
int columnMin = 0;
double averageMin = 9999;
double averageMax = 0;
double avg = 0.0;
double avgTotal = 0.0;
double totalScore = 0;
double totalStudent = 0;
System.out.println("How many rows?");
n = scan.nextInt();
for (int row = 1; row <= n; row++) {
System.out.println("How many student in row " + row + "?");
rowsum=0;
c = scan.nextInt();
totalStudent += c;
for (int column = 1; column <= c; column++) {
System.out.println("Score for student " + column + " in row " + row + "?");
score = scan.nextInt();
rowsum += score;
totalScore += score;
if (score >= max) {
max = score;
columnMax = column;
}
if (score < min) {
min = score;
columnMin = column;
}
}
avg = rowsum/c;
if( avg >= averageMax){
averageMax=avg;
rowMaxNumber = row;
}
if( avg < averageMin){
averageMin=avg;
rowMinNumber = row;
}
}
avgTotal = totalScore/totalStudent;
System.out.println("Student " + columnMax + " of row " + rowMaxNumber + " was highest with " + max);
System.out.println("Student " + columnMin + " of row " + rowMinNumber + " was lowest with " + min);
System.out.println("Row " + rowMaxNumber + " had highest average with " + averageMax);
System.out.println("Row " + rowMinNumber + " had lowest average with " + averageMin);
System.out.println("Class average is " + avgTotal);
}
}

Related

for loop to calculate students' average mark. my average keeps on adding up

I had this assignment which I need to use Java to create a for-loop to calculate the average of each students' score. The teacher must enter number of test score per student and the loop will then iterate for each student.
import java.util.Scanner;
public class average {
public static void main(String[] args) {
int count = 0;
int student;
int sum = 0;
int grade;
int average = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the number of students:");
student = keyboard.nextInt();
System.out.println("Enter number of test scores per student: ");
count = keyboard.nextInt();
for (int s = 0; s < student; s++) {
System.out.println("Student number " + (s + 1) );
for (int i = 0; i < count; i++) {
System.out.println("Enter score no." + (i + 1) + " : ");
grade = keyboard.nextInt();
sum += grade;
average = sum/count;
}
System.out.println("The average is " + average);
}
}
}
Make sum = 0 after printing average
Just shown in below loop
for (int s = 0; s < student; s++) {
System.out.println("Student number " + (s + 1));
for (int i = 0; i < count; i++) {
System.out.println("Enter score no." + (i + 1) + " : ");
grade = keyboard.nextInt();
sum += grade;
average = sum / count;
}
System.out.println("The average is " + average);
sum = 0; //THIS IS REQUIRED
}

Calculate the number of pyramid

I'm trying to calculate the sum of the numbers in my pyramid in java. For this, mathematical rule is 2+5+8+9. I mean first row+first number of second row+ second number of third row like that.
int[] numbers = { 2,5,7,1,8,3,6,0,9,4 };
System.out.println(" " + numbers[0]);
System.out.println(" " + numbers[1] + " " + numbers[2]);
System.out.println(" " + numbers[3] + " " + numbers[4] + " " + numbers[5]);
System.out.println("" + numbers[6] + " " + numbers[7] + " " + numbers[8] + " " + numbers[9]);
For example:
2
5 7
1 8 3
6 0 9 4
How can I calculate 2+5+8+9 in Java?
The simplest way of calculating 2+5+8+9 is using Java build-in feature:
int result = 2+5+8+9;
You should construct your pyramid as a 2D array.
int[] numbers = { 2,5,7,1,8,3,6,0,9,4 };
int addedElements = 0;
int nextSize = 1;
ArrayList<int[]> pyramid = new ArrayList<>();
while(addedElements< numbers.size()) {
int[] level = new int[nextSize++];
for (int i = 0; i < nextSize - 1; i++) {
level[i] = numbers[addedElements++];
}
}
int result = 0;
//add maximum of each `int[]` in pyramid.
for (int[] array : pyramid) {
int currentMax = array[0];
for (int i = 0; i < array.size(); i++) {
if (array[i] > currentMax) {
currentMax = array[i];
}
result+=currentMax;
}
System.out.println(result);
Try the follwoing code :
int[] numbers = { 2,5,7,1,8,3,6,0,9,4 };
int index = 1;
int number = 2;
int result = numbers[0];
while (index < numbers.length) {
result += numbers[index + number -2];
index += number;
number += 1;
}
System.out.println(result);
But the whole whing would be much easier and clearer if you just put your pyramide into a 2 dimensional array.
int[][] numbers = { {2},
{5,7},
{1,8,3},
{6,0,9,4} };
int result = numbers[0][0];
for (int i = 1; i < numbers.length; i++) {
result += numbers[i][i-1];
}
System.out.println(result);

Getting min and max values from an array - Java

import java.util.Scanner;
public class Sales {
public static void main(String[] args) {
int[] sales;
sales = getSales();
printSales(sales);
printSummary(sales);
}
private static int[] getSales() {
Scanner input = new Scanner(System.in);
int[] temp;
System.out.print("Enter the number of salespeople: ");
temp = new int[input.nextInt()];
for (int i = 0; i < temp.length; i++) {
System.out.print("Enter sales for salesperson " +
(i + 1) + ": ");
temp[i] = input.nextInt();
}
return temp;
}
private static void printSales(int[] s) {
System.out.println();
System.out.println("Salesperson Sales");
System.out.println("----------- -----");
for (int i = 0; i < s.length; i++) {
System.out.printf("%6d%12d\n", i + 1, s[i]);
}
}
private static void printSummary(int[] s) {
int sum = 0;
int max_sale = 0; // Salesperson with the most sales
int min_sale = 0; // Salesperson with the least sales
for (int i = 0; i < s.length; i++) {
sum = (s[i] + sum);
if (s[i] > max_sale)
max_sale = s[1];
else if (s[i] > min_sale)
s[i] = min_sale;
}
System.out.println();
System.out.println("Total sales: " + sum);
System.out.println("Average sales: " + (double)sum / s.length);
System.out.println("Salesperson " + (max_sale + 1) +
" had the maximum sale with " +
s[max_sale]);
System.out.println("Salesperson " + (min_sale + 1) +
" had the minimum sale with " +
s[min_sale]);
}
}
The purpose of the application is to take the number of salespeople as input, along with their sales and then display individual sales, total sales, and average. That is working fine, but it's also supposed to display which salesperson had the max and minimum sales and what they were (lines 51 - 54). At the moment, any time the max is greater than the number of salespeople I get an ArrayIndexOutOfBoundsException and for whatever reason can't figure it out.
1 - Modify your for loop to get the max and min without modifying the array
2 - Try to print max and min instead of printing sum[max] and some[min] (which can throws IndexOutOfBoundsException)
3 - min_sale should be greater than 0, actually a value enough large (because you can have only positive values in your array)
To summarize :
int sum = 0;
int max_sale = Integer.MIN_VALUE; // Salesperson with the most sales
int min_sale = Integer.MAX_VALUE; // Salesperson with the least sales
for (int i = 0; i < s.length; i++){
sum = (s[i] + sum);
if (s[i] > max_sale)
max_sale = s[i];
else if (s[i] < min_sale)
min_sale = s[i];
}
System.out.println("Salesperson " +
" had the maximum sale with " +
max_sale);
System.out.println("Salesperson " +
" had the minimum sale with " +
min_sale);
if (s[i] > max_sale)
max_sale = s[1];
else if (s[i] > min_sale)
s[i] = min_sale;
Here you are trying to assign the value in s[1] to max_sales. whereas you should be assigning max_sale = i. and the if condition should be
if(s[i] > s[max_sale] )
Updated code:
for (int i = 0; i < s.length; i++)
{
sum = (s[i] + sum);
// Finds the index of the sales person with best sales
if (s[i] >= s[max_sale])
max_sale = i;
// If this sales person is not the best, then check if he is last
else if (s[min_sale] > s[i])
min_sale = i;
}
the specific problem that's causing your error is here,
System.out.println("Salesperson " + (max_sale + 1) +
" had the maximum sale with " +
s[max_sale]);
System.out.println("Salesperson " + (min_sale + 1) +
" had the minimum sale with " +
s[min_sale]);
you're using your result as though it were an index
change it to the following
System.out.println("Salesperson " + (max_sale + 1) +
" had the maximum sale with " +
max_sale);
System.out.println("Salesperson " + (min_sale + 1) +
" had the minimum sale with " +
min_sale);

Creating multiplication table by looping in Java

I was tasked to make a multiplication table from 1-10 but I was not satisfied with my code, it seems to be long:
for (int i = 1; i <= 10; i++)
{
System.out.println("1x" + i + " = " + i + "\t" + "2x" + i + " = " + (2*i)
+ "\t" + "3x" + i + " = " + (3*i) + "\t" + "4x" + i + " = " + (4*i)
+ "\t" + "5x" + i + " = " + (5*i) + "\t" + "6x" + i + " = " + (6*i)
+ "\t" + "7x" + i + " = " + (7*i) + "\t" + "8x" + i + " = " + (8*i)
+ "\t" + "9x" + i + " = " + (9*i) + "\t" + "10x" + i + " = " + (10*i));
}
Output:
1x1 = 1 2x1 = 2
1x2 = 2 2x2 = 4
etc.
Try something like
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
System.out.println(i + "x" + j + "=" (i*j));
}
}
so you have an inner and an outer loop, controlling what you want multiplied and what you want it multiplied by.
To be more explicit you could (should?) rename i and j as multiplier and multiplicand
This will format the table how you have it in your example code, and uses two loops:
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
System.out.print(i + "x" + j + "=" + (i * j) + "\t");
}
System.out.println();
}
The outer loop controls the rows in the multiplication table and the inner loop controls the columns in the multiplication table. System.out.println() signifies moving into a new row of the table
You could use two loops:
for (int i = 1; i <= 10; i++)
{
for (int j = i; j <= 10; j++)
{
System.out.println(i + "x" + j + "=" + (i*j));
}
}
for (int i = 1; i <= 10; i++)
{
for(int j=1; j<10; j++){
System.out.println(j+"x"+i+"="+(j*i)+"\t");
}
System.out.println("\n");
}
import java.util.Scanner;
public class TableMultiplication {
public static void main(String[] args) {
int table,count,total;
//Reading user data from console
Scanner sc = new Scanner(System.in);
//reading data for table
System.out.println("Enter Your Table:");
table = sc.nextInt();
//reading input for how much want to count
System.out.println("Enter Your Count to Table:");
count = sc.nextInt();
//iterate upto the user required to count and multiplay the table and store in the total and finally display
for (int i = 1; i < count; i++) {
total = table*i;
System.out.println(table+"*"+i+"="+total);
}//for
}//main
}//TableMultiplication
import java.util.Scanner;
public class P11 {
public static void main(String[] args) {
Scanner s1=new Scanner(System.in);
System.out.println("Enter a value :");
int n=s1.nextInt();
for(int i=1;i<=10;i++)
{
for(int j=1;j<=10;j++)
{
System.out.println(i+"x"+j+ "="+(i*j)+"\t");
}
}
}
}

How to return person with max sales in salesperson program- JAVA [PT. 2]

Sorry if I'm being bothersome. But I am stuck on another part of my HW problem. I am now tasked at instead of having an salesperson 0, salespersons start with 1. I have tried to solve this by parsing the strings i to an integer and adding 1. Seemed like it would work. However, for some reason the calculating of min is not correct. It stays at 0. I've tried stepping through the code and seeing why, but I cannot see it.
import java.util.Scanner;
public class Cray {
public static void main(String[] args){
final int SALESPEOPLE = 5;
int[] sales = new int[SALESPEOPLE];
int sum, randomValue, numGreater = 0;
int max = sales[0];
int min = sales[0];
int maxperson = 0;
int minperson = 0;
Scanner scan = new Scanner(System.in);
for (int i=0; i<sales.length; i++)
{
//To attempt print out the information of salesperson 0 and salesperson 1, I returned the integer value of i and added one.
System.out.print("Enter sales for salesperson " + Integer.valueOf(i+1) + ": ");
sales[i] = scan.nextInt();
//max if statemnent works fine and is correct
if(sales[i] > max){
max= sales[i];
maxperson = i;
}
//For some reason max is calculating but not min.
if(sales[i] < min){
min = sales [i];
minperson= i;
}
}
System.out.println("\nSalesperson Sales");
System.out.println("--------------------");
sum = 0;
for (int i=0; i<sales.length; i++)
{
System.out.println(" " + Integer.valueOf(i+1) + " " + sales[i]);
sum += sales[i];
}
System.out.println("\nTotal sales: " + sum);
System.out.println("Average sales: " + sum/5);
System.out.println();
System.out.println("Salesperson " + Integer.valueOf(maxperson+1) + " had the most sales with " + max );
System.out.println("Salesperson " + Integer.valueOf(minperson+1) + " had the least sales with " + min);
System.out.println();
System.out.println("Enter any value to compare to the sales team: ");
randomValue = scan.nextInt();
System.out.println();
for(int r=0; r < sales.length; r++){
if(sales[r] > randomValue){
numGreater++;
System.out.println("Salesperson " + Integer.valueOf(r+1) + " exceeded that amount with " + sales[r]);
}
}
System.out.println();
System.out.println("In total, " + numGreater + " people exceeded that value");
}
}
The problem is that the min is never set as all positive sales will be > 0
You should initialise min as a large value so that a positive sales figure can be less then the minimum value specified in your if statement:
if (sales[i] < min) {
You could use:
int min = Integer.MAX_VALUE;

Categories