output an array

Output an array using for loops

Page 1 of 1

1 Replies - 2119 Views - Last Post: 17 November 2009 - 05:41 PM Rate Topic: -----

#1 maximus6619  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-November 09

output an array

Post icon  Posted 17 November 2009 - 05:00 PM

I need to output a histogram from an array of random generated numbers. I need to call methods from main to execute the program. I am really struggling with getting the last two methods to work properly, especially ouputting the array. each row(println) need to show the index number of the row followed by asteriks equaling the number of random generated numbers in the bin, followed by the actual number of numbers in the bin. This is really confusing me. Any ideas? I am not sure if I need another loop or more execution in the second loop. here is my code so far. Simple method and array is the peak of what I know.


public static void main(String[] args) 
	{
		Scanner in = new Scanner (System.in); //Declare Scanner
		
		// Prompt user for input
		System.out.print("Would you like to create a histogram? ");
		String create = in.nextLine();
		
		// Create condition for execution or termination of program
		while((create.charAt(0) == 'y' || create.charAt(0) == 'Y'))
		{	
			int numOfNum = howManyNum(in);
			int numOfBin = howManyBins(in);
			double[] gen = Generator.getData(numOfNum);
			double max = maxOfGen(gen);
			double min = minOfGen(gen);
			double range = rangeMinToMax(max, min);
			double width = widthOfBin(range,numOfBin);
			double [] inEachBin = numOfNumInEachBin(gen, width, min, numOfBin);
			outputHistogram(inEachBin);
			
			// Prompt user for input based execution
			System.out.print("Would you like to create another histogram series? ");
			create = in.nextLine();
		}
		// exit program message
		System.out.println("\nHave a nice day!");

	}
	private static int howManyNum(Scanner in)
	{
		System.out.print("How many numbers do you want to generate? ");			//len
		int num = in.nextInt();
		
		return num;
	}
	private static int howManyBins(Scanner in)
	{
		System.out.print("How many intervals do you want? ");
		int bin = in.nextInt();
		
		return bin;
	}
	
	private static double maxOfGen(double[] gen)
	{
		double max = gen[0];
		for(int index = 0; index<gen.length; index++)
			if(gen[index] > max)
			{
				max = gen[index];
			}
		
		return max;
	}
	private static double minOfGen(double[] gen)
	{
		double min = gen[0];
		for(int index = 0; index < gen.length; index++)
			if(gen[index] < min)
			{
				min = gen[index];
			}
		return min;
	}
	private static double rangeMinToMax(double mn, double mx)
	{
		double max = mx;
		double min = mn;
		double range = max - min;
		
		return range;
	}
	private static double widthOfBin(double range, int numB)
	{
		double width = range/numB;
		return width;
	}
	private static double[] numOfNumInEachBin(double[] g, double wid, double mn, int nOBin )
	{
		double[]nOfN = new double[(int) wid];
		for(int i = 0; i < nOfN.length; i++ )
		{
			for(int index = 0; index < g.length; index++)
			{
				double max = mn + wid;
				if(g[index]< max)
				{
					nOfN[i]++;
				}
				wid = wid*2;
			}
			
		}
		
		return nOfN;
		
	}
	private static void outputHistogram(double nOfN[])
	{
		int index = 1;
		for(int i = 0; i < nOfN.length; i++)
		{
			for(int in = 0; in < nOfN[index];in++)
			{
				
				System.out.print(nOfN[index]);
			}
			
		System.out.println(index);
		}
		index++;
	}

}




Is This A Good Question/Topic? 0
  • +

Replies To: output an array

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: output an array

Posted 17 November 2009 - 05:41 PM

to print the index, then the number of asterisks in line, then the random number itself:
suppose the aray is int[] array.
for(int i=0;i<array.length;i++){
System.out.print(i + ") ");//prints index
for(int j=0j<array[i];j++){
System.out.print("*");//prints asterisks the number of the value
}
System.out.println(" " + array[i]);//prints the value
}


Was This Post Helpful? 1

Page 1 of 1