In: Computer Science
In C an array Numbers is declared float Numbers[10][10].
Due to a bug, the program tried to reference Numbers[11][-13]. Which element of Numbers will actually be accessed?
In order to understand how to solve above problem, one should have a knowledge of the array.
In C programming, the array is the collection of homogenous items of the same type and items can be accessed using array indexes.
Code of given problem:
#include <stdio.h>
#include <conio.h>
int main()
{
/*
Columns -> 0 1 2 3 4 5 6 7 8 9
Rows
0 {1.235,2.334,3.4335,4.34,5.233,6.3745,7.234,8.234,9.23343,10.34523},
1 {1.123,2.435,3.655,4.444,5.23533,6.33445,7.2534,8.2634,9.26,10.2435},
2 {1.2233,2.3344,3.4455,4.364,5.2833,6.3495,7.2434,8.2344,9.2333,103.23},
3 {1.1223,2.3344,34.454,4.3445,5.26373,6.65345,7.23334,8.2234,934.233,10.623},
4 {1.1223,2.3334,44.4325,4.33434,5.4233,6.33345,74.234,85.234,93.233,103.23},
5 {1.3223,2.334,34.4,44.34,5.4233,6.4345,75.234,8.5234,9.52353,10.523},
6 {1.223,2.3343,45.45,44.534,5.5233,65.345,75.2364,8.2364,9.6233,160.23},
7 {1.4253,2.354,434.45,45.34,55.233,66.345,76.2734,8.6234,9.5233,105.23},
8 {1.423,2.434,36.45,4.534,54.233,6.34,7.32334,8.3234,9.3233,130.23},
9 {12.23,2.334,12.45,4.334,53.233,6.3345,73.234,878.234,914.233,14.23},
*/
//declaration of array
float Number[10][10] =
{
{1.23,2.34,3.45,4.34,5.233,6.345,7.234,8.234,9.233,10.23},
{1.123,2.45,3.655,4.444,5.23533,6.33445,7.2534,8.2634,9.26,10.2435},
{1.2233,2.3344,3.4455,4.364,5.2833,6.3495,7.2434,8.2344,9.2333,103.23},
{1.1223,2.3344,34.454,4.3445,5.26373,6.65345,7.23334,8.2234,934.233,10.623},
{1.1223,2.3334,44.4325,4.33434,5.4233,6.33345,74.234,85.234,93.233,103.23},
{1.3223,2.334,34.4,44.34,5.4233,6.4345,75.234,8.5234,9.52353,10.523},
{1.223,2.3343,45.45,44.534,5.5233,65.345,75.2364,8.2364,9.6233,160.23},
{1.4253,2.354,434.45,45.34,55.233,66.345,76.2734,8.6234,9.5233,105.23},
{1.423,2.434,36.45,4.534,54.233,6.34,7.32334,8.3234,9.3233,130.23},
{12.23,2.334,12.45,4.334,53.233,6.3345,73.234,878.234,914.233,14.23},
};
printf("%f", Number[11][-13]);
}
here's the output:
so, Number[9][7] will actually be accessed if due to bug, the program tried to reference Number[11][-13] (as Number[9][7] have the same value as for Number[11][-13] i.e 878.234 )
Now, the question is how?
Two ways to solve this problem:
Visualization in terms of memory , result is:
according to some logic:
Basically, its the concept of negative indexing of array in C i.e
#include <stdio.h>
#include <conio.h>
int main()
{
/*
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 <- negative indexing
Columns -> 0 1 2 3 4 5 6 7 8 9
Rows
0 {1.235,2.334,3.4335,4.34,5.233,6.3745,7.234,8.234,9.23343,10.34523},
1 {1.123,2.435,3.655,4.444,5.23533,6.33445,7.2534,8.2634,9.26,10.2435},
2 {1.2233,2.3344,3.4455,4.364,5.2833,6.3495,7.2434,8.2344,9.2333,103.23},
3 {1.1223,2.3344,34.454,4.3445,5.26373,6.65345,7.23334,8.2234,934.233,10.623},
4 {1.1223,2.3334,44.4325,4.33434,5.4233,6.33345,74.234,85.234,93.233,103.23},
5 {1.3223,2.334,34.4,44.34,5.4233,6.4345,75.234,8.5234,9.52353,10.523},
6 {1.223,2.3343,45.45,44.534,5.5233,65.345,75.2364,8.2364,9.6233,160.23},
7 {1.4253,2.354,434.45,45.34,55.233,66.345,76.2734,8.6234,9.5233,105.23},
8 {1.423,2.434,36.45,4.534,54.233,6.34,7.32334,8.3234,9.3233,130.23},
9 {12.23,2.334,12.45,4.334,53.233,6.3345,73.234,878.234,914.233,14.23},
*/
//declaration of array
float Number[10][10] =
{
{1.23,2.34,3.45,4.34,5.233,6.345,7.234,8.234,9.233,10.23},
{1.123,2.45,3.655,4.444,5.23533,6.33445,7.2534,8.2634,9.26,10.2435},
{1.2233,2.3344,3.4455,4.364,5.2833,6.3495,7.2434,8.2344,9.2333,103.23},
{1.1223,2.3344,34.454,4.3445,5.26373,6.65345,7.23334,8.2234,934.233,10.623},
{1.1223,2.3334,44.4325,4.33434,5.4233,6.33345,74.234,85.234,93.233,103.23},
{1.3223,2.334,34.4,44.34,5.4233,6.4345,75.234,8.5234,9.52353,10.523},
{1.223,2.3343,45.45,44.534,5.5233,65.345,75.2364,8.2364,9.6233,160.23},
{1.4253,2.354,434.45,45.34,55.233,66.345,76.2734,8.6234,9.5233,105.23},
{1.423,2.434,36.45,4.534,54.233,6.34,7.32334,8.3234,9.3233,130.23},
{12.23,2.334,12.45,4.334,53.233,6.3345,73.234,878.234,914.233,14.23},
};
printf("%f", Number[1][-3]);
}
Output be like:
i.e Number[1][-3] are same as Number[0][7].
similarly for Number[11][-13]:
=> Number[11][-13]
=> Number[11][-10 - 3] ( up to element -10 is itself a s whole row)
=> Number[10][-3]
=> Number[10][-10 + 7]
=> Number[9][7]
hence, this is how Number[11][-13] == Number[9][7]