
array query
I'm writing this program right now where the user inputs integers with a limit of 100 integers and entering 999 to denote the end of the list. I'm having some trouble... it seems like the way that I have it written now, the program must read 100 integers no matter what. But I don't know what to change. I tried flipping around my "while" and "for" statements, but then that gave me some kind of internet explorer error and shut down. Anyways, if anyone can take a look and see what I have going wrong here, let me know. I'd really appreciate it.
Code:
#include <iostream>
#include <iomanip>
using namespace std;
const int sentinel = 999;
void selectionSort(int number[], int length);
int main()
{
int number[100];
int index;
cout << "Enter a maximum of 100 positive integers ending with " << sentinel << endl;
for (index = 0; index < 100; index++)
{
while (number[index] != sentinel)
{
cin >> number[index];
}
}
selectionSort(number, 100);
for (index = 0; index < 100; index++)
cout << number[index] << endl;
return 0;
}
void selectionSort(int number[], int length)
{
int index;
int smallestIndex;
int minIndex;
int temp;
for (index = 0; index < length  1; index++)
{
smallestIndex = index;
for (minIndex = index + 1; minIndex < length; minIndex++)
if (number[minIndex] < number[smallestIndex])
smallestIndex = minIndex;
temp = number[smallestIndex];
number[smallestIndex] = number[index];
number[index] = temp;
}
}

Try changing (as a start):
Code:
while (number[index] != sentinel)
To:
Code:
if (number[index] != sentinel)
Of course, it makes more sense to stop the loop early when you get a sentinel value:
Code:
for ( index = 0; index < 100; index++ ) {
cin>> number[index];
if ( number[index] == sentinel )
break;
}
Then index will be the number of valid items in the array, or you can use the sentinel for loop termination.

And then change this
Code:
selectionSort(number, 100);
to
Code:
selectionSort(number, index);

OK... I've done some revising. But there's still something wrong right now. If someone could copy my code into their compiler and see what is happening when you execute it maybe you could tell me what I'm still doing wrong. Something else that I'm supposed to be doing in this program is counting how many times numbers appear. I'm pretty sure that I need to use like a binary search or something, but I'm not sure how to write and keep track of it. Help!
Code:
#include <iostream>
#include <iomanip>
using namespace std;
const int sentinel = 999;
void selectionSort(int number[], int length);
int main()
{
int number[100];
int index;
cout << "Enter a maximum of 100 positive integers ending with " << sentinel << endl;
for (index = 0; index < 100; index++)
{
if (number[index] != sentinel)
cin >> number[index];
if (number[index] == sentinel)
break;
}
selectionSort(number, index);
cout << "Number Count";
for (index = 0; index < 100; index++)
cout << number[index] << endl;
return 0;
}
void selectionSort(int number[], int length)
{
int index;
int smallestIndex;
int minIndex;
int temp;
for (index = 0; index < length  1; index++)
{
smallestIndex = index;
for (minIndex = index + 1; minIndex < length; minIndex++)
if (number[minIndex] < number[smallestIndex])
smallestIndex = minIndex;
temp = number[smallestIndex];
number[smallestIndex] = number[index];
number[index] = temp;
}
}

Code:
for (index = 0; index < 100; index++)
{
if (number[index] != sentinel)
cin >> number[index];
if (number[index] == sentinel)
break;
}
The logic here could be cleaned up a bit, though what you have is valid.
When that section is done, index equals the number of ints put into number. Index will be valid with values of 0 to 100; inclusive.
Then, in the second for loop you only want to loop as many times as the actual number of elements in the array, not the potential maximal number of elements in the array. So change the variable int the loop to something like i or actual or whatever, and terminate when i == index, not when index == 100, say:
for(int i = 0; i < index; ++i)

Oh, duh! Thanks for pointing that out. So now, as my program stands, whatever integers I input, they are sorted and output in ascending order. But I don't want the numbers that are repeated to be output multiple times. I need to be able to read the multiple numbers and put the count of how many of each number was input in the output. Does that make sense?

You could make an array of 101 ints and initialize each element to zero. Then read through the original array using a loop. Increment the value of the element whose index equals the value of the int in the original array by one each time through. Some of the values may remain zero, so if you only want to output those that are present at least once in the original array, you'll have to evaluate each element before outputting.
Alternatively you could use a map with two ints, the first being the int value in the original array, and the second being the number of times that int appears in the original array.
I'm sure there are others to do this, too. Give it your best shot and repost when needed.