Question

In: Computer Science

What are the contents of the array after the for-loop in the following code?

(IN C)


What are the contents of the array after the for-loop in the following code?

int array[ SIZE ][ SIZE ] = { { 4, 5, 6, 7, 8 },

{ 1, 2, 3, 4, 5 },

{ 3, 6, 7, 8, 9 },

{ 2, 3, 4, 5, 6 },

{ 5, 6, 7, 8, 9 } };

int i;

int *ptr = array[ 0 ];

for( i = 0; i < SIZE * SIZE; i++ ) {

if( i % SIZE < 2 ) {

*( ptr + i ) = 0;

}

else if( i % SIZE == 2 ) {

*( ptr + i * SIZE ) = 100;

}

}

Solutions

Expert Solution

if( i % SIZE < 2 ) {

          *( ptr + i ) = 0;

}

The above condition make the memory address 0,1,5,6,10,11,15,16,20,21 is set to 0

else if( i % SIZE == 2 ) {

          *( ptr + i * SIZE ) = 100;

}

The above condition is satisfied when i=2,7,12,17,22

the index which are changed is 10,35,60,85,100

but the index 10 is overwritten by first if condition to 0

So the final output is


Related Solutions

Show the contents of the array after the fourth iteration of selectionSort Your answer should be...
Show the contents of the array after the fourth iteration of selectionSort Your answer should be 4 lines The array values horizontally for iteration 1 The array values horizontally for iteration 2 The array values horizontally for iteration 3 The array values horizontally for iteration 4
Study the following code with a while-loop and convert it to a for-loop (fill in the...
Study the following code with a while-loop and convert it to a for-loop (fill in the blanks). int i=4, result=1; while(i>1) { result *= i; i--; } The following for-loop performs the same functionality: int result=1; for (__________ i=4; i _________1;____________) { result *= i; }
After three passes through the outer loop of a sorting algorithm, an array changes from 43...
After three passes through the outer loop of a sorting algorithm, an array changes from 43 16 48 37 81 54 71 29 to 16 29 37 48 81 54 71 43 What sorting algorithm is being used to sort the array? A. Selection sort B. Bubble sort C. Insertion sort
After the following statements execute, what are the contents of the deque? DequeInterface<String> myDeque = new...
After the following statements execute, what are the contents of the deque? DequeInterface<String> myDeque = new LinkedDeque<>(); myDeque.addToFront("Jim"); myDeque.addToFront("Jess"); myDeque.addToBack("Jill"); myDeque.addToBack("Jane"); String name = myDeque.removeFront(); myDeque.addToBack(name); myDeque.addToBack(myDeque.getFront()); myDeque.addToFront(myDeque.removeBack()); myDeque.addToFront(myDeque.getBack()); I need to get this as an output: Jim Jess Jim Jess Jim Jill Jess Jim Jill Jane Jim Jill Jane Jim Jill Jane Jess Jim Jill Jane Jess Jim Jim Jim Jill Jane Jess Jess Jim Jim Jane Jess Help me please
Thinking in Assembly language What values will be written to the array when the following code...
Thinking in Assembly language What values will be written to the array when the following code executes? .data array DWORD 4 DUP(0) .code main PROC mov eax,10 mov esi,0 call proc_1 add esi,4 add eax,10 mov array[esi],eax INVOKE ExitProcess,0 main ENDP proc_1 PROC call proc_2 add esi,4 add eax,10 mov array[esi],eax ret proc_1 ENDP proc_2 PROC call proc_3 add esi,4 add eax,10 mov array[esi],eax ret proc_2 ENDP proc_3 PROC mov array[esi],eax ret proc_3 ENDP
What is the ouput of the following code? void loop(int num) { for(int i = 1;...
What is the ouput of the following code? void loop(int num) { for(int i = 1; i < num; ++i) { for(int j = 0; j < 5; ++j) { cout << j; } } } int main() { loop(3); return 0; }
Translate the following C code into MIPS Assembly code, assuming Loop Variable k is in $s0...
Translate the following C code into MIPS Assembly code, assuming Loop Variable k is in $s0 and initially containing 0 . Also assume base of array Arr is in $s3 while ( k < = 10 ) { Arr[k] = k ; k = k + 1; }
1. Adapt the custom array list implementation code with the following changes: (a) Add code to...
1. Adapt the custom array list implementation code with the following changes: (a) Add code to the ensureCapacity() method to print out a message including how many elements are copied to the new array on resizing Array List Implementation: public class MyArrayList<E> implements MyList<E> { public static final int INITIAL_CAPACITY = 16; private E[] data = (E[])new Object[INITIAL_CAPACITY]; private int size = 0; // Number of elements in the list public MyArrayList() { }    public MyArrayList(E[] objects) { for...
In C programming: Write a program that initializes an array-of-double and then copies the contents of...
In C programming: Write a program that initializes an array-of-double and then copies the contents of the array into another arrays. To make the copy, use a function with array notation. This function takes two arguments the name of the target array and the number of elements to be copied. That is, the function calls would look like this, given the following declarations: double source[5] ={1.1, 2.2, 3.3., 4.4, 5.5}; double target1[5]; double target2[5]; copyarr(source, target1, 5);
Write a method that takes an integer array as its parameter and sorts the contents of...
Write a method that takes an integer array as its parameter and sorts the contents of the array in ascending order using the Insertion Sort algorithm. Call this method after the original array and other stats have been displayed. Once the array has been sorted by your method, display its contents to the screen in the same manner as the original array was displayed. CODE SO FAR: import java.util.*; public class ArrayInteger { public static void getRandomNumber(int A[],int n){ Random...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT