Solved

Calling data from two dimensional array

Posted on 2003-11-02
10
279 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
ID: 9669113
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
ID: 9669130
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
ID: 9669411
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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9669456
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
ID: 9669468
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
 
LVL 6

Expert Comment

by:Ajar
ID: 9669542
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
ID: 9669599
>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
ID: 9670744
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
ID: 9672459
> 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
ID: 9676210
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

821 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