6 Replies - 1007 Views - Last Post: 04 March 2009 - 08:30 AM Rate Topic: -----

#1 billy81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 24-February 09

a random array

Post icon  Posted 04 March 2009 - 07:51 AM

Hi all.This is a question i got from college.I'll post my code so-far after the question.


Information:

The idea behind the program is to fill a randomly generated character array with randomly generated upper and lower case characters. The generated array is of random size between 1 and 10000. Then count how many occurrences of each separate character there is. The program should ask the user if they would like to create another array (i.e. program loop).

The program should output the following:

1. The size of the generated array.
2. The actual array.
3. The total count of each character.


Ok thats it.
I think i am very close with my code but i keeps returning errors with the
b[i] and the "char y[] = new char(size)".when i initialize the "i" it gives me an error on "int= i;" Here's what i have...

package randomarray;

public class randomArray 
{

	/**
	 * @param args
	 */
	public static void fillIntArray(int b[])
	{
		int = i;
		for (int i = 0; i < b.length;i++ );
		{
			b[i] = (int)(Math.random()* 10000);
		}
	}
	public static void fillcharArray(char b[])
	{
		int = i;
		for (int i = 0; i < b.length;i++ );
		{
			b[i] = (char)(Math.random()* 10000);
		}
			if (Math.random() < 0.5);
			{
				b[i] = (char)((int)(Math.random()*26)+65);
		}
	}
		
	
	public static void main(String[] args)
	{
		int size = (int)(Math.random()*10000);
		char y[] = new char(size);
		fillCharArray (y);
		bubblesort (y);
		System.out.println("The size of the array is:"+y.length);
		printArray (y);
		
		


		char c = (char)((int)(Math.random()*26)+65);
		{
		System.out.println("The random Uppercase is:" +c);
		}



		char d = (char)((int)(Math.random()*26)+97);
		{
		System.out.println("The random Lowercase is:" +d);
		}
	}
}



thanks in advance for the help :)

Is This A Good Question/Topic? 0
  • +

Replies To: a random array

#2 Lucrosen  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 21
  • Joined: 11-February 09

Re: a random array

Posted 04 March 2009 - 08:07 AM

You need to remove the semi colon in your for loop.
for (int i = 0; i < b.length;i++ );
		

it should be
for(int i = 0; i < b.length; i++)

the semi-colon is screwing up your loop
Was This Post Helpful? 0
  • +
  • -

#3 billy81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 24-February 09

Re: a random array

Posted 04 March 2009 - 08:14 AM

View PostLucrosen, on 4 Mar, 2009 - 07:07 AM, said:

You need to remove the semi colon in your for loop.
for (int i = 0; i < b.length;i++ );
		

it should be
for(int i = 0; i < b.length; i++)

the semi-colon is screwing up your loop

Thank you. It is still giving me an error where"b [i] = (char)((int)(Math.random........
I'm still getting an error in the "main method" too, at char y[] = new char (size)....
any ideas on this??
Was This Post Helpful? 0
  • +
  • -

#4 Lucrosen  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 21
  • Joined: 11-February 09

Re: a random array

Posted 04 March 2009 - 08:16 AM

I went through your code again. and found more rogue semi-colons.
public class RandomArray 
{

	/**
	 * @param args
	 */
	public static void fillIntArray(int b[])
	{
		for (int i = 0; i < b.length;i++ )
		{
			b[i] = (int)(Math.random()* 10000);
		}
	}
	public static void fillCharArray(char b[])
	{
		for (int i = 0; i < b.length;i++ )
		{
			b[i] = (char)(Math.random()* 10000);

			if (Math.random() < 0.5)
			{
				 b[i] = (char)((int)(Math.random()*26)+65);
			}
		}
	}
		
	
	public static void main(String[] args)
	{
		int size = (int)(Math.random()*10000);
		char y[] = new char[size];
		fillCharArray (y);
		bubblesort(y); // there is no method defined for this
		System.out.println("The size of the array is:"+y.length);
		printArray(y); // same here there is no method defined to do this function
		
		


		char c = (char)((int)(Math.random()*26)+65);
		{
		System.out.println("The random Uppercase is:" +c);
		}



		char d = (char)((int)(Math.random()*26)+97);
		{
		System.out.println("The random Lowercase is:" +d);
		}
	}
}


this is what i have so far. 2 problems you are going to have are your bubblesort, and your print array. Neither are defined as methods anywhere in your program.
Was This Post Helpful? 0
  • +
  • -

#5 Lucrosen  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 21
  • Joined: 11-February 09

Re: a random array

Posted 04 March 2009 - 08:22 AM

This code will sort the array but i have it print each element of the array. You'll need to figure out how to get the counts of each instance of what is in the array.
import java.util.Arrays;

public class RandomArray 
{

	/**
	 * @param args
	 */
	 
	public static void fillIntArray(int b[])
	{
		for (int i = 0; i < b.length;i++ )
		{
			b[i] = (int)(Math.random()* 10000);
		}
	}
	public static void fillCharArray(char b[])
	{
		for (int i = 0; i < b.length;i++ )
		{
			b[i] = (char)(Math.random()* 10000);

			if (Math.random() < 0.5)
			{
				 b[i] = (char)((int)(Math.random()*26)+65);
			}
			else
				--i;
		}
	}
		
	
	public static void main(String[] args)
	{
		int size = (int)(Math.random()*10000);
		char y[] = new char[size];
		fillCharArray (y);
		Arrays.sort(y);
		System.out.println("The size of the array is: "+y.length);
		//printArray(y); // same here there is no method defined to do this function
		for(int i = 0; i < y.length; i++){
			System.out.println(y[i]);
		}
		


		char c = (char)((int)(Math.random()*26)+65);
		//{
		System.out.println("The random Uppercase is:" +c);
		//}



		char d = (char)((int)(Math.random()*26)+97);
		//{
		System.out.println("The random Lowercase is:" +d);
		//}
	}
}


This post has been edited by Lucrosen: 04 March 2009 - 08:25 AM

Was This Post Helpful? 0
  • +
  • -

#6 billy81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 24-February 09

Re: a random array

Posted 04 March 2009 - 08:29 AM

View PostLucrosen, on 4 Mar, 2009 - 07:16 AM, said:

I went through your code again. and found more rogue semi-colons.
public class RandomArray 
{

	/**
	 * @param args
	 */
	public static void fillIntArray(int b[])
	{
		for (int i = 0; i < b.length;i++ )
		{
			b[i] = (int)(Math.random()* 10000);
		}
	}
	public static void fillCharArray(char b[])
	{
		for (int i = 0; i < b.length;i++ )
		{
			b[i] = (char)(Math.random()* 10000);

			if (Math.random() < 0.5)
			{
				 b[i] = (char)((int)(Math.random()*26)+65);
			}
		}
	}
		
	
	public static void main(String[] args)
	{
		int size = (int)(Math.random()*10000);
		char y[] = new char[size];
		fillCharArray (y);
		bubblesort(y); // there is no method defined for this
		System.out.println("The size of the array is:"+y.length);
		printArray(y); // same here there is no method defined to do this function
		
		


		char c = (char)((int)(Math.random()*26)+65);
		{
		System.out.println("The random Uppercase is:" +c);
		}



		char d = (char)((int)(Math.random()*26)+97);
		{
		System.out.println("The random Lowercase is:" +d);
		}
	}
}


this is what i have so far. 2 problems you are going to have are your bubblesort, and your print array. Neither are defined as methods anywhere in your program.


yeah i've just seen that too. Ihavent looked at this program in a couple of weeks so i kinda forget where i was....anyway i've deleted those "famous" semicolon's of mine (i'm known for them around these parts).
Should i create a new method for bubblesort and print array??


View PostLucrosen, on 4 Mar, 2009 - 07:22 AM, said:

This code will sort the array but i have it print each element of the array. You'll need to figure out how to get the counts of each instance of what is in the array.
import java.util.Arrays;

public class RandomArray 
{

	/**
	 * @param args
	 */
	 
	public static void fillIntArray(int b[])
	{
		for (int i = 0; i < b.length;i++ )
		{
			b[i] = (int)(Math.random()* 10000);
		}
	}
	public static void fillCharArray(char b[])
	{
		for (int i = 0; i < b.length;i++ )
		{
			b[i] = (char)(Math.random()* 10000);

			if (Math.random() < 0.5)
			{
				 b[i] = (char)((int)(Math.random()*26)+65);
			}
			else
				--i;
		}
	}
		
	
	public static void main(String[] args)
	{
		int size = (int)(Math.random()*10000);
		char y[] = new char[size];
		fillCharArray (y);
		Arrays.sort(y);
		System.out.println("The size of the array is: "+y.length);
		//printArray(y); // same here there is no method defined to do this function
		for(int i = 0; i < y.length; i++){
			System.out.println(y[i]);
		}
		


		char c = (char)((int)(Math.random()*26)+65);
		//{
		System.out.println("The random Uppercase is:" +c);
		//}



		char d = (char)((int)(Math.random()*26)+97);
		//{
		System.out.println("The random Lowercase is:" +d);
		//}
	}
}


Thats very helpful Lucrosen.Thanks for your time :).
Was This Post Helpful? 0
  • +
  • -

#7 Lucrosen  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 21
  • Joined: 11-February 09

Re: a random array

Posted 04 March 2009 - 08:30 AM

I wouldn't write a sort method because the Arrays class has a built in sort feature. You could however write a method to print the array. I'm not sure how your supposed to print it, but i think that i would make the method void, you shouldn't neet to return anything within the method b/c it will basically be for printing. I also noticed only uppercase letters are being loaded into the array

This post has been edited by Lucrosen: 04 March 2009 - 08:32 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1