60 314
Assignments Done
98,1%
Successfully Done
In April 2018

Answer to Question #50605 in C++ for Umer

Question #50605
Let’s say that you need to store 100 integers so that they’re easily accessible. However, let’s further assume that there’s a problem: The memory in your computer is so fragmented that the largest array that you can use holds only 10 integers.
(Such problems actually arise, although usually with larger memory objects.) You can solve this problem
by defining 10 separate int arrays of 10 integers each, and an array of 10 pointers to
these arrays. The int arrays can have names like a0, a1, a2, and so on. The address of
each of these arrays can be stored in the pointer array of type int*, which can have a
name like ap (for array of pointers). You can then access individual integers using
expressions like ap[j][k], where j steps through the pointers in ap and k steps through
individual integers in each array. This looks as if you’re accessing a two-dimensional
array, but it’s really a group of one-dimensional arrays.
Fill such a group of arrays with test data (say the numbers 0, 10, 20, and so on up to
990). Then
Expert's answer
// main.cpp -- stores 100 integer as 10 arrats of 10 integers
#include <iostream>
const int MAX_ARRAY_SIZE = 10;
int main()
{
    // Define 10 arrays
    int a0[MAX_ARRAY_SIZE],
        a1[MAX_ARRAY_SIZE],
        a2[MAX_ARRAY_SIZE],
        a3[MAX_ARRAY_SIZE],
        a4[MAX_ARRAY_SIZE],
        a5[MAX_ARRAY_SIZE],
        a6[MAX_ARRAY_SIZE],
        a7[MAX_ARRAY_SIZE],
        a8[MAX_ARRAY_SIZE],
        a9[MAX_ARRAY_SIZE];
    // Define array of pointers to arrays defined previously
    int *pa[MAX_ARRAY_SIZE] = {a0, a1, a2, a3, a4,
                               a5, a6, a7, a8, a9};
    // Filling the arrays with numbers 0, 10, 20, ..., 990
    for (int i = 0; i < MAX_ARRAY_SIZE * MAX_ARRAY_SIZE; ++i)
    {
        pa[i / MAX_ARRAY_SIZE][i % MAX_ARRAY_SIZE] = i * 10;
    }
    // Print arrays
    for (int i = 0; i < MAX_ARRAY_SIZE * MAX_ARRAY_SIZE; ++i)
        std::cout << pa[i / MAX_ARRAY_SIZE][i % MAX_ARRAY_SIZE] << " ";
    std::cout << std::endl;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions