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

1) What's the contents of array vals after the following code is executed? int[] vals =...
1) What's the contents of array vals after the following code is executed? int[] vals = {6, 1, 4, 5, 1}; int size = vals.length; for(int i = 0; i < size; i++) vals[i] = vals[i] * i; q) [ 6, 1, 4, 5, 1 ] b)[ 0, 1, 2, 3, 4] c)[0, 1, 8, 15, 4] d)[6, 2, 12, 20, 5] 2) Consider function foo defined below: static int[] foo(int vals[]) { int result[] = new int[vals.length - 1];...
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 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
Question 110 pts What are the contents of the queue bankLine after the following statements execute?...
Question 110 pts What are the contents of the queue bankLine after the following statements execute? The front of the queue in the answers is the left-most value QueueInterface<String> bankLine = newLinkedQueue<>(); bankLine .enqueue(“John”); bankLine .enqueue(“Matthew”); String next = bankLine .dequeue(); bankLine .enqueue(“Drew”); bankLine .enqueue(“Heather”); bankLine .enqueue(“David”); next = bankLine .dequeue(); John, Drew, Heather Heather, John, Drew Drew, Heather, David David, Heather, Drew Flag this Question Question 210 pts What are the contents of the deque waitingLine after the following...
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
Please code C# Convert the following for loop into a while loop: for(int count = 8;...
Please code C# Convert the following for loop into a while loop: for(int count = 8; count > 0; count--) { Console.WriteLine(count); }
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; }
True or False: It's easy to loop through an array using a for loop in C++...
True or False: It's easy to loop through an array using a for loop in C++ because you can use the .size() function to get the total number of spaces in the array True or False: It's easy to loop through a vector using a for loop in C++ because you can use the .size() function to get the total number of spaces in the vector
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT