I can not get this to work. Can someone please help me. I have been working on this for a week. I am trying to take up to 10 numbers from the user (if they enter a zero it stops taking numbers). Then I try to sort the numbers from lowest to highest. Give the user the sum and the average. I am geting so frustrated and I am about ready to just give up. You guys have been great so far but I am just not getting this to work.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define SIZE 10 /* size of our array */
void bubbleSort (int arr[]);
void swap (int arr[], int nIndex);
int main(void)
{
char theNums[10][50];
int nums[10];
int i, j, sum = 0;
int numberCount = 0;
float average;
printf("Start by entering up to 10 numbers. To exit enter a zero.\n");
printf("\n");
for(i=0; i < 10; i++)
{
printf("Enter a Number: "); /*get the numbers*/
fgets(theNums[i], 50, stdin);
j = strlen(theNums[i])-1;
theNums[i][j] = '\0'; /* get rid of '\n' */
if(theNums[i][0] == '0') /* exit if zero */
break;
else if(!isdigit(theNums[i][0])) /*verify user data is a digit*/
{
printf("%s is not a valid entry.\n", theNums[i]);
nums[i] = 0;
}
else
nums[i] = atoi(theNums[i]);
numberCount ++; /*count the numbers being entered*/
}
printf("\n\n");
average = 0; /*calculate average*/
for(j=0; j < i; j++)
sum+=nums[j];
average = (sum/numberCount);
printf("Here are the numbers you entered:\n"); /*print the numbers entered on the screen*/
for(i=0; i < numberCount; i++)
printf("%i ",nums[i]);
printf("\n");
printf("Here are the numbers you entered sorted from lowest to highest:\n"); /*sort and print*/
bubbleSort(theNums); /* sort the array */
printf("\n\n");
/* print the sorted array */
for(i=0;i<theNums;++i)
printf("array[%d] = %d\n",i,theNums[i]);
printf("The sum of your numbers is %d", sum); /* display sum*/
printf("\n");
printf("You have entered %d numbers", numberCount); /*display qty of numbers entered*/
printf("\n");
printf("The average of the numbers is %.2f", average); /*display average*/
printf("\n");
getchar(); /*wait for keystroke*/
return 0;
}
void bubbleSort (int arr[])
{
int i,j;
for (i=0; i<SIZE; ++i)
for(j=0; j < (SIZE-1); ++j)
if(arr[j] > arr[j+1])
swap(arr,j);
}
void swap (int arr[], int nIndex)
{
int temp = arr[nIndex];
/* swap the elements */
arr[nIndex] = arr[nIndex+1];
arr[nIndex+1] = temp;
}