A little trouble with Arrays

I need to read in value to my array

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 660 Views - Last Post: 13 September 2010 - 05:49 PM Rate Topic: -----

#1 LumberJack10  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 27-February 10

A little trouble with Arrays

Posted 12 September 2010 - 10:45 AM

I am currently getting a StackOverFlow error that I cannot get rid of and some slight Logical errors.
import java.io.*;
import java.util.*;







public class prog1_1 {
    
    public static void main(String[] args) {
    	int[] list = {43, 98, 82, 57, 78, 63, 88, 26, 91, 76, 75, 67, 49, 77, 83, 72, 69, 84, 65,
    				  73, 78, 89, 46, 76, 66, 87, 22, 45, 85, 92, 88, 55, 89, 77, 78, 67, 99, 63, 68, 71, 79, 82, 68, 70};
    
    	System.out.println("The lowest grade is " + minIndex(list));
    	System.out.println("The highest grade is " + maxIndex(list));
    	System.out.println("The mean is " + meanIndex(list));
    	System.out.println("The standard deviation is " + standardDeviationIndex(list));
    	System.out.println("The mode is " + modeIndex(list));
    	System.out.println("The range is " + rangeIndex(list));
    	System.out.println("The median is " + medianIndex(list));
    }
    	
    public static int minIndex(int[] list) {
    	int min = list[0];
    	int minIndex = 0;

    	for (int i = 1; i < list.length; i++)
      		if (min > list[i]) {
        	min = list[i];
        	minIndex = i;
         }

    return minIndex;
    }
    public static int maxIndex(int[] list) {
    	int max = list[0];
    	int maxIndex = 0;

    	for (int i = 1; i < list.length; i++)
      		if (max < list[i]) {
        	max = list[i];
        	maxIndex = i;
      		}
     
    return maxIndex;
    }
    public static double meanIndex(int[] list) {
    	int mean = list[0];
    	double meanIndex = 0;

    	for (int i = 1; i < list.length; i++){
    		meanIndex = meanIndex + list[i]; 
    	}
    	meanIndex = meanIndex / 100;
    	
    return meanIndex;
    
    }
    public static double standardDeviationIndex(int[] list) {
    
    	int standardDeviation = list[0];
    	double mean = 0;
    	double meanTwo = 0;
    	double standardDeviationIndex = 0;
    	double sd1 = 0;

    	for (int i = 1; i < list.length; i++){
    		mean = mean + list[i];
    	}
    	meanTwo = mean / 100;
    	
    return standardDeviationIndex;
    }
    
    public static int modeIndex(int[] list) {
    	return modeIndex(list);
    }
    public static int rangeIndex(int[] list) {
    	return rangeIndex(list);
    }
    public static int medianIndex(int[] list) {	
    	return medianIndex(list);
    }
    
    	
  }
















Is This A Good Question/Topic? 0
  • +

Replies To: A little trouble with Arrays

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: A little trouble with Arrays

Posted 12 September 2010 - 10:54 AM

Your error is here:
    public static int modeIndex(int[] list) {
    	return modeIndex(list);
    }
    public static int rangeIndex(int[] list) {
    	return rangeIndex(list);
    }
    public static int medianIndex(int[] list) {	
    	return medianIndex(list);



Each of those methods is calling itself over and over and over and over, never returning anything at all. This is an infinite loop. You need to return something concrete, like an explicit value.
Was This Post Helpful? 0
  • +
  • -

#3 LumberJack10  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 27-February 10

Re: A little trouble with Arrays

Posted 12 September 2010 - 11:05 AM

View PostDogstopper, on 12 September 2010 - 09:54 AM, said:

Your error is here:
    public static int modeIndex(int[] list) {
    	return modeIndex(list);
    }
    public static int rangeIndex(int[] list) {
    	return rangeIndex(list);
    }
    public static int medianIndex(int[] list) {	
    	return medianIndex(list);



Each of those methods is calling itself over and over and over and over, never returning anything at all. This is an infinite loop. You need to return something concrete, like an explicit value.


Can you give a brief example??
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: A little trouble with Arrays

Posted 12 September 2010 - 11:14 AM

Yes, right now, when you say int mode = modeIndex(list), it's doing this:

int mode = modeIndex(list)
 = modeIndex(list)
  = modeIndex(list)
   = modeIndex(list)
    = modeIndex(list)
     = modeIndex(list)
      = modeIndex(list)
      ...

because it's never RETURNING anything. It's simply calling again EXPECTING something to return. Fix it like this:

public static int modeIndex(int[] list) {
	int mode = /* find mode and add it here */;
	return mode;
}
...


Was This Post Helpful? 1
  • +
  • -

#5 LumberJack10  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 27-February 10

Re: A little trouble with Arrays

Posted 12 September 2010 - 11:37 AM

Is it possible that I can get rid of this segment of code?
public static int modeIndex(int[] list) {
    	return modeIndex();
    }
    public static int rangeIndex(int[] list) {
    	return rangeIndex();
    }
    public static int medianIndex(int[] list) {	
    	return medianIndex();
    }





Since I already have return statements in the beginning of the program?

This post has been edited by LumberJack10: 12 September 2010 - 11:40 AM

Was This Post Helpful? 0
  • +
  • -

#6 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: A little trouble with Arrays

Posted 12 September 2010 - 12:17 PM

View PostLumberJack10, on 12 September 2010 - 10:37 AM, said:

Is it possible that I can get rid of this segment of code?
public static int modeIndex(int[] list) {
    	return modeIndex();
    }
    public static int rangeIndex(int[] list) {
    	return rangeIndex();
    }
    public static int medianIndex(int[] list) {	
    	return medianIndex();
    }





Since I already have return statements in the beginning of the program?

Doing that just going to give you an error since the modeIndex method accept a parameter and you would be calling it without any parameter:
return modeIndex();



If what you want is get the position in the list of some value then you should do something like this:
   public static int modeIndex(int[] list, int modeToSearch) {

          int index =0;
          for (index = 0; index < list.length; index++) {
			if(list[index] == modeToSearch){
				break;
			}
		  }
          
          if(index==list.length-1){
        	  index = -1;
          }
          
          return index;
     }


This post has been edited by guidojavier: 12 September 2010 - 12:18 PM

Was This Post Helpful? 0
  • +
  • -

#7 LumberJack10  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 27-February 10

Re: A little trouble with Arrays

Posted 12 September 2010 - 12:40 PM

My methods are not calculating anything and I am having some trouble.
import java.io.*;
import java.util.*;







public class prog1_1 {
    
    public static void main(String[] args) {
    	int[] list = {43, 98, 82, 57, 78, 63, 88, 26, 91, 76, 75, 67, 49, 77, 83, 72, 69, 84, 65,
    				  73, 78, 89, 46, 76, 66, 87, 22, 45, 85, 92, 88, 55, 89, 77, 78, 67, 99, 63, 68, 71, 79, 82, 68, 70};
    
    	System.out.println("The lowest grade is " + minIndex(list));
    	System.out.println("The highest grade is " + maxIndex(list));
    	System.out.println("The mean is " + meanIndex(list));
    	System.out.println("The standard deviation is " + standardDeviationIndex(list));
    	System.out.println("The mode is " + modeIndex(list));
    	System.out.println("The range is " + rangeIndex(list));
    	System.out.println("The median is " + medianIndex(list));
    }
    	
    public static int minIndex(int[] list) {
    	int min = list[0];
    	int minIndex = 0;

    	for (int i = 1; i < list.length; i++)
      		if (min > list[i]) {
        	min = list[i];
        	minIndex = i;
         }

    return minIndex;
    }
    public static int maxIndex(int[] list) {
    	int max = list[0];
    	int maxIndex = 0;

    	for (int i = 1; i < list.length; i++)
      		if (max < list[i]) {
        	max = list[i];
        	maxIndex = i;
      		}
     
    return maxIndex;
    }
    public static double meanIndex(int[] list) {
    	int mean = list[0];
    	double meanIndex = 0;

    	for (int i = 1; i < list.length; i++){
    		meanIndex = meanIndex + list[i]; 
    	}
    	meanIndex = meanIndex / 100;
    	
    return meanIndex;
    
    }
    public static double standardDeviationIndex(int[] list) {
    
    	int standardDeviation = list[0];
    	double mean = 0;
    	double meanTwo = 0;
    	double standardDeviationIndex = 0;
    	double sd1 = 0;

    	for (int i = 1; i < list.length; i++){
    		mean = mean + list[i];
    	}
    	meanTwo = mean / 100;
    	
    return standardDeviationIndex;
    }
    
    public static int modeIndex(int[] list) {  
    int mode = list[i];  
  	return mode;  

    }
     public static int rangeIndex(int[] list) {
    	int range = list[i];
    	return range;
    }
    public static int medianIndex(int[] list) {	
    	int median = list[i];
    	return median;
    }
    
    	
  }





Was This Post Helpful? 0
  • +
  • -

#8 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: A little trouble with Arrays

Posted 12 September 2010 - 12:59 PM

What is suppose to do the modeIndex method ?. You are using an i variable which has not been declared in anywhere.
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: A little trouble with Arrays

Posted 12 September 2010 - 01:11 PM

Also, in the future, please specifically describe your errors. What is/isn't happening? What do you expect/want to happen? What don't you understand? Answering these questions will better help us help you.
Was This Post Helpful? 0
  • +
  • -

#10 LumberJack10  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 27-February 10

Re: A little trouble with Arrays

Posted 12 September 2010 - 01:12 PM

View Postguidojavier, on 12 September 2010 - 11:59 AM, said:

What is suppose to do the modeIndex method ?. You are using an i variable which has not been declared in anywhere.



the modeIndex method calculates the mode for the array
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: A little trouble with Arrays

Posted 12 September 2010 - 01:13 PM

Perhaps it is better to sort the array (using Arrays.sort() possibly). Then just iterate through the array, and the largest quantity of consecutive values is your mode.
Was This Post Helpful? 0
  • +
  • -

#12 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: A little trouble with Arrays

Posted 12 September 2010 - 01:16 PM

I don't know what to say, I've explained this the best I could. You are returning a METHOD, not a VALUE. If you return a method which returns a method which returns a method...you will have issues unless SOMEWHERE along the line, you return a value. A value is a datatype (in this case, you NEED to return an int), like you did with the first couple of methods. If you truly wrote the first part of this code, you should understand what I'm talking about, at least a little. Is this your code?
Was This Post Helpful? 0
  • +
  • -

#13 LumberJack10  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 27-February 10

Re: A little trouble with Arrays

Posted 12 September 2010 - 01:42 PM

of course it is...Im just trying to understand things alot more in depth.
Was This Post Helpful? 0
  • +
  • -

#14 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: A little trouble with Arrays

Posted 12 September 2010 - 09:15 PM

Topics merged.

Please don't create duplicate topics as it makes it difficult for our members to best help you.
Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: A little trouble with Arrays

Posted 13 September 2010 - 05:20 PM

I'll give it a shot explaining.

In the background, there is something called the Java call Stack. As all Stacks work, data is ordered last-in-first-out. Each operation done is pushed onto the Stack.

So when your first call to the method is made, it is pushed onto the Stack. However, when it invokes itself, another call to that method is pushed onto the Stack. This keeps happening until you reach something called a base case. The base case is used to say "ok, this is the last call to the method." That base case usually returns a value, or performs an operation necessary for the previous calls. The top method call is now popped/removed from the call Stack, trickling down allowing the other method calls to finish performing, and be popped from the call Stack. If you do not have such a base case, then calls will be pushed onto the Stack until space runs out. When this happens, it is called a StackOverflowError.

A simple factorial example to illustrate:
public int factorial(int n){
 
   //our base case
   //note how we don't make
   //any more recursive calls
   if(n <= 1) return 1;

   //now we make our recursive call to 
   //factorial(), evaluating at n-1
   //until n <= 1, then it returns 1
   return n*factorial(n-1);
}



So if we evaluate factorial(4), that will return:
4*factorial(3)
factorial(3) returns 3*factorial(2)
factorial(2) returns 2*factorial(1)
factorial(1) returns 1.

So going back:
2*1=2
3*2=6
6*4=24

So, factorial(4) == 24.

Does this help clarify recursion?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2