
need help with arrays please
: I'm trying to write a program using a 2-D array as a lookup table for
: referencing part numbers to give the corresponding inventory. Why isn't my
: code printing the correct numbers?
: #include <stdio.h>
: #include <conio.h>
/* ### non-standard header */
: #define x 8
: #define y 2
/* ### pretty confusing; try ROWS, COLS for want
* of anything better. Some like MAXPARTS would
* be more useful.
*/
: main()
/* ### int main(void) */
: {
: int a, b;
/* ### Use 'int a, b, partnumber;' here */
: int parts[x][y]={{1,10},{2,20},{3,30},{4,40},{5,50},{6,60},{7,70},{8,80}};
: clrscr();
: printf("Enter part number:");
: scanf("%d", &parts);
/* ### parts is an array; you can't do this. Use
scanf("%d", &partnumber);
* and check the return from scanf() as well.
*/
/* ### this gets confusing - use the code added below, which
* should do what you want. There's no point in iterating
* across the numbers and quantities (which I _think_ is
* what you are doing), you just want to scan the list.
* And you can't set an array equal to a constant - I'm
* suprised it compiled.
*/
: for (a=0; a<x; a++)
: for (b=0; b<y; b++)
: if (parts==a)
: printf("\nNO.\tQuantity");
: printf("\n%d\t%d", parts[a][b]);
: // else
/* ### non-standard comment */
: // printf("Sorry that part number does not exist");
for (a=0; a<x; a++) {
if (parts[a][0] == partnumber) {
printf("NO.\tQuantity\n");
printf("%d\t%d\n", parts[a][0], parts[a][1]);
break;
}
}
if (a == x)
printf("Sorry that part number does not exist\n");
: return 0;
: }
Will