Hello guys thanks for your input.
To explain better this is what I have to do.
1. I have to start with a sentinel control loop
1a- check for negative input
1b- check for entry count so that negative input does not increment count / let user know that when 10 elements are in the array.
2. Nest a while loop that will sort and populate the array. // algorithm provided below
3. Print the array.
//Sort array Algorithm.
inserting a value num into an array maintaining ascending order
find the position pos in the array to insert num
move all values in the array from pos and on, down one location
insert the new value num at the position pos
add 1 to count
Stepwise Refinement
Step 1 - find the position pos in the array to insert num
set pos to 0
set found to false
while not found and pos < count
if numbers[pos] > num
set found to true
else
add 1 to pos
Step 2 - move all values in the array from pos and on, down one location
for i is count downto pos+1
set numbers[i] to numbers[i-1]
Final Algorithm
set pos to 0
set found to false
while not found and pos < count
if numbers[pos] > num
set found to true
else
add 1 to pos
for i is count downto pos+1
set numbers[i] to numbers[i-1]
set numbers[pos] to num
add 1 to count
// hence why is what I came up with
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#include <iostream>
#include <cmath>
using namespace std;
const int arraySize =10;
const int sentinel = 0;
int main ()
{
int array[arraySize]; //array as array index
int num;
int count = 0;
int index;
double average = 0;
int pos = 0;
bool found = false;
cout << "Enter up to 10 positive integers, 0 to quit: \n";
cin >> num;
while(num != sentinel)
{
if (count == arraySize)
cout << "You have 10 digits, more entries will be ignored. Enter 0 to quit";
else if (num<0)
cout << "Please enter a positive interger or 0 to quit" << endl;
else
{ // code to arrange the input without sorting the array.
while (!found && pos < count)
{
if (array[pos]>num)
found = true;
else
pos++;
for (index=count; index > pos; index--)
{
array[index]=array[index-1];
array[pos]=num;
count++;
}
}
}
cin >> num;
}
for (index=0; index<arraySize; index++)
cout << array[index] << " ";
return 0;
}
|