Reputation: 11
I cant seem to figure out why I cant get the sum of my array to work.
I'm trying to get the average from an array of 10 imputed by the user, the user has the choice to stop the input by entering a number 0 or below. the program will auto make all cut off numbers into 0.
I just need a way to get the sum of all the numbers from the array.
i tried to do something with a variable called sum inside the getNames method but im unsure if im able to send it to the main.
package arraydowhile;
import java.util.Scanner;
import java.lang.*;
import static java.lang.Float.sum;
/**
*
*
*/
public class Arraydowhile {
//constants
static final int MAXIMUM_NUMBERS = 10;
static final int MINIMUM_NAMES = 0;
static final float AVERAGE_COUNT = 10;
static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
int[] numbers = new int[MAXIMUM_NUMBERS]; // an array that can store 10 whole numbers- default value of each element is 0
float[] marks = {8.5f, 4, 6.75f, 3.5f, 10}; //an array that can store real numbers- default size is 5
float[] names ; //an array declaration but no storage allocation yet
float[] averageArray ;
float[] average ;
float sum = 0;
float total =0;
System.out.println("The elements of the 'numbers' array");
System.out.println("Index\t\tValue");
for(int counter = 0; counter < MAXIMUM_NUMBERS; counter++)
{
System.out.printf("%d\t\t%d%n", counter, numbers[counter]);
}//for
System.out.println("-------------------------------------------------------------");
//Every array object knows its own length and stores it in a length instance variable.
System.out.printf("There are %d elements in the marks list%n", marks.length);
for(int counter = 0; counter < marks.length; counter++)
{
if (counter!= marks.length-1){
System.out.printf("%.2f, ",marks[counter]);
}//if
else
{
System.out.printf("%.2f",marks[counter]); //last element does not need a comma after it
}//else
}//for
int totalNames = 10;
names = new float[totalNames];
for(float name: names)
{
System.out.println(name);
}//for
names = getNames(totalNames);
// total = total + names[totalNames];
// average = new float[totalNames];
// average = getAverage(average);
//calculate the average of all the numbers
//System.out.println("The Average was "+total / AVERAGE_COUNT);
if(names != null)
displayNames(names);
else{
System.out.println("------------No numbers were provided------------------");
}//else
System.out.println("The Sum is " );
}//of main
private static float[] getNames(int totalNames)
{
float[] names = new float[totalNames];
int counter = 0;
float name;
float total = 0;
float sum = 0;
float average = sum / counter;
System.out.printf("Please input a real number, %d must be given. You have the option to quit anytime by entering a number from 0 or lower %n", totalNames);
do{
System.out.printf("Please input number %d: ", counter+1);
name = input.nextFloat();
sum = sum+name;
// System.out.println("The Sum was "+sum);
// System.out.println("The Average was "+ sum / counter);
if(name == 0){
System.out.println("You have not provided a number, and nothing will be saved");
break;
}
else if (name < 0)
{
System.out.println("You have decided to end input");
break;
}
else
{
names[counter] = name;
counter++;
}//if
// System.out.println("The Sum was "+sum);
// System.out.println("The Average was "+ sum / counter);
}
while(counter < totalNames);
if(counter == 0)
return null;
else
return names;
// names[counter] = s.nextFloat();
// sum = sum + a[counter];
// total = names[totalnames];
}// of getNames
private static void displayNames(float[] names)
{
//System.out.printf("%d names were entered.%n",names.length);
System.out.println("-----------------List of numbers-------------------");
for(float name: names)
{
// if(name != null)
System.out.println(name);
}//for
}//displayNames
}//of ArrayDemonstration
Upvotes: 1
Views: 232
Reputation: 190
You can get sum of arrays as below,
int sum = 0;
for(int i = 0; i < sampleArray.length; i++){
sum += sampleArray[i];
}
Upvotes: 1