help with 2d array

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 2
Reputation: cwtliew is an unknown quantity at this point 
Solved Threads: 0
cwtliew cwtliew is offline Offline
Newbie Poster

help with 2d array

 
0
  #1
Oct 29th, 2007
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:
  1. #include <stdio.h>
  2. #define NO_OF_PROD 4
  3. int main(void)
  4. {
  5. int row, col;
  6. int stocks[3][NO_OF_PROD]={{10,13,14,23},{21,24,18,5},{9,14,12,4}};
  7. double cost[4]={35.45,99.99,210.50,145.00};
  8. double product[3];
  9. void matrix_vector(int, int, int[][], double[], double[]);
  10. matrix_vector(3,NO_OF_PROD,stocks,cost,product);
  11.  
  12. for (row=0;row<3;row++)
  13. { for (col=0;col<3;col++)
  14. printf("Division inventory value=%lf\n", product[col]);
  15.  
  16. system("PAUSE");
  17. return 0;
  18. }
  19. }
  20. void matrix_vector(int row, int col, int stocks[][NO_OF_PROD], double cost[], double product[])
  21. {
  22. int i, j;
  23. for(i=0;i<=row-1;i++)
  24. product[i]=0.0;
  25. for(j=0;j<=col-1;j++)
  26. product[i]+=stocks[i][j]*cost[i];
  27. }
Last edited by WaltP; Oct 29th, 2007 at 1:20 pm. Reason: Added CODE tags -- you actually typed right over how to use them when you entered this post... And you need to indent better
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: help with 2d array

 
0
  #2
Oct 29th, 2007
You haven't used [code] 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):
  1. int main()
  2. {
  3. ... // stuff skipped here
  4.  
  5. for (row=0;row<3;row++)
  6. {
  7. for (col=0;col<3;col++)
  8. printf("Division inventory value=%lf\n", product[col]);
  9.  
  10. system("PAUSE");
  11. return 0;
  12. }
  13. }
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.
  1. void matrix_vector(int row, int col, int stocks[][NO_OF_PROD], double cost[], double product[])
  2. {
  3. int i, j;
  4. for(i=0;i<=row-1;i++)
  5. product[i]=0.0;
  6. for(j=0;j<=col-1;j++)
  7. product[i]+=stocks[i][j]*cost[i];
  8. }
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.
Last edited by Duoas; Oct 29th, 2007 at 12:16 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 2
Reputation: cwtliew is an unknown quantity at this point 
Solved Threads: 0
cwtliew cwtliew is offline Offline
Newbie Poster

Re: help with 2d array

 
0
  #3
Oct 30th, 2007
THANKS! it worked!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 884 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC