• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with arrays  RSS feed

 
Mido Apilo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm new here and I've a question.
We're now working with arrays in Java.
This is our assignment:

First we need use this:
public static void main(String[] args) {
int[] numbers = {
8, 4, 0, 7, 3, 4, 4, 7, 7, 1,
9, 1, 9, 6, 3, 1, 5, 5, 0, 2,
5, 3, 4, 1, 2, 1, 3, 1, 4, 0,
8, 8, 2, 6, 4, 8, 8, 0, 3, 3,
5, 1, 4, 2, 5, 9, 3, 6, 2, 0
};

Now I need to declare frequences to count how many times a number is in the 'numbers'.

Example: we see number '8' five times in the 'numbers' class.

This is what I already have:


This is the output:
-Frequences:
0-->5
1-->7
2-->....
......etc...

Which array I need to use? Can someone please help me?
Thanks!
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Have you thought about declaring another array to hold the frequencies?
 
Mido Apilo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I haven't think about that:
this is what I now have, but doesn't work well:
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about how you would do this by hand. Write it out step by step. I'll start, but you finish:

1) Go through the array number by number
2) When I see a number...
...

After you have that written out, how would you translate that into code?
 
Mido Apilo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Think about how you would do this by hand. Write it out step by step. I'll start, but you finish:

1) Go through the array number by number
2) When I see a number...
...

After you have that written out, how would you translate that into code?
4

Okay,
When I see a number, I need to look in the whole code how many times this number appears.
And I do this for all the numbers.

I really don't know how to do this....
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to StopCoding (<---that's a link to a page you should read). Never write any java until you understand the problem, have thought through the solution, and have a good, solid idea of what your code is supposed to do.

Knute has given you EXCELLENT advice on what do to and how to THINK about the problem. Writing code is really about 90% thinking, and only 10% typing.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's another hint...

When I see a number (like 4), I don't need to know how many TOTAL 4's there are. I only need to know how many times I've ALREADY SEEN 4, and now I know I have one more.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember, you're doing this for all the numbers. Maybe, "When I see a number, I write that number down and put a check mark next to it."

So you need a place to put all the numbers and their check marks. What would you use in Java to do this?
 
Mido Apilo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, finally I've found the solution!!! And it works

 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congradulations!
 
Mido Apilo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Congradulations!


Was that sarcasm? lol
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not at all.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done

But I challenge you to do the counting without sorting the array. Let us say you are confined to numbers in the range 0…9, so a 10‑element array will hold the counts.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!