Need some guidance with an assignment please - arrays and functions
Hi, Im hoping someone can give me some guidance point me in the right direction with an assignment I have.
I have done the first part, i think??, which is to allow the user to enter up to 10 numbers in total with a choice of yes or no if they want to continue entering up to that point. They are to be stored in an array.
Now the bit I am struggling with is, once the numbers have been entered the program should work out the total value, smallest number, largest number, the average, display the numbers in ascending order and also show the range, all to be displayed on screen. I've used bubblesort to reorganise the numbers but Im not really sure how it works.
I feel completly lost and it needs to be finished for next week.
heres what I have done so far:
·
Code:
int obtainNumber(int numbers[]);
void displaynumbers(int numbers[10], int lastIndex);
void bubbleSort(int numbers[], const int arrayLength);
void swap(int *p, int *q);
int main()
{
const int arraySize = 10;
int numbers[arraySize];
int lastIndex;
lastIndex; obtainNumber(numbers);
int lastIndex = 10;
printf ("starting array contents \n");
displayNumbers (numbers, lastIndex);
bubbleSort (numbers, lastIndex + 1);
printf (" \n After sorti8ng array\n");
displayNumbers (numbers, lastIndex);
system("pause");
return 0;
}
int obtainNumber(int numbers[])
{
int index = -1;
char option = 'y';
while(option == 'y' || option == 'Y')
{
index = index + 1;
printf("Enter a number: ");
scanf("%i", &numbers[index]);
_flushall();
if (index < 9)
{
printf("Do you want to enter another number?('y' for yes 'n' for no) ");
scanf("%c", &option);
_flushall();
}
else
{
option = 'n';
}
}
return index;
}
printf ("starting array contents \n");
displayNumbers (numbers, lastIndex);
bubbleSort (numbers, lastIndex + 1);
printf (" \n After sorti8ng array\n");
displayNumbers (numbers, lastIndex);
system ("pause");
return 0;
}
void displayNumbers (int numbers[], int lastIndex)
{
printf ("The contents of the array is:\n");
int i = 0;
while (i <= lastIndex)
{
printf ("%i\n", numbers [i]);
i++;
}
}
void bubbleSort (int numbers [], const int arrayLength)
{
int i, sorted = 0;
while (sorted == 0)
{
sorted = i;
i = 1;
while (i < arrayLength)
{
if (numbers [i - 1] > numbers [i])
{
swap (&numbers[i - 1], &numbers[i]);
sorted = 0;
}
i++;
}
}
}
void swap(int *p, int *q)
{
int temp;
temp = *p;
*p = *q;
*q = temp;
}
Thanks for looking, hope someone has time to help me out!