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

Write a java program creating an array of the numbers 1 through 10. Shuffle those numbers....
Write a java program creating an array of the numbers 1 through 10. Shuffle those numbers. Randomly pick a number between one and ten and then sequentially search the array for that number. Once the number is found, place that number at the top of the list. Print the list. Perform #3 thru #5 ten times.
A professor has constructed a 3-by-4 two-dimensional array of float numbers. This array currently contains the...
A professor has constructed a 3-by-4 two-dimensional array of float numbers. This array currently contains the lab grades, quiz grades and exam grades of three students in the professor’s class. Write a C++ program that calculate the final grade for each student and save it to the last column. You program should display the following output:             Lab    Quiz      Exam       Final     Student 1          ## ## ## ## Student 2          ## ## ## ## Student 3          ## ## ## ## The...
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....
Design a program that uses an array to store 10 randomly generated integer numbers in the...
Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: 1. Display 10 random numbers stored in the array 2. Compute and display the largest number in the array 3. Compute and display the average value of all numbers 4. Exit The options 2...
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.
11.2 Exercise 2: You are required to write a program that creates a float array of...
11.2 Exercise 2: You are required to write a program that creates a float array of size 90. Using a for loop, populate the array element whose index is the value of the loop variable with the value of the loop variable (e.g. array[0] = 0, array[1] = 1 etc.) Using a second loop display the loop index and the value in the array - each pair of numbers is to be displayed on a new line. Hi, could you...
In C++, write a program that will read up to 10 letters (characters) into an array...
In C++, write a program that will read up to 10 letters (characters) into an array and write the letters back to the screen in the reverse order. The program should prompt the user to input all values and can determine whether the input has ended by a punctuation mark such as ‘.’ (make sure that you’ll include meaningful and intuitive messages to the user). For example, if the input is abcde, the output should be edcba. Include appropriate messaging...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT