Thread: Program that prints closest number to average value

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    92

    Program that prints closest number to average value

    Write a program code which is enters 10 integers .
    It prints the average value of all the numbers entered and prints nearest loaded number to the average .
    Example:
    input : 1 2 3 4 5 0 0 0 0 1
    Output: 1.6 2

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int main(void) {
        
    
    
        int Num[10+1], i, closestNum = 0;
        float middle_Value = 0 , check[10+1];
    
    
        printf("Enter 10 integer values:\n");
        
        for( i = 0; i < 10; i++){
            
            scanf("%d", &Num[i]);
            middle_Value += Num[i];
        }
        
        middle_Value = middle_Value / (i + 1.0);
        
        for ( i = 0; i < 10; i++){
            
            check[i] = fabs(middle_Value - Num[i]);
            
            if( i < 1){
                
                closestNum = Num[i];
                
            }else{
        
                if( check[i] < check[i-1] ){
                
                    closestNum = Num[i];
            
                }
            }
            
        }
        
        printf(" %f  %d", middle_Value, closestNum );
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2015
    Posts
    92
    It prints 1 for the closest number.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You should make 10 a macro constant and use it everywhere.
    Code:
    #define SIZE 10
    I don't see what the +1 is for in the sizes of your arrays.

    In calculating middleValue you're dividing by 11.0 since i would have been 10 after the for statement. Try dividing by (double)i .And shouldn't middleValue be called average?

    Instead of a separate if (i < 1) test in the second for, just do that before the loop and start the loop at 1. And you don't need a SIZE-sized array for check. You just need the current and last values.
    Code:
    closestNum = num[0];
    lastDiff = avg - num[0];
    for (i = 1; i < SIZE; i++) {
        diff = fabs(avg - num[i]);
        if (diff < lastDiff)
            closestNum = num[i];
        lastDiff = diff;
    }
    BTW, your capitalization is a little odd. Normally Num would be num and middle_Value would be middleValue.
    Last edited by algorism; 04-24-2016 at 12:37 PM.

  4. #4
    Registered User
    Join Date
    Dec 2015
    Posts
    92
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define SIZE 10 
     
    int main(void) {
         
     
     
        int num[10+1], i, closestNum;
        float avg = 0, diff , lastDiff;
        
     
     
        printf("Enter 10 integer values:\n");
         
        for( i = 0; i < 10; i++){
             
            scanf("%d", &num[i]);
            avg += num[i];
        }
         
        avg = avg / 10.0;
        closestNum = num[0];
        lastDiff = avg - num[0];
        
        for (i = 1; i < SIZE; i++) {
            diff = fabs(avg - num[i]);
            
            if (diff < lastDiff)
                closestNum = num[i];
            
            
            
            lastDiff = diff;
        }
         
        printf(" %f  %d", avg, closestNum );
         
        return 0;
    }
    I am still getting the same result , closest number: 1.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I guess I should've tested it. We don't want the lastDiff, but instead the smallestDiff !
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    #define SIZE 10 
      
    int main(void) {
        int num[SIZE], i, closestNum;
        double avg = 0, diff, smallestDiff;
      
        printf("Enter %d integer values:\n", SIZE);
        for (i = 0; i < SIZE; i++){
            scanf("%d", &num[i]);
            avg += num[i];
        }
          
        avg /= (double)SIZE;
        closestNum = num[0];
        smallestDiff = avg - num[0];
    
        for (i = 1; i < SIZE; i++) {
            diff = fabs(avg - num[i]);
            if (diff < smallestDiff) {
                closestNum = num[i];
                smallestDiff = diff;
            }
        }
          
        printf(" %f  %d\n", avg, closestNum);
          
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-30-2015, 09:15 AM
  2. Replies: 38
    Last Post: 02-19-2015, 10:45 PM
  3. Replies: 14
    Last Post: 01-09-2013, 06:35 AM
  4. Replies: 8
    Last Post: 12-30-2010, 10:08 PM
  5. Replies: 9
    Last Post: 11-20-2003, 08:55 PM