Results 1 to 16 of 16
Like Tree2Likes
  • 1 Post By Wnt2bsleepin
  • 1 Post By Sierra

Thread: Help finding sum of an array

  1. #1
    HalfAZN is offline Member
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    32
    Rep Power
    0

    Default Help finding sum of an array

    I have to make a program where I have the user enter the amount of numbers they want to input, and then have a menu that allows them to see the sum, average, printed order, reverse order, number of even numbers, and number of odd numbers. To find the sum of the numbers, I have to know how many numbers the user has entered, but I'm not sure how. (It's choice 1 in the program)




    Java Code:
    import cs1.Keyboard;
    import java.util.*;
    public class moreArray
    	{
    	public static void main(String[] args)
    	{
    
    
    System.out.println("How many elements would you like to enter");
    	int numElements=Keyboard.readInt();
    	int[] a = new int[numElements];
    	int count;
    	int[]b=new int[numElements];
    
    
    
    	if(numElements<=0)
    		System.out.println("Invalid number. Must be greater than 0");
    
    	else
    	{
    
    
    		for (count=0; count<numElements; count++)
    			{
    				System.out.println("Please enter a number for bucket " +(count+1));
    				a[count]=Keyboard.readInt();
    			}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    		System.out.println("1. Sum of numbers");
    		System.out.println("2. Average of numbers");
    		System.out.println("3. Print list backwards");
    		System.out.println("4. Print list forwards");
    		System.out.println("5. Count even numbers");
    		System.out.println("6. Count odd numbers");
    		System.out.println("7. Quit");
    		int choice=Keyboard.readInt();
    
    		if(choice==1)
    		{
    
    			 	
    			}
    
    		if(choice==2)
    		{
    
    
    		}

    Edit: Program isn't done. I'm just having trouble with the first choice
    Last edited by HalfAZN; 04-19-2012 at 06:24 PM.

  2. #2
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Help finding sum of an array

    Sounds like you are putting these numbers into an array. Why not use the length of the array to determine how many numbers the user entered?

  3. #3
    HalfAZN is offline Member
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    32
    Rep Power
    0

    Default Re: Help finding sum of an array

    Quote Originally Posted by Wnt2bsleepin View Post
    Why not use the length of the array to determine how many numbers the user entered?
    How do I do this?

  4. #4
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Help finding sum of an array

    Quote Originally Posted by HalfAZN View Post
    How do I do this?
    You can get the length of the array like this:

    Java Code:
    arrayname.length;
    Notice there is no () there.

  5. #5
    HalfAZN is offline Member
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    32
    Rep Power
    0

    Default Re: Help finding sum of an array

    Quote Originally Posted by Wnt2bsleepin View Post
    You can get the length of the array like this:

    Java Code:
    arrayname.length;
    Notice there is no () there.
    I sent an int (k) equal to a.length, and everything compiled, but when I ran it

    Java Code:
    if(choice==1)
    		{
    
    			 	System.out.println("Sum: " +a[k]);
    	}
    Help finding sum of an array-wtf.png

  6. #6
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Help finding sum of an array

    Can you post the rest of the code with the correction you added? You seem to be trying to print out an array that doesn't exits. If you wanted to print out the last element in the array, use k-1.

  7. #7
    HalfAZN is offline Member
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    32
    Rep Power
    0

    Default Re: Help finding sum of an array

    Java Code:
    import cs1.Keyboard;
    import java.util.*;
    public class moreArray
    	{
    	public static void main(String[] args)
    	{
    
    
    System.out.println("How many elements would you like to enter");
    	int numElements=Keyboard.readInt();
    	int[] a = new int[numElements];
    	int count;
    	int[]b=new int[numElements];
    	boolean go;
    	int k=a.length;
    
    int counter;
    
    	if(numElements<=0)
    		System.out.println("Invalid number. Must be greater than 0");
    
    	else
    	{
    
    
    		for (count=0; count<numElements; count++)
    			{
    				System.out.println("Please enter a number for bucket " +(count+1));
    				a[count]=Keyboard.readInt();
    			}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    		System.out.println("1. Sum of numbers");
    		System.out.println("2. Average of numbers");
    		System.out.println("3. Print list forwards");
    		System.out.println("4. Print list backwards");
    		System.out.println("5. Count even numbers");
    		System.out.println("6. Count odd numbers");
    		System.out.println("7. Quit");
    		int choice=Keyboard.readInt();
    
    		if(choice==1)
    		{
    
    			 	System.out.println("Sum: " +a[k]);
    	}
    
    
    		if(choice==2)
    		{
    
    
    		}
    
    
    
    	if(choice==3)
    		{
    
    
    
    			}
    
    
    }
    
    }
    
    }



    I realize that a[k] isn't the sum, but not sure where to go
    Last edited by HalfAZN; 04-20-2012 at 01:13 AM.

  8. #8
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Help finding sum of an array

    If you want to sum up the elements in the array, you should use a loop.

  9. #9
    HalfAZN is offline Member
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    32
    Rep Power
    0

    Default Re: Help finding sum of an array

    What would that look like? I'm assuming it would be a for loop, but I"m not sure how to construct it

  10. #10
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Help finding sum of an array

    Try to use the for loop, you should come back with the new code if you have trouble - it will look similar to
    Java Code:
    for(int i=0; i<myarray.length; i++) iResult += myarray[i];
    I would also suggest you read about using the switch() statement for your choice evaluation at the end of the program.
    Last edited by Sierra; 04-20-2012 at 10:09 AM.

  11. #11
    HalfAZN is offline Member
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    32
    Rep Power
    0

    Default Re: Help finding sum of an array

    Java Code:
    if(choice==1)
    		{
    
    		for( count=0; count<a.length; count++)
    		{
    			countResult += a[count];
    		}
    			 	System.out.println("Sum: " +a[k]);
    	}
    Is this what it should look like? It gets an error, so not sure

  12. #12
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Help finding sum of an array

    It would be helpful if you tell us WHAT error... ;)
    I would say the sum is countResult, not a[k], right?

  13. #13
    HalfAZN is offline Member
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    32
    Rep Power
    0

    Default Re: Help finding sum of an array

    Quote Originally Posted by Sierra View Post
    It would be helpful if you tell us WHAT error... ;)
    I would say the sum is countResult, not a[k], right?
    Java Code:
    if(choice==1)
    		{
    
    		for( count=0; count<a.length; count++)
    		{
    			countResult += a[count];
    		}
    			 	System.out.println("Sum: " +a[k]);
    	}


    Errors:

    X:AROYAL\menuArray.java:63: cannot find symbol
    symbol : variable countResult
    location: class menuArray
    countResult += a[count];

    X:\AROYAL\menuArray.java:65: cannot find symbol
    symbol : variable countResult
    location: class menuArray
    System.out.println("Sum: " +countResult);

  14. #14
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Help finding sum of an array

    Quote Originally Posted by HalfAZN View Post
    Java Code:
    if(choice==1)
    		{
    
    		for( count=0; count<a.length; count++)
    		{
    			countResult += a[count];
    		}
    			 	System.out.println("Sum: " +a[k]);
    	}


    Errors:

    X:AROYAL\menuArray.java:63: cannot find symbol
    symbol : variable countResult
    location: class menuArray
    countResult += a[count];

    X:\AROYAL\menuArray.java:65: cannot find symbol
    symbol : variable countResult
    location: class menuArray
    System.out.println("Sum: " +countResult);
    Where did you define CountResult?
    HalfAZN likes this.

  15. #15
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Help finding sum of an array

    Probably because you did not declare that variable... and if you are on it, remember to initialize it with zero before you enter the loop. :)
    HalfAZN likes this.

  16. #16
    HalfAZN is offline Member
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    32
    Rep Power
    0

    Default Re: Help finding sum of an array

    Thanks to both of you. That turned out to be the problem. Didn't define CountResult ._.

Similar Threads

  1. Finding a string in an array.
    By rokit boy in forum New To Java
    Replies: 8
    Last Post: 03-18-2012, 07:38 AM
  2. Finding Mode of an array
    By nfill14 in forum New To Java
    Replies: 0
    Last Post: 02-13-2012, 11:51 PM
  3. Algorithm for finding max and min in an array.
    By Shyamz1 in forum New To Java
    Replies: 4
    Last Post: 10-04-2011, 11:45 PM
  4. Finding the Mode in An Array
    By carlodelmundo in forum New To Java
    Replies: 23
    Last Post: 10-31-2010, 01:44 PM
  5. Finding a the max value of the array using a for loop
    By soccer_kid_6 in forum New To Java
    Replies: 1
    Last Post: 04-12-2010, 12:25 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •