Thread: Array help!

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    6

    Array help!

    task:

    There are a number of ways to normalize, or scale, a set of values. One common normalization technique scales the values such that the minimum value goes to 0, the maximum value goes to 1, and other values are scaled accordingly. For example, using this normalization, we can normalize the values in the following array:

    Array values
    -2, -1, 2, 0

    Normalized array values
    0.0, 0.25, 1.0, 0.5

    The equation that computes the normalized value from a value xk in the array is

    Normalized xk = (xk - min)/(max - min)

    where min and max represent the minimum and maximum values in the array x, respectively. If you substitute the minimum value for xk in this equation, the numerator is zero, and thus the normalized value for the minimum is zero. If you substitute the maximum value for xk in this
    equation, the numerator and denominator are the same value, and hence the normalized value for the maximum is 1.0.

    Write a C program that normalizes a set of data. You may read the data from a data file or from the keyboard. However, the data must be stored in an array. Print out the original and normalized values in a table that is labeled. "
    my attempt:

    Code:
    main()
    {
      int x;
      float a[100],
            i,
            normx,
            max=a[0],
            min=a[0];
    
      printf("Enter the array separating each by a space:");
      scanf("%f",&a[x]);
    
      for(x=0; x < 100; x++)
      {
        if(a[x]<min)
         min = a[x];
        if(a[x]>max)
         max = a[x];
      }
      normx = ((float)x-min)/(max-min);
    
      printf("\n\n%f",a[normx]);
    
          system("PAUSE");
          return 0;
    }
    compilation errors: a[normx] subscript needs to be an integer. however in my task, the normalized values need to be float.




    HELP PLEASE!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    main()

    it should be int main(void) - read FAQ http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    Code:
    max=a[0],
            min=a[0];
    as your a array is not initialized yet - your min and max values get garbage

    scanf("&#37;f",&a[x]);
    this line should be inside the loop - you have to give the user a possibility to enter less than 100 numbers

    Code:
    normx = ((float)x-min)/(max-min);
    you should normalize a[x] not x
    you should do it for every x - so enother loop is required here


    Code:
    printf("\n\n%f",a[normx]);
    just horrible, what you are trying to achieve here? some random code put here and there?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    Quote Originally Posted by vart View Post
    Code:
    max=a[0],
    min=a[0];
    as your a array is not initialized yet - your min and max values get garbage

    Code:
    printf("\n\n%f",a[normx]);
    just horrible, what you are trying to achieve here? some random code put here and there?
    what should i do about these then?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    initialize min and max after you read a[0] from the user

    add the second loop for normalizing members of the array
    add the 3rd loop for output result

    add comments that explain what the next code statement should do
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Day Dreamer
    Join Date
    Apr 2007
    Posts
    45
    I have entered comments in your code! Go through them.


    Quote Originally Posted by killcapital View Post
    task:



    my attempt:

    Code:
    main()
    {
      int x;
      float a[100],
            i,
            normx,
            max=a[0],  // a[0] contains a garbage value at this point. 
            min=a[0];
    
      printf("Enter the array separating each by a space:");
      scanf("%f",&a[x]);   /* This should be in a loop which allows the user to enter multiple values 
                                      into your array. Thereafter, assign max and min */
    
      for(x=0; x < 100; x++)
      {
        if(a[x]<min)
         min = a[x];
        if(a[x]>max)
         max = a[x];
      }
      normx = ((float)x-min)/(max-min);
    
      printf("\n\n%f",a[normx]);  // An array index should be an interger type!!!
    
          system("PAUSE");   // Why do you want to do that??? Comments??
          return 0;
    }
    compilation errors: a[normx] subscript needs to be an integer. however in my task, the normalized values need to be float.




    HELP PLEASE!
    I would love to change the world but they dont give me the source code!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM