still having a problem

This is a discussion on still having a problem within the C Programming forums, part of the General Programming Boards category; I can not get this to work. Can someone please help me. I have been working on this for a ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    55

    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;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    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.

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    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);

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    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;
    }

  6. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Code:
    /* print the sorted array */
    	for(i=0;i<10;++i)
          printf("array[%d] = %d\n",i,nums[i]);

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    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.
    Last edited by ct26torr; 02-18-2003 at 02:11 PM.

  8. #8
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    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.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>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};
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    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.
    Demonographic rhinology is not the only possible outcome, but why take the chance

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21