Average and sum of multiple numbers

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 10388 Views - Last Post: 30 July 2010 - 07:14 AM Rate Topic: -----

#1 Guest_Tuppi*


Reputation:

Average and sum of multiple numbers

Posted 29 July 2010 - 11:19 AM

I have a program which requires me to output the sum, average, largest and smallest numbers of 10 inputted integer numbers. I have gotten the part where it requires the largest and smallest numbers but I'm stuck on how to output the sum and average. Any of you have any suggestions of what code to use? Thanks

This is what I have so far.

import java.util.*;
import java.io.*;

class Prog68{
	public static void main (String args[]){
	Scanner s = new Scanner(System.in);
		int largest, smallest, current;
		System.out.print("Enter a number: ");
		current = s.nextInt();
		smallest = current;
		largest = current;
		System.out.print("Enter another number: ");
		current = s.nextInt();
		if (largest < current)
			largest = current;
		if (smallest > current)
			smallest = current;
		System.out.print("Enter another number: ");
		current = s.nextInt();
		if (largest < current)
			largest = current;
		if (smallest > current)
			smallest = current;
		System.out.print("Enter another number: ");
		current = s.nextInt();
		if (largest < current)
			largest = current;
		if (smallest > current)
			smallest = current;
		System.out.print("Enter another number: ");
		current = s.nextInt();
		if (largest < current)
			largest = current;
		if (smallest > current)
			smallest = current;
		System.out.print("Enter another number: ");
		current = s.nextInt();
		if (largest < current)
			largest = current;
		if (smallest > current)
			smallest = current;
		System.out.print("Enter another number: ");
		current = s.nextInt();
		if (largest < current)
			largest = current;
		if (smallest > current)
		    smallest = current;
		System.out.print("Enter another number: ");
		current = s.nextInt();
		if (largest < current)
			largest = current;
		if (smallest > current)
			smallest = current;
		System.out.print("Enter another number: ");
		current = s.nextInt();
		if (largest < current)
			largest = current;
		if (smallest > current)
			smallest = current;
		System.out.print("Enter another number: ");
		current = s.nextInt();
		if (largest < current)
			largest = current;
		if (smallest > current)
			smallest = current;


		System.out.println("The sum is: " );
		System.out.println("The average is: " );
		System.out.println("The smallest number is: " + smallest);
		System.out.println("The largest number is: " + largest);

	}
}



Is This A Good Question/Topic? 0

Replies To: Average and sum of multiple numbers

#2 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Average and sum of multiple numbers

Posted 29 July 2010 - 11:51 AM

Well the way you're doing it now you don't have all of your numbers saved at the end of the program, so you can't use them. The only 2 numbers you know at that point are the largest and the smallest.

This program would be much better off using an array and some loops. Every time the user inputs a number you should throw it into an array, and then sort the array at the end of the inputting.

One you have that taken care of it'll be simple.

Getting the sum and average would be something like this:
int total = 0;
for(int i = 0; i < array.length; i++)
{
      int total += array[i];
}//after the for loop finishes total is your sum

double average = total / 10;//this is now your average





That will get you going, but it would be even better to do this using methods. Would make it a lot more resuable.




---
If you really don't want to do it this way, you could just keep a variable total from the beginning, and every time you get input just add current to that total. Sum would be whatever total is equal to at the end, and then for average you could just divide by 10.

Hope this helps!

This post has been edited by eZACKe: 29 July 2010 - 11:53 AM

Was This Post Helpful? 1
  • +
  • -

#3 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: Average and sum of multiple numbers

Posted 29 July 2010 - 01:08 PM

OP, how far has your syllabus covered? Have you learnt loops and arrays yet? Cause from the way you wrote your code, I have a feeling you are not allowed to use loops or arrays in your code. If you're not allowed to use them, then I guess you'll have to make 10 different variables if you want to find the sum and average of the numbers.

This post has been edited by orangcacat: 29 July 2010 - 01:09 PM

Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Average and sum of multiple numbers

Posted 29 July 2010 - 01:09 PM

loops were designed for problems that require repetition, just to take 10 inputs from the user, you wrote a lot of code for it, where you could have done it in 2 lines using loops. Imagine you had to read 100 or 1000 numbers from the user, that would take a lot of typing to do. Here's my version of the solution to this without the use of arrays
Scanner sc = new Scanner(System.in);
int min, max, sum = 0;
float average;

min = sc.nextInt();
max = min;
sum += min;

int x;
for(int i = 0; i < 9; i++)
{
    x = sc.nextInt();
    if(x > max)
        max = x;
    if(x < min)
        min = x;
    sum += x;
}
average = sum/10;
System.out.printf("min number is %d\nmax number is %d\nsum is %d\naverage is %f\n", min, max, sum, average);



hope this helps
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Tuppi*


Reputation:

Re: Average and sum of multiple numbers

Posted 29 July 2010 - 01:43 PM

View Postorangcacat, on 29 July 2010 - 12:08 PM, said:

OP, how far has your syllabus covered? Have you learnt loops and arrays yet? Cause from the way you wrote your code, I have a feeling you are not allowed to use loops or arrays in your code. If you're not allowed to use them, then I guess you'll have to make 10 different variables if you want to find the sum and average of the numbers.


I've just done loops but not arrays. Kind of mixed up right now.
Was This Post Helpful? 0

#6 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Average and sum of multiple numbers

Posted 29 July 2010 - 02:04 PM

Alright, follow the advice on using loops. This assignment is surely an assignment to help you figure out when to use loops!
Was This Post Helpful? 0
  • +
  • -

#7 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: Average and sum of multiple numbers

Posted 29 July 2010 - 02:54 PM

Quote

I've just done loops but not arrays. Kind of mixed up right now.


What do you mean kind of mixed up right now? Do you understand how to use loops? This is an example of how to use for loop.

for(int i = 0;i < 10;i++){
    System.out.println("Haha");
}



This for loop will print "Haha" 10 times. As you can see, for loop takes in 3 arguments. The first argument is the initializer, second argument is the condition to continue the loop, and the third argument is the updater. So basically, this for loop means initially i is 0, as i is less than 10, "Haha" is printed out, and then the value of i is increased by 1. This goes on until i is 9, the "Haha" has been printed out 10 times, and the for loop stops. Try modifying your code and utilize for loop. It'll shorten your code a lot.
Was This Post Helpful? 0
  • +
  • -

#8 Guest_Tuppi*


Reputation:

Re: Average and sum of multiple numbers

Posted 29 July 2010 - 11:34 PM

View Postorangcacat, on 29 July 2010 - 01:54 PM, said:

Quote

I've just done loops but not arrays. Kind of mixed up right now.


What do you mean kind of mixed up right now? Do you understand how to use loops? This is an example of how to use for loop.

for(int i = 0;i < 10;i++){
    System.out.println("Haha");
}



This for loop will print "Haha" 10 times. As you can see, for loop takes in 3 arguments. The first argument is the initializer, second argument is the condition to continue the loop, and the third argument is the updater. So basically, this for loop means initially i is 0, as i is less than 10, "Haha" is printed out, and then the value of i is increased by 1. This goes on until i is 9, the "Haha" has been printed out 10 times, and the for loop stops. Try modifying your code and utilize for loop. It'll shorten your code a lot.


Though is it possible with loops to print out what the user wants to input?
Was This Post Helpful? 0

#9 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Average and sum of multiple numbers

Posted 29 July 2010 - 11:54 PM

Yes anything is possible in a loop.

for(int i = 0; i < 5; i++)
{
  System.out.println("HI");
}


Was This Post Helpful? 0
  • +
  • -

#10 Guest_Tuppi*


Reputation:

Re: Average and sum of multiple numbers

Posted 30 July 2010 - 12:27 AM

import java.util.*;
import java.io.*;

class Prog73{
	public static void main (String args[]){
		int num;
		Scanner s = new Scanner(System.in);
		for(int i = 0;i < 10;i++){
		System.out.println("Enter a number: ");
		num = s.nextInt();

		}
	}
}



So far I've gotten the base down now I need to output the highest, lowest, sum and average. For highest/lowest should I use this type of code or?

                                if (highest<num){
				highest=num;
				}
				if (lowest>num){
				lowest=num;	
				}
 
		}
	}


Was This Post Helpful? 0

#11 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Average and sum of multiple numbers

Posted 30 July 2010 - 12:37 AM

You will want two tracker variables, max and min.

// declare these outside loop, after you declare num
int max = Integer.MIN;
int min = Integer.MAX:

if(num > max)
  max = num;
else if(num < min)
  min = num;


Was This Post Helpful? 0
  • +
  • -

#12 Guest_Tuppi*


Reputation:

Re: Average and sum of multiple numbers

Posted 30 July 2010 - 12:47 AM

I've done what you told me to do but it comes up with an error that variables MIN & MAX cannot be found.

import java.util.*;
import java.io.*;

class Prog73{
	public static void main (String args[]){
		int num;
		int max = Integer.MAX;
		int min = Integer.MIN;
		Scanner s = new Scanner(System.in);
		for(int i = 0;i < 10;i++){
		System.out.println("Enter a number: ");
		num = s.nextInt();

		if(num > max)
  		max = num;

		else if(num < min)
  		min = num;

  		System.out.println("The smallest number is: " +min);
		System.out.println("The largest number is: " +max);

		}
	}
}


Was This Post Helpful? 0

#13 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Average and sum of multiple numbers

Posted 30 July 2010 - 01:04 AM

Oh sorry my bad! Forgot the VALUE part!

Should be:

int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;



By the way, you will want to print your min/max outside the loop at the end of your program.

This post has been edited by bcranger: 30 July 2010 - 01:05 AM

Was This Post Helpful? 0
  • +
  • -

#14 aniri  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 54
  • View blog
  • Posts: 657
  • Joined: 24-November 09

Re: Average and sum of multiple numbers

Posted 30 July 2010 - 01:11 AM

use MAX_VALUE and MIN_VALUE instead of MIN and MAX

and watch out how you initialize the values. it's supposed to be like this:
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;


max takes is initialized to the minimum value and min to the maximum value.

Also, take these lines
System.out.println("The smallest number is: " +min);
System.out.println("The largest number is: " +max);



outside the loop. You will only need to print the results once when the loop ends :)

As for the sum and average, you need another variable for sum, initialize it to 0 before the loop and add the "num" variable to it inside the loop.

For the average you will have to divide this by 10 ;)
Was This Post Helpful? 0
  • +
  • -

#15 Guest_Tuppi*


Reputation:

Re: Average and sum of multiple numbers

Posted 30 July 2010 - 01:43 AM

So should it be something like this?

import java.util.*;
import java.io.*;

class Prog73{
	public static void main (String args[]){
		int num;
		int sum = 0;
		int average;
		Scanner s = new Scanner(System.in);
		for(int i = 0;i < 10;i++){
		System.out.println("Enter a number: ");
		num = s.nextInt();
		int max = Integer.MAX_VALUE;
		int min = Integer.MIN_VALUE;

		if(num > max)
  		max = num;

		else if(num < min)
  		min = num;

		sum = num;
		average = sum/10;

		}
	}
}



Also when you outside the loop where is that exactly? Thanks :)
Was This Post Helpful? 0

  • (2 Pages)
  • +
  • 1
  • 2