Thread: array program help.

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    12

    array program help.

    i need some direction on how to do this. i need to take the values in my two arrays and find the product of them and store that into my third array. This is what i have so far.

    Code:
    #include <stdio.h>
    #include <conio.h>
    int main()
    {
        void extend(float [9], float[9] , float[9]);
        float price[9];
        float quantity[9];
        float amount[9];
        
        int i;
        
        
        printf("this program will accept 10 numbers and the quantity\n");
        
        for(i = 0; i < 9; i++)
              {
              printf("Please enter the price:\n");
              scanf("%f", &price[i]);
              printf("Please enter the quantity:\n");
              scanf("%f", &quantity[i]);
              }
        extend(price, quantity, amount);
        
        getch();
        return 0;
    
    
    
    
    }
    void extend(float price[], float quantity[], float amount[])
    {
         int j;
         int h;
         
         for(j = 0; j < 9; j++)
         {
               amount[j] = price[j] * quantity[j];
         }
               
         for(h = 0; h < 9;h++)
         {
               printf("%.2f\n", amount[h]);
         }
    }
    for some reason i keep getting errors.
    Last edited by chumpp; 07-25-2012 at 10:57 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You'll have to tell us the errors, because it seems OK the me.
    Code:
    $ gcc foo.c
    $ ./a.out 
    this program will accept 10 numbers and the quantity
    Please enter the price:
    1
    Please enter the quantity:
    1
    Please enter the price:
    2
    Please enter the quantity:
    2
    Please enter the price:
    3
    Please enter the quantity:
    3
    Please enter the price:
    4
    Please enter the quantity:
    4
    Please enter the price:
    5
    Please enter the quantity:
    5
    Please enter the price:
    6
    Please enter the quantity:
    6
    Please enter the price:
    7
    Please enter the quantity:
    7
    Please enter the price:
    8
    Please enter the quantity:
    8
    Please enter the price:
    9
    Please enter the quantity:
    9
    1.00
    4.00
    9.00
    16.00
    25.00
    36.00
    49.00
    64.00
    81.00
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    12
    Quote Originally Posted by Salem View Post
    You'll have to tell us the errors, because it seems OK the me.
    Code:
    $ gcc foo.c
    $ ./a.out 
    this program will accept 10 numbers and the quantity
    Please enter the price:
    1
    Please enter the quantity:
    1
    Please enter the price:
    2
    Please enter the quantity:
    2
    Please enter the price:
    3
    Please enter the quantity:
    3
    Please enter the price:
    4
    Please enter the quantity:
    4
    Please enter the price:
    5
    Please enter the quantity:
    5
    Please enter the price:
    6
    Please enter the quantity:
    6
    Please enter the price:
    7
    Please enter the quantity:
    7
    Please enter the price:
    8
    Please enter the quantity:
    8
    Please enter the price:
    9
    Please enter the quantity:
    9
    1.00
    4.00
    9.00
    16.00
    25.00
    36.00
    49.00
    64.00
    81.00
    i just re-did the code. My question is this the right way to go about putting the values in that new array. or any tips to make it more clean?

    FYI im very new to C

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    int main()
    {
        void extend(float [9], float[9] , float[9]);
    You usually put function prototypes outside of main (at the beginning of the file, after all include's).
    And if you pass an array as a parameter it's more flexible to add an additional parameter for the size of the array and then you use it instead of the hardcoded size:
    Code:
    void extern(float price[], float quantity[], float amount[], int size);
    I prefer to match the prototype with the actual definition hence I've added the parameter names.

    Code:
    float price[9];
    float quantity[9];
    float amount[9];
    printf("this program will accept 10 numbers and the quantity\n");
    You know that you are only processing 9 numbers? :-)
    I'm not sure if you already know about the preprocessor but if you use magic numbers in your code you should define them at the beginning of the file. Then there is only one place to change them:
    Code:
    #define SIZE 10
    ... // later in the program you use it everywhere you would have used the hardcoded value
    for (i = 0; i < SIZE; i++)
    ...
    Code:
         
    for(j = 0; j < 9; j++)
    {
        amount[j] = price[j] * quantity[j];
    }
               
    for(h = 0; h < 9;h++)
    {
        printf("%.2f\n", amount[h]);
    }
    Since both for-loops have the same range you could merge them into one.

    Quote Originally Posted by chumpp View Post
    My question is this the right way to go about putting the values in that new array. or any tips to make it more clean?
    Looks fine for me.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a array program
    By antim in forum C Programming
    Replies: 2
    Last Post: 03-06-2011, 01:16 PM
  2. Replies: 1
    Last Post: 03-16-2010, 10:17 AM
  3. Help converting array program to link list program
    By hsmith1976 in forum C++ Programming
    Replies: 0
    Last Post: 02-14-2010, 09:50 PM
  4. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  5. Help with Array Program!
    By ProgrammingDlux in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2002, 01:02 PM