Question

In: Computer Science

Arrays in C, including Multi DimensionalArrays   - Which is more space efficient - a...

Arrays in C, including Multi Dimensional Arrays


   - Which is more space efficient - a 2 dimensional array (int array[10][10]) or an array of int pointers where each element of the array points to an int array (int *array[10], array[0] = int sub_array[10])? Why?

   - Given the declaration int array[10][10], using pointer arithematic and the address &array[0][0], how do you retrieve the value of the element at array[5][3]?

   - Given the declaration int array[10][10][10], using pointer arithematic and the address &array[0][0][0], how do you retrieve the value of the element at array[5][3][1]?

   - Given the following code, what does the print statement output?
       int foo[2][2][3] = {
   { {1,2,3}, {4,5,6} },
   { {7,8,9}, {10,11,12} }
   };

   printf("%d", foo[0][1][2]);

Solutions

Expert Solution

1)

array[10][10] requires 100 continuous memory locations to store them. But array of pointers requires just 100 memory locations. It need not to be continuous,

So array of pointers is efficient if memory doesn't have 100 continuous memory locations but 100 locations available in sparsed manner.

2)

array[5][3] = 5 is row, 3 is column.

i*rowSize+k =

i is the required row number, 5 in our case

rowSize is 10 in our case

k is the required column which is 3.

Each element occupies memory of its type. In our case it is int.

address(array[0][0]) + (5 * 10 + 3) * size(int)

base address + (each row contains 10 elements, we need 53rd element address of integer type)

4)

foo[0] is  { {1,2,3}, {4,5,6} } //It contains two elements indexed 0

foo[0][1] is {4,5,6} // So index 1 is second set of elements

foo[0][1][2] is 6 // Second element in the above set is 6 as it is 0 indexed

so it prints 6

3)

total number of rows * total number of columns * required row number + total number of third index * required column number + 1 will give the required element from the base addres

*(&array[0][0][0] + 10 * 10 * 5 + 10 * 3 + 1) as in answer 2


Related Solutions

Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
Suggest one way in which your Efficient frontiers could be made more efficient. What is the...
Suggest one way in which your Efficient frontiers could be made more efficient. What is the limitation of this suggestion?
Multi-Dimensional Arrays Create main class named MultiArray.    Create a method which outputs (prints) all the values...
Multi-Dimensional Arrays Create main class named MultiArray.    Create a method which outputs (prints) all the values in the array. Call this method from each of the other methods to illustrate the data in the array. Note: Be sure to populate, and update the values in the multi-array, and then print the contents of the multi-array (Don't just print the design patterns.) The goal is to practice navigating the multi-array. Declare a multi-dimensional array of integers, which is 10 rows of...
1. What are the strengths and weaknesses of mercantilism and liberalism? Which would be more efficient...
1. What are the strengths and weaknesses of mercantilism and liberalism? Which would be more efficient in addressing global economic issues? 2. What are the reasons states impose protectionists policies on other countries? 3. What are the advantages and disadvantages of state ownership of the economy and private ownership? 4. How important is a stable international monetary system to cooperation among countries trade relations? Why would this be considered a collective good? 5. What are the factors that multinational corporations...
Please use Java language to write an efficient function that compute the intersection of two arrays...
Please use Java language to write an efficient function that compute the intersection of two arrays of integers (not necessary sorted).
Program Assignment 1 C++ please Instructions This assignment will require the use of three arrays, which...
Program Assignment 1 C++ please Instructions This assignment will require the use of three arrays, which will be used in parallel. Create a program that keeps track of the sales of BBQ sauces for a company. The company makes several different types of sauces, Original, Sticky Sweet, Spicy, Sweet Heat, Hickory Bourbon and Smokey Mesquite. One array will contain the names of the different BBQ sauces. This array will be initialized from a text file with the 6 different names....
How to be more productive and efficient at work?
How to be more productive and efficient at work?
When are firms more efficient? Order the following firms from least efficient to most efficient. Michael...
When are firms more efficient? Order the following firms from least efficient to most efficient. Michael opens a food cart serving a type of food that is only slightly differentiate from his nearby competitors'. Kayla's cranberry bog produces one product and it sells it at market price. Comcast is the only high-speed Internet provider for a particular urban area. Javier's shoe store sells many types of shoes and operates with a lot of excess capacity.
How can there be more than one efficient distribution of goods? Which distribution of goods is...
How can there be more than one efficient distribution of goods? Which distribution of goods is "most efficient”?
Which of the following statements about angiosperm reproduction is FALSE? Animal pollination is more efficient than...
Which of the following statements about angiosperm reproduction is FALSE? Animal pollination is more efficient than wind pollination Fruits increase the success of dispersal because animals consume the seeds can survive digestive tracts There is a diverse array of flower shapes and colors, largely related to the mode of pollination The pollen grain goes through lots of rounds of mitosis to become the fruit
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT