Hi all,
I need to write a program which you input up to 10 numbers, and it sorts them into ascending, descending and random ordering.
It also needs to alert the user when a float or a char is entered, as it can only take integers.
The rest is pretty much sorted, but how would I go about altering the program such that the user is alerted and re-prompted to enter the value? I'm guessing the main function will have to be re-written.
The program:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
/*Prototype declarations */
void sort(int inputValues[], int promptCount);
int main()
{
int inputValues[10], promptCount, count1, count2;
inputValues[10] = 0;
printf("Please Enter Your Values (as Integers - up to 10 Values Maximum). If you require less than 10 values, please enter any negative Integer.\n");
for(promptCount = 0; promptCount < 10; promptCount++)
{
printf("\nEnter the #%d value:", (promptCount+1));
scanf("%d", &inputValues[promptCount]);
}
sort(inputValues, promptCount);
printf("\nArranged in Ascending Order:");
for (count1 = 0; count1 < 10; count1++)
printf(" %d,", inputValues[count1]);
printf("\nArranged in Descending Order:");
for (count2 = 9; count2 > -1; count2--)
printf(" %d,", inputValues[count2]);
}
void sort(int inputValues[], int promptCount)
/* Bubble Sort. */
{
int i, j, temp;
for(i = 0; i < (promptCount); i++)
{
for (j = 0; j < (promptCount - i - 1); j++)
{
if( inputValues[j] > inputValues[j + 1] )
{
temp = inputValues[j];
inputValues[j] = inputValues[j + 1];
inputValues[j + 1] = temp;
}
}
}
}