-
still having a problem
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;
}
-
Start by reading the numbers in properly, like I showed you in one of your previous threads.
Please don't start new threads on the same topic.
-
I have tried to do this. If I take the sorting part away this works properly. I can not get this to work at all.
-
Code:
> 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]);
Code:
bubbleSort(nums); /* sort the array */
printf("\n\n");
/* print the sorted array */
for(i=0;i<10;++i)
printf("array[%d] = %d\n",i,nums);
-
Thanks to everyone for the help. This is what I have now. But it still doesn't seem to work properly when it sort and prints the sorted numbers in the array.
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(nums); /* sort the array */
printf("\n\n");
/* print the sorted array */
for(i=0;i<10;++i)
printf("array[%d] = %d\n",i,nums);
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;
}
-
Code:
/* print the sorted array */
for(i=0;i<10;++i)
printf("array[%d] = %d\n",i,nums[i]);
-
Thanks guys. The code works pretty good now with just one exception. If the user doesn't fill up the array with 10 numbers I will get junk numbers in the empty spaces. Is there a way to have those not print?
One other thing I am trying to do is calculate the aveage. How do I get the average to display a decimal? Such as the user enters 7 numbers with a total of 34. My program only seems to give whole values for an average. For instance my answer to average would be 4 instead of 4.85. Is there a way to do this? I tried using "%" but that didnt work.
-
Initialise the array
int nums[10]={""};
should do the trick I think.
% is the modulus operator, it gives a remainder. ie. 5 % 3 = 2
Want you probably want it to use floats instead of ints.
-
>>the user doesn't fill up the array with 10 numbers I will get junk numbers in the empty spaces. Is there a way to have those not print?
Count how many the user enters and only loop that many times when printing from the array.
>>How do I get the average to display a decimal?
Use a float or a double.
>>int nums[10]={""};
Almost, the correct way would be
>>int nums[10]={0};
-
Ah yes, whoops, brain was stuck on character array's. Thanks Hammer. ct26orr, what he said, A) it's good to initialise array's, but B) you should only print out the number of entries that are actually there.