Adding a specific range of numbers to an array

I have been looking and looking at my code and I still can't figure out why the result is always the highestGrade value and not a different value for every position in the array.

Here is my code:

int[] gradesDescription;
int[] gradesCount;

gradesDescription = new int[(highestGrade-lowestGrade) + 1];
gradesCount = new int[(highestGrade-lowestGrade) + 1];

for(int b = lowestGrade; b <= highestGrade; b++){
  Arrays.fill(gradesDescription, b);
}

for(int d = 0; d < gradesDescription.length; d++){
 System.out.println("Grade: " + gradesDescription[d] + 
                    " had " + gradesCount[d] + " students with the same grade.");

What is the logic that I am missing; is there a better way on how to accomplish what I am trying to do?

Thanks so much!

Answers


This line is causing your problem:

Arrays.fill(gradesDescription, b);

This will assign EVERY value in gradesDescription to b. What you want instead is something like:

for(int b = 0; b < gradesDescription.length; b++) {
    gradesDescription[b] = b + lowestGrade;
}

Although, I have to say even this code looks wrong. What is the expected behavior if there are three students with grades of 70, 80, and 100? gradesDescription.length will end up being 30, but really it should only be 3? I'm assuming you left out the code where the elements of gradesCount are assigned?


for(int b = lowestGrade; b <= highestGrade; b++){
     Arrays.fill(gradesDescription, b);
}

This line will place the value in b at every position in your gradesDescription array. Hence the same value every time.


Need Your Help

Symbolication of Crash Log fails to Symbolicate any Framework Libraries

iphone ios crash-reports symbolicate symbolicatecrash

I'm trying to include some crash reports with a TSI incident, however I can only get the logs to be partially symbolicated. I have tried using the Organizer panel in Xcode 4.2 and using the Perl sc...

C++ creating sorted vector from map

c++ sorting vector map

I'm trying to create a sorted vector from a map, sorted according to a value that isn't the map's key.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.