1

I am writing a program using a 2D array. The user inputs 5 original price values and 5 discount rates for each price. I have the input figured out but I do not understand how to execute the process for a 2D array. I need to figure out the sales price for each original price. My process is:

 amtDiscount = originalPrice * discount;
 salePrice = originalPrice - amtDiscount;

I just don't understand how to do it with the array. I tried doing

amtDiscount = priceDiscount[ROWS] * priceDiscount[COLS];

but when compiling I got this error: subscripted value is neither array nor point

Here is my code:

#include <stdio.h>
#define ROWS 5
#define COLS 5

int main()
 {
    // Declarations
    float priceDiscount[ROWS][COLS];
    float originalPrice = 0.0;
    float discount = 0.0;
    float salePrice = 0.0;
    float amtDiscount = 0.0;
    int i, j = 0;

   //input
   for(i = 0; i < 5; i++)
   {
       printf("Please enter 5 original prices, price %d: ", i+1);
       scanf("%f", &priceDiscount[ROWS]);
   }


   for(j=0; j < 5; j++)
    {
      printf("Please enter the discount rate for each 5 prices, discount
      rate %d: ", j+1);          
      scanf("%f", &priceDiscount[COLS]);
    }

    //Process
   amtDiscount = originalPrice * discount; 
   salePrice = originalPrice - amtDiscount;

   //Output
   printf("Price %%Off Sale");


    /*Test Cases
        Case 1
            Input: $3.50, 25% Output: $3.50, 25%, $2.63
        Case 2
            Input: $5.25, 20% Output: $5.25, 20%, $4.20
        Case 3
            Input: $4.00, 50% Output: $4.00, 50%, $2.00*/

   return 0;

}

Any advice is greatly appreciated! Thanks!

3 Answers 3

1

Actually, I would recommend using an array of struct's:

#define NUM_ITEMS 5

struct {
  float list_price;
  float discount;
} prices;

struct prices current_inventory[NUM_ITEMS];

...
for(i = 0; i < NUM_ITEMS; i++) {
   printf("Please enter 5 original prices, price %d: ", i+1);
   scanf("%f", &current_inventory[i].list_price);
}
for(i = 0; i < NUM_ITEMS; i++) {
   printf("Please enter the discount rate for each 5 prices, discount %d: ", i+1);
   scanf("%f", &current_inventory[i].discount);
}
for(i = 0; i < NUM_ITEMS; i++) {
   printf("sales_price[%d]= %f *f = %f\n",
     current_inventory[i].list_price, current_inventory[i].discount,
     current_inventory[i].list_price * current_inventory[i].discount);
   ...

Untested: but might be a viable alternative for you...

Another variation: you might want to save the sales price (along with other information like "Description" and "quantity" in the record:

struct {
  char description[80;
  int quantity;
  float list_price;
  float discount;
  float sales_price;
} items;
...
Sign up to request clarification or add additional context in comments.

Comments

0

You don't need:

float priceDiscount[ROWS][COLS];

to store the original price and the discount. You can use:

float priceDiscount[ROWS][2];

or

float priceDiscount[2][COLS];

If you go with:

float priceDiscount[ROWS][2];

priceDiscount[0][0] can be used to store the original price of the 0-th item and priceDiscount[0][1] can be used to store the discount of the 0-th item.

The lines of code to read the data will be:

for(i = 0; i < 5; i++)
{
   printf("Please enter 5 original prices, price %d: ", i+1);
   scanf("%f", &priceDiscount[i][0]);
}


for(j=0; j < 5; j++)
{
   printf("Please enter the discount rate for each 5 prices, discount
          rate %d: ", j+1);          
      scanf("%f", &priceDiscount[j][1]);
}

Comments

0

When you create the array, it is correct to use ROWS and COLS. However, if you use such values when processing the array, you will always be accessing position ( 5, 5 ), which, by the way, falls outside the bounds of the array. Any dimension in an array starts its index in 0 and goes on, on increments of 1, until the last index, which equals the size of that dimension - 1. I'm not sure how to process your array, but you could use two nested for statements to accomplish that:

for ( rowCounter = 1 ; rowCounter <= ROWS - 1 ; ++rowCounter )
{
    for ( columnCounter = 1 ; columnCounter <= COLS - 1 ; ++columnCounter )
    {
        //Process element priceDiscount[ rowCounter ][ columnCounter ]
    }
}

In this code, it is assumed that integer variables rowCounter and columnCounter have already been declared. Also, you could view the mechanics of the two nested for statements as: "first, choose a row. Then, process the element of each column in that row. After that, go to the next row, process the element of each column in that row. And so on".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.