Question

In: Computer Science

In C an array Numbers is declared float Numbers[10][10]. Due to a bug, the program tried...

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?

Solutions

Expert Solution

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:

  • First is visualization in terms of memory
  • second is apply some logic.

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]


Related Solutions

in C++, Write a program that asks the user to enter 6 numbers. Use an array...
in C++, Write a program that asks the user to enter 6 numbers. Use an array to store these numbers. Your program should then count the number of odd numbers, the number of even numbers, the negative, and positive numbers. At the end, your program should display all of these counts. Remember that 0 is neither negative or positive, so if a zero is entered it should not be counted as positive or negative. However, 0 is an even number....
Write a C program that prompt the user to enter 10 numbers andstores the numbers...
Write a C program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
Write a C++ program that prompt the user to enter 10 numbers andstores the numbers...
Write a C++ program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
Write a C program that asks the user to enter 15 integer numbers and then store them in the array.
Write a C program that asks the user to enter 15 integer numbers and then store them in the array. Then, the program will find the second largest element in array and its index without sorting the array. For example, In this array {-55,-2,1, 2, -3, 0, 5, 9, 13, 1, 4, 3, 2, 1, 0}, the second largest element is 9 [found at index 7].
write a c++ program that prompts a user to enter 10 numbers. this program should read...
write a c++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the two numbers and the average of the 10 numbers PS use file I/o and input error checking methods
Write a program in Easy68K: a) Define an array of numbers in the memory.
Write a program in Easy68K: a) Define an array of numbers in the memory. b) Read two numbers from keyboard. The first number is the size of the array and the second number is what index of the array you want to access. The index you entered can be larger than the array. c) Display the element indexed by (index % size) in the array. 
Write a C++ program that uses array to store the salaries of 10 employees working in...
Write a C++ program that uses array to store the salaries of 10 employees working in a small firm. The program should take average of the salaries and the max and min salaries being paid to the employees
Write a C program to Declare an integer array of size 10 with values initialized as...
Write a C program to Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9])...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT