*️⃣ For an array of real numbers RealArr [20][20], find the address of RealArr [10,12], if RealArr [1,1] is stored in location 1000. Assume each real number required 4 bytes. Show steps in your calculation. *️⃣
*️⃣ Class 12 - Computer Science *️⃣
*️⃣ Chapter - Array *️⃣
Answers
Answer:
#include <stdio.h>
int main() {
int number1;
int number2;
int number3;
int number4;
int number5;
number1 = 10;
number2 = 20;
number3 = 30;
number4 = 40;
number5 = 50;
printf( "number1: %d\n", number1);
printf( "number2: %d\n", number2);
printf( "number3: %d\n", number3);
printf( "number4: %d\n", number4);
printf( "number5: %d\n", number5);
}
It was simple, because we had to store just five integer numbers. Now let's assume we have to store 5000 integer numbers. Are we going to use 5000 variables?
To handle such situations, almost all the programming languages provide a concept called array. An array is a data structure, which can store a fixed-size collection of elements of the same data type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Instead of declaring individual variables, such as number1, number2, ..., number99, you just declare one array variable number of integer type and use number1[0], number1[1], and ..., number1[99] to represent individual variables. Here, 0, 1, 2, .....99 are index associated with var variable and they are being used to represent individual elements available in the array.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
Arrays in C
Create Arrays
To create an array variable in C, a programmer specifies the type of the elements and the number of elements to be stored in that array. Given below is a simple syntax to create an array in C programming −
type arrayName [ arraySize ];
This is called a single-dimensional array. The arraySize must be an integer constant greater than zero and type can be any valid C data type. For example, now to declare a 10-element array called number of type int, use this statement −
int number[10];
: A matrix B[10][20] is stored in the memory with each element requiring 2 bytes of storage. If the base address at B[2][1] is 2140, find the address of B[5][4] when the matrix is stored in Column Major Wise.
Address of [I, J]th element in column-major = B + W[R(J – Lc) + (I – Lr)]
= 2140 + 2[10(4 – 1) + (5 – 2)]
= 2140 + 2[10 × 3 + 3]
= 2140 + 2[30 + 3]
= 2140 + 2[33]
= 2140 + 66
= 2206