# completely lost

• 04-05-2012
jonesk1978
completely lost
Here is my problem:
Write a program that reads in a set of positive integers, representing test scores for a class, and outputs how many times a particular number appears in the list. You may assume that the data has a set at most 100 numbers and that -999 marks the end of the input data. The numbers must be output in increasing order: Example data:
55 80 78 92 95 55 78 53 92 65 78 95 85 92 85 95 95
output should look like:

Test Score Count
53 1
55 2
65 1
78 3
80 1
85 2
92 3
95 4

I started trying and am completely lost first I am trying just to get the data stored and to print out in order, not trying to do any kinda count yet until I can store and sort the data. This is what I have done but I know it isn't even close: I am not completely sure how arrays work just yet:

Code:

```#include <iostream> using namespace std; void testSort(int list[], int length); void fillArray(int list[], int length); void initialize(int list[], int length); int main () {         int test[100];         int i;         int lengthx;         int score = 0;         int length = 100;         cout << "Enter how many test scores will be entered:";         cin >> lengthx;         cout << endl;         initialize(test, length);         cout << "Enter " << lengthx << " test scores: ";             fillArray(test, lengthx);         testSort(test, lengthx);         for ( i = 0; i < length; i++)                 cout << test[i] << " ";                 cout << endl;                 return 0; } void testSort(int list[], int length) {         int temp;         int iteration;         int index;         for (iteration = 0; iteration < length; iteration++)         {                 for (index = 0; index < length - iteration; index++)                         if (list[index] > list[index + 1])                         {                                 temp = list[index];                                 list[index] = list[index+1];                                 list[index + 1] = temp;                         }         } } void fillArray(int list[], int length) {         int index;         for (index = 0; index < length; index++)         cin >> list[index]; } void initialize(int list[], int length) {         int index;         for (index = 0; index < length; index++)                 list[index] = 0; }```
• 04-05-2012
aquatorrent
seems you have trouble in sorting :)
a bubble sort should be like this:
Code:

```    for (iteration = 0; iteration < length; iteration++)     {         for (index = 0; index < length - iteration-1; index++) // i put -1 on it because if you're not doing it, in the last loop, it'll access the 'length' index(which is nothing)             if (list[index] > list[index + 1])             {                 temp = list[index];                 list[index] = list[index+1];                 list[index + 1] = temp;             }     }```
• 04-05-2012
überfuzz
It seems the real problem is that you're in over you head here... Is it possible to alter the solution to something bruter jet functional. Well you know, so that you can get it running, but without and fancy pancy stuff?

quasicode
Code:

```results[maxScore] = 0, declare an array with length maxScore and set all instances to zero. loop until score is negative   result[score]++;```
Now you have an array looking like this.
Code:

`results = {3, 0, 2, 1, etc...}`
3 students scored zero points and so on.
• 04-05-2012
omGeeK
Code:

`int test[100] = {0};`
Is a much simpler way of initializing an array to zero than calling a function like that as long as you want to initialize it to 0.
• 04-05-2012
grumpy
Quote:

Originally Posted by omGeeK
Code:

`int test[100] = {0};`
Is a much simpler way of initializing an array to zero than calling a function like that as long as you want to initialize it to 0.

Sure, but I've seen people who believe that "int test[100] = {4};" initialises all elements of test to the value 4, and get into a arguments that the compiler is at fault when it doesn't.

Much easier to avoid such silly debates entirely by either initialising the whole array explicitly (i.e. 100 values to initialise a 100 element array) or write code to overtly initialise or reinitialise the array.
• 04-05-2012
omGeeK
True, but if you know you want to initialize to zero why not lol
• 04-05-2012
überfuzz
I don't know whether your debate is over my quasi code or not. I was merely trying to point out a possible solution. Cheers!
• 04-06-2012
iMalc
Quote:

Originally Posted by omGeeK
Code:

`int test[100] = {0};`
Is a much simpler way of initializing an array to zero than calling a function like that as long as you want to initialize it to 0.

I've seen a compiler (VS2005 I believe it was) add 4MB to the size of an executable, for a 4MB array I had initialised that way.
Using ZeroMemory instead dropped it down to just a few hundred KB.

I thoroughly recommend it for small arrays such as this, but be careful doing it for something quite large.
• 04-06-2012
Elkvis
Quote:

Originally Posted by iMalc
I've seen a compiler (VS2005 I believe it was) add 4MB to the size of an executable, for a 4MB array I had initialised that way.

is it possible that it was declared in the global scope or in a namespace, not in a function or class?
• 04-06-2012
iMalc
Quote:

Originally Posted by Elkvis
is it possible that it was declared in the global scope or in a namespace, not in a function or class?

I think it was a static class member, but I could be wrong.