Solved

Calling data from two dimensional array

Posted on 2003-11-02
10
274 Views
Last Modified: 2010-04-15
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?
               
0
Comment
Question by:Tabris42
10 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
Comment Utility
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
 
LVL 10

Expert Comment

by:Sys_Prog
Comment Utility
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
 

Author Comment

by:Tabris42
Comment Utility
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
 
LVL 10

Expert Comment

by:Sys_Prog
Comment Utility
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
 
LVL 10

Expert Comment

by:Sys_Prog
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 6

Expert Comment

by:Ajar
Comment Utility
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
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
>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
 

Author Comment

by:Tabris42
Comment Utility
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
 
LVL 15

Expert Comment

by:efn
Comment Utility
> 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
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 500 total points
Comment Utility
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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now