Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

not counting right

Posted on 2005-02-24
2
Medium Priority
?
234 Views
Last Modified: 2010-04-17
public class counting
{
    public static void main(String args[])
    {
        int responses[] = {10,3,1,2,6,4,8,5,9,9,7,8,6,8,6,3,10,3};
        int frequency[]= new int[11];
       
        // for each answer, select responses element and use that value
        // as frequency index to determine element to increment
       
        for(int answer = 0; answer < frequency.length; answer++)
        ++frequency[responses[answer]];
       
        System.out.print("Rating\tFrequency\n");
       
        // append frequencies to String system.out
        for(int rating = 1; rating < frequency.length; rating++)
        System.out.print(rating + "\t" + frequency[rating] + "\n");

    }
}

why is this not counting right?
0
Comment
Question by:m-jansen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
guitaristx earned 80 total points
ID: 13395835
What should the correct output be?  I'm guessing:
1     1
2     1
3     3
4     1
5     1
6     3
7     1
8     3
9     2
10     2

(pardon the formatting)

If so, you'll want to (first) initialize your array members to zero.  This might be throwing it off.

Also, your actual "doing stuff" loop should look like this:

for(int i = 0; i < responses.length; i++)
     ++frequency[ responses[i] ];

If you look closely, you're not iterating through the entire array of responses.  Good luck!
0
 

Author Comment

by:m-jansen
ID: 13396048
I'm getting this out put when running posted 10:54AM class.

Rating      Frequency
1      1
2      1
3      1
4      1
5      1
6      1
7      1
8      1
9      2
10      1

I didn't understand how the loops worked so I was blinded.
Now I see it better.

And I'm getting output:

Rating      Frequency
1      1
2      1
3      3
4      1
5      1
6      3
7      1
8      3
9      2
10      2

thanx
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Make the most of your online learning experience.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Simple Linear Regression

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question