d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Help With Java Please First Attempt At Arrays
Add Reply New Topic New Poll
Member
Posts: 702
Joined: May 8 2011
Gold: 3.95
Oct 4 2012 04:41pm
Creates an array of integers of size 100 and fills the array with random integers from 0 through 9

Creates a second integer array of size 10 and counts the occurrences of values in the first array into the corresponding position in the second array (and print out neatly to verify).

Finds the least frequent value in the large array (and print out) by finding the smallest value in the second array (with a sequential search).

Displays (print out neatly) the locations (indices) in the large array of the least frequent value by finding the result from step three in the large array.

basically i'm trying to get random numbers 0-9 then print in rows/columns
so i can read them easily with spacing of course at the end i want it to count up the numbers print least so 7 was least at 15, 38, 92, 98

i'm not really sure about this anyway. any help would be very appreciated.

code

Code
public class Program7
{
   public static void main(String[] args)
   {

   creatRandom();

   int[] randomArray = new int [100];

   for (randomArray [i] = (int) (math.random() * 10);

   printrandom();

   for ( int i = 0; i < randomArray.length; i++)
   {
       if (i+1 % 10 == 0)
           println(randomArray [i]);
       else
           print(randomArray[i]);
   }

   print int [] counts = new int [10];

       //0-99
       for (big array loop);

       counts[randomArray [i]] ++;

       //row and col 0-9

       for (row = 0);
           (col = 0);

       Array[row * 10 + col];

   }

}


errors as of now

Code
Program7.java:20: error: illegal start of expression
 for ( int i = 0; i < randomArray.length; i++)
 ^
Program7.java:20: error: ')' expected
 for ( int i = 0; i < randomArray.length; i++)
 ^
Program7.java:20: error: > expected
 for ( int i = 0; i < randomArray.length; i++)
 ^
Program7.java:20: error: not a statement
 for ( int i = 0; i < randomArray.length; i++)
 ^
Program7.java:20: error: ';' expected
 for ( int i = 0; i < randomArray.length; i++)
   ^
Program7.java:28: error: not a statement
 print int [] counts = new int [10];
 ^
Program7.java:28: error: ';' expected
 print int [] counts = new int [10];
 ^
Program7.java:31: error: ';' expected
   for (big array loop);
   ^
Program7.java:31: error: ';' expected
   for (big array loop);
 ^
Program7.java:37: error: ';' expected
   for (row = 0);
   ^
Program7.java:38: error: not a statement
   (col = 0);
   ^
Program7.java:38: error: ')' expected
   (col = 0);
 ^
Program7.java:40: error: not a statement
   Array[row * 10 + col];
 ^
13 errors
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 4 2012 04:49pm
for (randomArray [i] = (int) (math.random() * 10);

thats the first thing you gotta fix. convert that to a real "for" loop

eg

for (int i = 0; i < ??; i++){

}
Member
Posts: 702
Joined: May 8 2011
Gold: 3.95
Oct 4 2012 05:37pm
Quote (carteblanche @ Oct 4 2012 03:49pm)
for (randomArray [i] = (int) (math.random() * 10);

thats the first thing you gotta fix. convert that to a real "for" loop

eg

for (int i = 0; i < ??; i++){

}

so like this

for (int i = 0; i < randomArray; i++;)
{

}

which leaves me with 10 errors :)

Code
Program7.java:16: error: ')' expected
       for (int i = 0; i < randomArray; i++;)
                                           ^
Program7.java:16: error: illegal start of expression
       for (int i = 0; i < randomArray; i++;)
                                            ^
Program7.java:30: error: not a statement
       print int [] counts = new int [10];
       ^
Program7.java:30: error: ';' expected
       print int [] counts = new int [10];
            ^
Program7.java:33: error: ';' expected
               for (big array loop);
                             ^
Program7.java:33: error: ';' expected
               for (big array loop);
                                  ^
Program7.java:39: error: ';' expected
               for (row = 0);
                           ^
Program7.java:40: error: not a statement
                       (col = 0);
                       ^
Program7.java:40: error: ')' expected
                       (col = 0);
                                ^
Program7.java:42: error: not a statement
               Array[row * 10 + col];
                    ^
10 errors


This post was edited by trio0 on Oct 4 2012 05:39pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 4 2012 05:46pm
the compiler is very helpful and points out many mistakes with a brief explanation. Go look them up. read your text book if you're having problems with syntax. i'm sure it has plenty of examples of how to write if statements, loops, assignments, etc. Gooo. you seem to be making no effort to do this.

i recommend scrapping all the code you wrote, then rewrite it line by line. as soon as you get a compile error, FIX IT.

This post was edited by carteblanche on Oct 4 2012 05:47pm
Member
Posts: 702
Joined: May 8 2011
Gold: 3.95
Oct 4 2012 06:16pm
um i couldn't get my compiler to work so i do it through cmd

but i tried again after looking at some stuff

do you know how to have it look up which showed up the least

so if the output was

7 8 5 4 6 8 3 9 5 8
8 5 4 6 8 3 9 5 1 1

it would count it and say 7 was the least at 1

say it comes up 2 times and is still the least it says 7 was the least at 1, 10 etc.

Code
import java.util.Random;


public class Program7 {
public static void main(String[] args) {
 Random randomGenerator = new Random();
 short table[][] = new short[10][10];
 for(int x = 0; x < table.length; x++) {
  for(int y = 0; y < table[x].length; y++) {
   table[x][y] = (short) randomGenerator.nextInt(9);
  }
 }
 
 for(short[] x:table) {
  for(short y:x) {
   System.out.format("%d ", y);
  }
  System.out.print("\n");
 }
}
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 4 2012 06:33pm
Quote (trio0 @ Oct 4 2012 08:16pm)
um i couldn't get my compiler to work so i do it through cmd

That means you're using the compiler. i assume you mean you dont know how to set up your IDE?

Quote
do you know how to have it look up which showed up the least

so if the output was

7 8 5 4 6 8 3 9 5 8
8 5 4 6 8 3 9 5 1 1

it would count it and say 7 was the least at 1

say it comes up 2 times and is still the least it says 7 was the least at 1, 10 etc.


how about i tell you the algorithm then you write the code?

1) use some data structure to associate the count of each occurance (eg: number 7 one time)
2) go through your list. if this number does not appear in #1, then add it to your data structure with value 1 (eg: 8 -> 1 since this is your first pass)
if this number does appear in #1, then increment the counter (eg: 8 - > 1 already exists, so replace it with 8 -> 2 the second time you see it)
3) go through the (now-filled) data structure you created. whichever one has the lowest frequency is your magic number
Member
Posts: 702
Joined: May 8 2011
Gold: 3.95
Oct 4 2012 06:46pm
yep IDE forgot what it was called ok i'll get to it thanks :)
Member
Posts: 528
Joined: Jul 5 2011
Gold: Locked
Trader: Scammer
Oct 8 2012 11:18am
Hey didn't I tell you how to do this on dreamincode ? =p
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll