• C

Calling data from two dimensional array

The following snippet of code crashes after I enter in a 4 digit number. What this is supposed to do is look up a particular 'animal' (represented by a 4 digit number in an 2 dimensional array), and print the 7 numbers in it's own array.
                while(1){
                      printf("Enter an animal to look data up for");
                      scanf("%4d", &lookup);
                      if(lookup > 1000 && lookup < 9999){
                          printf("Food eaten during the week for animal #%d\n", lookup);
                          printf("%d ,", fooddata[lookup][1]);
                          printf("%d ,", fooddata[lookup][2]);
                          continue;
                      }else{
                          printf("Enter an animal to look data up for");
                          scanf("%d", &lookup);
                      }
               }
So looking at my very amateurish code there, I can't help but wonder why the program crashes whenever I enter in a 4 digit number. Any thoughts on what I'm doing wrong?
               
Tabris42Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sys_ProgCommented:
Nothing seems to be incorrect in the above program which would crash.

Only thing which could do that is your fooddata array size

i.e U are using your 4 digit number entered by user as the first index to your fooddata array

If 1 st dimension of  your fooddata arrayis of less size than the entered number, then your program would crash

Hope this helps

Amit
0
Sys_ProgCommented:
Basically you are trying to access some location which u do not own, In some cases, it would be possible to access some locations.

For example, the foll prog runs well till the value of i entered is 3102

#include <stdio.h>
#include <stdlib.h>

int main ( void ) {
      int i ;
      int a[10][2] = { 0 }  ;
      scanf ( "%4d", &i ) ;
      printf ( "\n%d", i ) ;
      printf ( "\n%d", a[i][1] ) ;
      system ( "PAUSE" ) ;  
}

But later it crashes

Basically it tries to access some memory which does not belong to you, it may be some priviledged memory area OR an area belonging to some other program [Memory out your program domain]

0
Tabris42Author Commented:
Well the problem seems to lie in using lookup as a variable. How would I go about searching an array of 300 elements to see if lookup matches one of those numbers?
If it doesn't match, I'd like to prompt for a correct number. If it does match, I'd like it to print from it's array of 7.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Sys_ProgCommented:
If I got what u say, here's what I think

U have declared a two dimensional array say

int a[300][7] ;  ---------- I took 300 from your comments above

As per my assumption, your four digit number is not to be used as array index

I guess the 4 digit numbers which indicate as animal are stored in 0th location of every row...?? Am i right

If yes, then u will have to run a for loop as below

for ( i = 0 ; i < 300; i++ )
  if ( arr[i][0] = lookup ) {
    YOUR PRINTF statements for Printing the data
  }
}


If my assumption isint correct, post your code, so that I can help in a better way

Amit



0
Sys_ProgCommented:
Sorry for the comparison operator

It should == instead of =

i.e

for ( i = 0 ; i < 300; i++ )
  if ( arr[i][0] == lookup ) {
    YOUR PRINTF statements for Printing the data
  }
}
0
AjarCommented:
Tabris42,
   
1.
dont use  scanf("%4d", &lookup);
instead use   scanf("%d", &lookup);
because  %4d  makes sense for writing and not for reading a number

2.
To be sure that lookup is what is causing the error   hard code the value to check



0
sunnycoderCommented:
>The following snippet of code crashes after I enter in a 4 digit number.
>What this is supposed to do is look up a particular 'animal'
>(represented by a 4 digit number in an 2 dimensional array),
>and print the 7 numbers in it's own array.

               while(1){
                     printf("Enter an animal to look data up for");
                     scanf("%4d", &lookup);
                     if(lookup > 1000 && lookup < 9999){
                         printf("Food eaten during the week for animal #%d\n", lookup);
                         printf("%d ,", fooddata[lookup][1]);
                         printf("%d ,", fooddata[lookup][2]);
                         continue;
                     }else{
                         printf("Enter an animal to look data up for");
                         scanf("%d", &lookup);
                     }
              }

can you post the declaration for fooddata[] and the rest of your code where you initialize fooddata ... assuming that what sys_prog said was true, i.e. you have animal number as the first element of each row of fooddata, you can reorganize your code as

int i;
               while(1){

                     printf("Enter an animal to look data up for");
                     scanf("%d", &lookup);               /* get the animal number */

                     if(lookup > 1000 && lookup < 9999){      /* chack if it is valid */
                         i = 0;
                         while ( i < MAX_ARRAY )      /* MAX_ARRAY is the maximum number of rows in
                                                                     * fooddata here we are checking all rows hence the while loop */
                         {
                                if ( fooddata[i][0] == lookup ) /* if we found the animal print the data.. also you said 7 numbers, so guess there will be more printfs (if you want to unroll the loop) or another loop for printing the values*/
                                {
                                          printf("Food eaten during the week for animal #%d\n", lookup);
                                          printf("%d ,", fooddata[i][1]);
                                          printf("%d ,", fooddata[i][2]);
                                          break;      /* get out of this loop since we already have found the animal*/
                                }else
                                     printf("Sorry ... I do not have any such animal !! \n"); /* the lookup was not found in fooddata*/
                       }
              }
0
Tabris42Author Commented:
Here is my new code:
                while(1){
                      printf("Enter an animal to look data up for: ");
                      scanf("%d", &lookup);
                      if(lookup > 1000 && lookup < 9999){
                          i = 0;
                          while (i < 300){
                               if (animaldata[i] == lookup){
                                      printf("Food eaten during the week for animal #%d: \n", lookup);
                                      printf("%d ,", fooddata[i][0]);
                                      printf("%d ,", fooddata[i][1]);
                                      printf("%d ,", fooddata[i][2]);
                                      printf("%d ,", fooddata[i][3]);
                                      printf("%d ,", fooddata[i][4]);
                                      printf("%d ,", fooddata[i][5]);
                                      printf("%d ,", fooddata[i][6]);
                                      break;
                               }else{
                                      printf("Invalid animal number.\n");
                               }
                          }
                      }
                }

Now the problem is, the program is only searching the first element of the 300 element array for the animal number. Here is a little more information about the arrays:
animaldata[300]: this is the 300 element array of 4 digit unique random numbers.
fooddata[300][7]: For every number in animaldata, there are 7 two digit numbers. This is what I need to print. When a number from the animaldata[300] array is printed, I need to print it's corresponding fooddata. (the 7 numbers).
0
efnCommented:
> the program is only searching the first element of the 300 element array for the animal number

That's because the loop never increments i.

Also, you probably don't want to say it's an invalid animal number for every animaldata element that doesn't match.  It would make more sense to do that once you have searched the whole array and not found a match.

--efn
0
Sys_ProgCommented:
You have not incremented i - the loop counter of the inner while loop

So, in the inner loop it checks for the 0th location of the animaldata array, if it matches, then it prints and quits the inner loop, else it prints an invalid animal number
This else part should not be here, first u should check for all the 300 locations

So your else part of inner loop should increment i.

After the inner while loop, u will have to figure out whether u found the animal, for this u can develop some logic - say keep a variable which gets set in the inner while loop when u find the aniimal, and then after the inner while ends u can check for this variable OR anything else. There can be many ways to do this

HTH
Amit


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.