| | |
help with 2d array
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Solved Threads: 0
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:
((((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:
c Syntax (Toggle Plain Text)
#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]; }
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
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):
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.
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.
In your main function, you have (properly indented):
C Syntax (Toggle Plain Text)
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 have a similar problem in matrix_vector.
C Syntax (Toggle Plain Text)
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]; }
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.
![]() |
Similar Threads
- Can I ghost a RAID array??? (Windows NT / 2000 / XP)
- Creating dynamic array structures (C++)
- Array limit (C)
- struct dynamic 2d array alloc (C)
- string to integer array transformation (C)
- Array (Visual Basic 4 / 5 / 6)
Other Threads in the C Forum
- Previous Thread: Problem when passing a structure containing an array of 2D-chars array to a function
- Next Thread: A 1d array int a[3,2]
Views: 884 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o inches infiniteloop initialization interest kilometer km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape socketprograming spoonfeeding stack standard strchr string strings structures student suggestions system systemcall test testautomation unix user voidmain() wab win32 win32api windows.h






