Thread: Normalization Problem

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    29

    Normalization Problem

    I must write a function that has a one dimensional double array and the number of values in the array as its arguments. Normalize the values. I must also print the maximum, minimum, average and numbers of values above the average in the array.

    The equation that computes the normalized value from a value x is:
    Normalized x= (x-min(array))/(max(array)-min(array))

    My code does not print the correct normalized value, average and values above average.

    Code:
    #include <stdio.h>
    
    
    int findMax( double array1[], int num_elements) // This function computes the maximum value of the array
    {
       int i, max=-32000;
       for (i=0; i<num_elements; i++)
       {
         if (array1[i]>max)
         {
            max=array1[i];
         }
       }
       return(max);
    }
    int findMin( double array1[], int num_elements) // This function computes the minimum value of the array
    {
        int i, min=0;
        for (i=0; i<num_elements; i++)
        {
           if (i==0)
                min=array1[i];
        else
            if (array1[i]<min)
                min=array1[i];
        }
        return(min);
    }
        double averageX(double array1[], int num_elements)  //This function computes the average value of the array, and the number of values that are
                                                            // larger then the average
        {
            float average;
            int i, sum=0, valuesAboveAverage=0;
    
    
                for (i=0; i<num_elements; i++)
                {
                    sum+=array1[i];
                    average=sum/num_elements;
    
    
                    if (array1[i]>average)
                        valuesAboveAverage++;
                }
                printf(" Values above the average: %d\n", valuesAboveAverage);
                printf("Average: %f\n", average);
    
    
                return(average);
    
    
        }
    
    
        void norm_1D(int min, int max, int average, double array1[], int num_elements)  // This function computes the normalized values of the original
                                                                                        // array values
        {
            int i;
            double normalizedVal[10];
            for (i=1; i<num_elements; i++)
            {
                    normalizedVal[i]=(array1[i]-min)/ (max-min);
            }
            for (i=0; i<10; i++)
            printf("Normalized Values: %5.2f\n", normalizedVal[i]);
        }
    
    
    int main(void)
    {
       double  array1[10] = {1, 0, -8, 10, 8.8, 4.5, 2, 12, 9, 5};
       int max, min;
       double average;
    
    
    
    
       max = findMax(array1, 10);
       min = findMin(array1, 10);
       average = averageX(array1, 10);
       norm_1D(min, max, average, array1, 10);
    
    
       printf("The min is %d\n", min);
       printf("The max is %d\n", max);
    
    
        return(0);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of using -32000 as the initial maximum value for findMax, #include <limits.h> and use INT_MIN. For findMin, you use the other approach of setting the initial minimum value to be the value of the first element. In this case, instead of constantly checking for i == 0 in the loop body, just check that num_elements is greater than 0 before the loop. If not, return 0, otherwise initialise min = array1[0] and start looping from the second element.

    Now, onto your problem with the average: instead of computing the average inside the loop, compute it after the loop. Only after you have processed all the elements can you compute the average. Consequently, you need to loop again to determine how many elements are greater than the average. In fact, I would split the averageX function into two: one function to compute the arithmetic mean and return it, another function to count the number of elements greater than the arithmetic mean and return that count.

    For norm_1D, I suggest that normalizedVal not be a local array. Rather, declare normalizedVal in the main function, then pass a pointer to its first element to norm_1D, just like what you do for array1. This way, norm_1D can be used with arrays of different sizes without problems. That said, I note that you start the first for loop in norm_1D from i = 1 instead of i = 0, and you don't use the average parameter.

    EDIT:
    I noticed a few other problems too, e.g., sum is an int rather than a double. average should be a double, but if you split the function as I suggested that you don't even need it to begin with. Oh, and your indentation needs work.
    Last edited by laserlight; 11-14-2013 at 10:05 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    29
    Okay, so everything is working but the above average function. I put it in another function but it still won't give me the right answer.

    Code:
    void aboveAverage(double average, int num_elements, double array1[])
        {
        int i;
        double aboveAverage;
            for (i=0; i<num_elements; i++)
            {
                if (array1[i]>average)
                aboveAverage++;
            }
            
        printf("Values above the average: %5.2f\n", aboveAverage);
        
        }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You forgot to initialise aboveAverage to 0. Furthermore, it should be an int, not a double, and then you should print it as an int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    29
    It is working fine now, thank you very much!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You're welcome. You really should move the printf to outside of the function: the function can return the count, then the caller can decide how to print it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem passing argument into function, basic problem
    By tsdad in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2013, 12:09 PM
  2. Replies: 2
    Last Post: 01-06-2013, 07:49 AM
  3. Replies: 1
    Last Post: 12-07-2012, 10:00 AM
  4. Hashing, indexing and phonetic normalization for approximate str matching...
    By biterman in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 11-21-2006, 09:42 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM

Tags for this Thread