need help with arrays please 
Author Message
 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>
#define x 8
#define y 2

main()
{
int a, b;
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);

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
//         printf("Sorry that part number does not exist");

return 0;

Quote:
}



Wed, 06 Sep 2000 03:00:00 GMT  
 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



Wed, 06 Sep 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Need Help with array...Please Advice

2. NEED HELP WITH PRITING AN ARRAY, PLEASE PLEASE HELP

3. Help Please-need algorithm for getting 9 point average in 2d array

4. help: Guru needed please please please

5. Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!

6. HELP Please Please (malloc on an array)

7. Please help!!!!Please help!!!!Please help!!!!

8. need help please please setting registry value

9. Help C newbie need help please

10. Help C newbie need help please

11. I really need some help with C graphics PLEASE HELP

12. Need help on this programming problem ( PLEASE HELP!!!)

 

 
Powered by phpBB® Forum Software