Go Back   Sports Car Forum - MotorWorld.net > Hobbies and Leisure Time > Computers, Consoles, Gadgets And Gizmos



Reply
 
Thread Tools Display Modes
Old 10-09-2004, 08:01 AM   #1
dingo
Regular User
 
Join Date: Jan 2004
Location: Perth, Australia
Posts: 6,395
Default Java programming question

I am writing a program using arrays and am having a bit of trouble.
The task is to accept integer inputs from the user and put them in an array.....but that part is not the prob.
Then I need to count the occurences of each integer and print them to the screen....so for example if the user enters 1, 3, 44, 42, 21, 21, 3, 8 then it would print out:

1 1
3 2
8 1
21 2
42 1
44 1

I can't figure out how to count the number of times each int has been entered...can anybody give me any ideas?
__________________
dingo is offline   Reply With Quote
Old 10-09-2004, 08:56 AM   #2
stracing
Regular User
 
Join Date: Jun 2003
Location: Sydney, Australia
Posts: 1,901
Default

int input[100]; /initialise arrays/
int freq[];
int count;

..... /get user input, store in array. you've done this bit/

for (count = 0; count <= "input array size(find the array size)" -1; answer++)
++freq[input[count]];

.../then print out your arrays/
__________________
Two rules for life:
1. Don't tell people everything you know.
2.
stracing is offline   Reply With Quote
Old 10-10-2004, 07:42 AM   #3
dingo
Regular User
 
Join Date: Jan 2004
Location: Perth, Australia
Posts: 6,395
Default

Thanks so much stracing, I appreciate the help.
I gave that a go and it seems to work sort of, but not auite right.
I tried it with an array size of 6, and when I enter six 1's...then I would expect it to print out:

1 6

but instead it prints out 0 6 0 0 0 0...so something is a little off...but not sure what.
Also I changed 'answer++' in the for loop to count++, as I assume thats what you meant.
__________________
dingo is offline   Reply With Quote
Old 10-10-2004, 10:47 AM   #4
graywolf624
Regular User
 
Join Date: Oct 2003
Location: Hellaware USA
Posts: 3,865
Default

One suggestion would be a hash table.
IF your not familiar with that then:

/loop through and add user values to your array
/create a 2 dimensional array of the same size. 0 out the whole thing.
/loop till array.getsize()-1 with a for loop.
/get the first value in the array.
/loop through and count and remove from the array all occurances of that value.
/place the value you were searching for in the first dimension in that 2d array. place the count in the second.

/loop through 2 d array finding the values in whatever order you wish.. and print both the occurance and value out.



If you want I can right this in java as well, but Id prefer you actually do some of the work.
graywolf624 is offline   Reply With Quote
Old 10-10-2004, 10:50 AM   #5
dingo
Regular User
 
Join Date: Jan 2004
Location: Perth, Australia
Posts: 6,395
Default

Thanks for the suggestion graywolf, but it needs to be implemented using only a single (preferably) array...and no hash tables since I haven't learnt about them yet.
__________________
dingo is offline   Reply With Quote
Old 10-10-2004, 10:56 AM   #6
graywolf624
Regular User
 
Join Date: Oct 2003
Location: Hellaware USA
Posts: 3,865
Default

ok fair enough..

/create two 1 d arrays
/loop through add all values of array.

/loop till array.getsize()-1 with a for loop.
/get the first value in the array.
/loop through and count and remove from the array all occurances of that value except the first one.
/place the count in the second. next to the first existance of the value you were searching for.
/loop through and where value in the second array doesnt =0 and value in first array is in order you want.. print them both out.
graywolf624 is offline   Reply With Quote
Old 10-10-2004, 11:04 AM   #7
DeMoN
Regular User
 
Join Date: Jun 2003
Location: Florida
Posts: 5,106
Default

Originally Posted by stracing
int input[100]; /initialise arrays/
int freq[];
int count;

..... /get user input, store in array. you've done this bit/

for (count = 0; count <= "input array size(find the array size)" -1; answer++)
++freq[input[count]];

.../then print out your arrays/
i would do it...

int input[100]; /initialise arrays/
int count;

then do a for loop to add the numbers to the array input, each time you add a number to the array do a count++

once the adding is complete, count should have a value equal to the numbers inputed to the array

now:

int freq[count] = 0; // will make a new array with the size = to count and make them all 0

for (int i=0 ; i < count ; i++) {
for (int j=0 ; j < count ; j++) {
if (input[i] == input[j]) freq[i]++;
}
}

then print array input and arry freq with another loop

for (int i = 0 ; i < count ; i++) {
System.out.println(input[i] + " " + input[j]);
}
__________________
Guess who's Back!
DeMoN is offline   Reply With Quote
Old 10-10-2004, 11:09 AM   #8
stracing
Regular User
 
Join Date: Jun 2003
Location: Sydney, Australia
Posts: 1,901
Default

with my code you needed to get rid of the zeroes. then find a way to match your two arrays. so the print out is kinda right. remember arrays begin at "zero" not "one". thats why the 6 is the second element because you wanted to print the amount of "ones".

sorry i couldn't get it to fully work for you. to be honest i haven't done java programming for few years now. i'm just rehashing what i've remembered

but graywlfs second suggestion was what i was getting at
__________________
Two rules for life:
1. Don't tell people everything you know.
2.
stracing is offline   Reply With Quote
Old 10-10-2004, 11:15 AM   #9
stracing
Regular User
 
Join Date: Jun 2003
Location: Sydney, Australia
Posts: 1,901
Default

i wish i had the net during my days of programming. i could just ask for solutions off ppl
__________________
Two rules for life:
1. Don't tell people everything you know.
2.
stracing is offline   Reply With Quote
Old 10-10-2004, 03:11 PM   #10
AlienDB7
Regular User
 
Join Date: Jun 2003
Location: Canada
Posts: 1,914
Default

DeMoN, I haven't test out your code but I think your code will get duplicate results. For example, if the user enters 1, 3, 4, 1 then the output will show the count for "1" twice. Not sure if you're familiar with 2 dimensional array but you can have a 2D array storing the # and the count. One simple and dirty solution would be to integrate the user input and counting together

1. Ask for user input
2. Search for the user input # in the array
3. If found then increment the count, else insert # to end of array with count = 1
4. Go to step 2

The code should be pretty simple, you don't even need multiple arrays. Good luck.
AlienDB7 is offline   Reply With Quote
Old 10-10-2004, 04:51 PM   #11
graywolf624
Regular User
 
Join Date: Oct 2003
Location: Hellaware USA
Posts: 3,865
Default

Not sure if you're familiar with 2 dimensional array but you can have a 2D array storing the # and the count
He already shot down my solution on that one... see above.
graywolf624 is offline   Reply With Quote
Old 10-10-2004, 09:54 PM   #12
dingo
Regular User
 
Join Date: Jan 2004
Location: Perth, Australia
Posts: 6,395
Default

Thanks for all the help guys, I will try out the new solutions later tonight.
__________________
dingo is offline   Reply With Quote
Old 10-11-2004, 12:54 AM   #13
DeMoN
Regular User
 
Join Date: Jun 2003
Location: Florida
Posts: 5,106
Default

int temp; // holds the number typed by user
int input[100] = -1; // assuming -1 is not valid input
int freq[100] = 0;
int count;
bool add; // if the number isnt found, it adds it

now you do a for loop to test if the value already exists
while ( (temp != null) && (temp >= 0) ) {
add = true; // always set tu ture and if it stays this way, we add
for (int i = count ; i >= 0 ; i--) {
if (temp == input[i]) { freq[i]++; add = false;}
}
if (add) { input[i] = temp; count++;}
}

ok this one should work. hope you understand it. Now i go sleep.
__________________
Guess who's Back!
DeMoN is offline   Reply With Quote
Old 10-11-2004, 01:47 AM   #14
DeMoN
Regular User
 
Join Date: Jun 2003
Location: Florida
Posts: 5,106
Default

this is doing the right stuff, but you have to play a little bit more with it


import java.io.*;

public class Lab04Test1 {
private static BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) );

public static void main ( String [] args ) throws IOException {
int SIZE = 100;
String input;
int[] intput = new int[SIZE];
int[] freq = new int[SIZE];
int count=0;
int temp;
boolean add;

for (int i=0 ; i<SIZE ; i++) {
intput[i] = -1;
freq[i] = 0;
}

System.out.println( "Type some data for the program: " );
while (true) {
input = stdin.readLine();
temp = Integer.parseInt(input);
if (temp == -1) {
add = false;
break;
}

add = true; // always set tu ture and if it stays this way, we add
int i;
for (i = count-1 ; i >= 0 ; i--) {
if (temp == intput[i]) {
freq[count-1]++;
add = false;
break;
}
}
if (add) {
intput[count] = temp;
freq[count]++;
count++;
}
}
System.out.println("End");
for (int i=0 ; i<count ; i++) {
System.out.println(intput[i] + " " + freq[i]);
}
}
}
__________________
Guess who's Back!
DeMoN is offline   Reply With Quote
Old 10-11-2004, 10:29 AM   #15
dingo
Regular User
 
Join Date: Jan 2004
Location: Perth, Australia
Posts: 6,395
Default

Thanks DeMon, and all the other guys. I thought you said you were going to sleep, but an hour later you posted again.
I haven't found the time to try them all out, but will let you know how I go.
__________________
dingo is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump