Hello! I'm practising 2d-arrays for my upcoming test so pls help! i cant figure out what went wrong :(((((

Q: A factory has 3 divisions and stocks 4 products. An inventory data is updated for
each division and for each product as they are received. The current inventory data
is shown in Figure H10.1. The cost data of each product is given.
P1 P2 P3 P4
inventory for 1st division 10 13 14 23
inventory for 2nd division 21 24 18 5
inventory for 3rd division 9 14 12 4

Cost of each product
P1 35.45
P2 99.99
P3 210.50
P4 145.00
Write C program to calculate and display the inventory value of each
division.

This is what I wrote:

#include <stdio.h>
#define NO_OF_PROD 4
int main(void)
{
 int row, col;
 int stocks[3][NO_OF_PROD]={{10,13,14,23},{21,24,18,5},{9,14,12,4}};
 double cost[4]={35.45,99.99,210.50,145.00};
 double product[3];
 void matrix_vector(int, int, int[][], double[], double[]);
 matrix_vector(3,NO_OF_PROD,stocks,cost,product);

 for (row=0;row<3;row++)
 {   for (col=0;col<3;col++)
     printf("Division inventory value=%lf\n", product[col]);

    system("PAUSE");
	return 0;
}
}
void matrix_vector(int row, int col, int stocks[][NO_OF_PROD], double cost[], double product[])
{
 int i, j;
 for(i=0;i<=row-1;i++)
 product[i]=0.0;
 for(j=0;j<=col-1;j++)
 product[i]+=stocks[i][j]*cost[i];
}

Recommended Answers

All 2 Replies

You haven't used [[B][/B]code[B][/B]] tags so I can't tell exactly what you were thinking when you wrote this, but your problem is with your for loops and indentation. Whenever you make something, always indent to show yourself which statements belong to another statement. If more than one statement is indented under another, then you need to use { and }.

In your main function, you have (properly indented):

int main()
{
  ... // stuff skipped here

  for (row=0;row<3;row++)
  {
    for (col=0;col<3;col++)
      printf("Division inventory value=%lf\n", product[col]);

    system("PAUSE");
    return 0;
  }
}

You can see that the call to system and return belong to the outer for loop, which is not what you wanted. Since product is a 1D array, you don't actually need two loops, so go ahead and delete lines 5, 6, and 12, and unindent 7..11 by two spaces.

You have a similar problem in matrix_vector.

void matrix_vector(int row, int col, int stocks[][NO_OF_PROD], double cost[], double product[])
{
  int i, j;
  for(i=0;i<=row-1;i++)
    product[i]=0.0;
  for(j=0;j<=col-1;j++)
    product[i]+=stocks[i][j]*cost[i];
}

You need lines 5, 6 and 7 to belong to the loop on line 4, but currently only line 5 does. Indent 6 and 7 a couple of spaces and place a { between lines 4 and 5 and a } between lines 7 and 8.

Other than these simple syntactic errors you seem to be thinking out the problem very well.

Hope this helps.

THANKS! it worked!! :icon_cheesygrin:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.