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
python Write a Loop class. The contents of a loop object are represented internally as a...
python Write a Loop class. The contents of a loop object are represented internally as a simple python list with the following additional instance variables: loop - a python list containing the elements of the loop list head - which stores the index of the first element of the loop list current - which stores the index of the last element of the loop list size - which stores how many actual elements are in the loop max - which...
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; }
Linked List: Complete the following code to create a linked list from an Array. After creating...
Linked List: Complete the following code to create a linked list from an Array. After creating the list, display the elements of the linked list iteratively. Write two others function called as RDisplayTailRecursion(first) and RDisplayTailRecursion(first) which will print elements of the linked list using the tail and head recursions respectively. #include <stdio.h> #include <stdlib.h> struct Node { }*first=NULL; void create(int A[], int n) { for(i=1; i<n; i++) { } } void Display(struct Node*p) { while(p!=NULL) { } } void RDisplayTailRecursion...
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); }
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; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT