Thread: Three ONE dimensional arrays

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    16

    Unhappy Three ONE dimensional arrays

    Hi
    What I need to do is declare three one dimensional arrays neamed price, quantity and amount. Each array shoule be capable of holding 10 elements. Using a for loop input values for the price and quantity arrays. The entries in the amount array should be the product of the corresponding values in the price and quantity arrays. After all of the data has been entered it should display the following output.
    Quantity Price Amount
    ______ ____ ______
    I know my program is a bit of a mess and not with out structure problems because I get a thought in my head and just type it out before I forget but if I could at least get help with the part of making the array print out correctly I would appericate it. Please help!

    Code:
    #include <stdio.h>
    int main()
    
    {
    #define Bulk 10    
    #define How_Much 10
    #define Sticker_Shock 10
    int Quantity[Bulk];  
    int Price[How_Much];
    
    int i;
    float Amount[Sticker_Shock];
        
        
            for (i = 0; i < Bulk; i++)
        {
            printf("Quantity");
            scanf("%d", &Quantity);
             
        }
                 
        for (i = 0; i < Bulk; i++)    
            printf("Quantity %d = %d\n", i, Quantity[i]);  
       
       
       for (i = 0; i < How_Much; i++)
       {
           printf("Price: ");
           scanf("%d", &Price[i]);
       }
      
       for (i = 0; i < How_Much; i++)
           printf("Price %d = %d\n", i, Price[i]);    
       }
       
       Amount=Quantity * Price;
       }
       for (i = 0; i <= 9; i++)
       
       printf("%3d %7d %6d\n", Amount,Quantity,Price);
       
        getchar();
        getchar();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    The name of an array is not a variable and can't be used as an lvalue.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    16
    Could you please explain what you mean by lvalue? All I want to know is how to make this one dimensional array print out so it prints out Quantity Price and Amount with line underneath them.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    itCbitC is remarking about this line:
    Code:
    Amount=Quantity * Price;
    You need to use a loop if you are trying to access each element. You cannot mutliply or assign arrays.

    An lvalue is something that can go on the left side of assignment. Arrays cannot.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Any particular reason You quoted yourself in your initial post? An attempt at third person narrative?

    You got your first three loops fine, using arrays correctly etc, by the time we get down to the fifth loop it's all done horribly wrong. Why did 9 suddenly become important when you have nicely name constants like Sticker_Shock? (Note that you shouldn't have 3 separate constants, they can all use the same one)
    Why do you have non-matching braces?

    Here's a big hint: When you know that your program is a bit of a mess, tidy it up as best you can before posting! We don't want to wade through the mess of bugs that would be obvious to yourself if you cleaned the code up first, and you don't want the embarrasment of that either.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    16
    Hi iMalc

    I got a little confused about this array program. I know that I have the first few loop correct but after that I'm not sure what to do. I do this all the time I'm able to figure out most of the program then I totally go blank my teacher told me I'm to up tight and I need a beer. I will work on it today and post what I have later. Thank you so much for the information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  2. Dynamic two dimensional arrays
    By ThWolf in forum C++ Programming
    Replies: 14
    Last Post: 08-30-2006, 02:28 PM
  3. passing two dimensional arrays
    By Nova_Collision in forum C++ Programming
    Replies: 3
    Last Post: 02-04-2003, 01:47 PM
  4. two dimensional arrays
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2002, 08:12 PM
  5. Two dimensional arrays
    By Jax in forum C Programming
    Replies: 1
    Last Post: 11-07-2001, 12:53 PM