Tabris42
asked on
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?
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?
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]
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]
ASKER
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.
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.
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
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
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
}
}
It should == instead of =
i.e
for ( i = 0 ; i < 300; i++ )
if ( arr[i][0] == lookup ) {
YOUR PRINTF statements for Printing the data
}
}
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
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
>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*/
}
}
>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*/
}
}
ASKER
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).
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).
> 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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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