Question

In: Computer Science

What would the output be for the following program code segment? #define ROWS  3      #define COLS 3...

What would the output be for the following program code segment?

#define ROWS  3     

#define COLS 3

void addCoulmns(int arr[][COLS],int Sum[COLS]);

int main(void)

{

       int array[ROWS][COLS] = {{ 10, 35, 23 },

                                  { 5, 4, 57 },

                                  { 4, 7, 21 }};

       int ColSum[ROWS];

       int rows;

       int columns;

       addCoulmns(array,ColSum);

       for (columns = 0; columns < COLS - 1; columns++)

              printf("%d\n",ColSum[columns]);

       return(0);

}

void addCoulmns(int arr[][COLS],int Sum[COLS])

{

       int row;

       int col;

       for (col = 0; col < COLS - 1; col++)

       {

              Sum[col] = 0;

              for (row = 0; row < ROWS - 1; row++)

                      Sum[col] += arr[row][col];

       }

       return;

}

Solutions

Expert Solution

Output:

15
39

Short Explanation:

In main function we are calling a function called addCoulmns(array,ColSum) here ColSum is a array of integer

Inside the addCoulmns function there is a for loop which iterate till ROWS-1 which means 3-1=2, So in first iteration Sum[col] which is equivalent to ColSum 1st iteration of outer loop and 1st of inner loop row=0,col=0 so Sum[0]=arr[0][0]+ Sum[0] so Sum[0]=10+0 //(0 because we have initialize Sum[]=0 at first). 2nd iteration of inner loop row=1 col=0 Sum[0]=arr[1][0]+Sum[0] which is Sum[0]=5+10=15

Therefore Sum[0]=15 and now 2nd iteration of outer loop 1st iteration of inner loop will be row=0 col=1 Sum[1]=arr[0][1]+Sum[1] which is Sum[1]=35+0 so Sum[1]=35 now 2nd iteration in inner for loop row=1 col=1 , Sum[1]=arr[1][1]+Sum[1] which is Sum[1]=4+35=39

Sum[0]=15 Sum[1]=39

Inside main() again

COLS has value 3 we are iterating in for loop which is in main up to COLS-1 which means 3-1=2. So the loop will iterate 2 times, Inside for loop we have print statement which prints a value which is most probably integer printing value of array named ColSum which is equivalent to Sum so ColSum has value 15 39

Col[0]=15 Col[1]=39


Related Solutions

Fill in the blanks of the following segment of code, so that the output would be 1 3 4.
Fill in the blanks of the following segment of code, so that the output would be 1 3 4.int count = 0;do{++ count;if (count == 2)Blank;cout << count << " ";} while (count <= Blank);cout << endl;
Given the following code segment, tell the output if it is part of an executable program....
Given the following code segment, tell the output if it is part of an executable program. Assume the addresses of the variables, a and b, are 0012A32A and 0012A340, respectively.        int* intPtr1, *intPtr2;        int a = 10, b;        intPtr1 = &a;        cout<< "The data in a is "<<a<<endl;        cout << "The address of a is "<<&a<<endl;        cout <<"The data in intPtr1 is "<< intPtr1 <<endl;        cout <<"The data in the variable pointed by...
21. What is the output of the following segment of code if 7 is input by...
21. What is the output of the following segment of code if 7 is input by the user when asked to enter a number?____________ int num; int total = 0; cout << "Enter a number from 1 to 10: "; cin >> num; switch (num) { case 1: case 2: total = 5; case 3: total = 10; case 7: total = total + 3; case 8: total = total + 6; default: total = total + 4; } cout...
no need to explain 11. What is output by the following code segment? boolean isHighTemp =...
no need to explain 11. What is output by the following code segment? boolean isHighTemp = true; double degree = 100.01; if (isHighTemp) if(degree >= 110) System.out.print(“Extremely Hot”); else System.out.println(“Not Hot”); A) Extremely Hot B) Not Hot C) Hot D) Extremely Hot Not Hot 12. To produce the output 2 4 6 8 10, what is the loop condition for the following loop? int n = 0; Page 3 do { n = n + 2; System.out.print(n + “ ”);...
what is the expected output of the following segment of code? dq = Deque() dq.push_back('M') dq.push_back('...
what is the expected output of the following segment of code? dq = Deque() dq.push_back('M') dq.push_back(' ') l = ['A', 'L', 'E', 'S', 'T', 'E'] for i in l: → if ord(i) % 3 == 0: → → dq.push_back(i) → elif ord(i) % 5 == 0: → → dq.push_front(i) → → dq.push_back(i) → elif ord(i) % 4 == 0: → → dq.push_back(i) → else: → → dq.peek_front() → → dq.push_front('X') → → dq.push_back('R') dq.push_front(dq.peek_back()) dq.pop_back() print(dq)
What will be the expected output of the following pseudo code? Write exactly what would display...
What will be the expected output of the following pseudo code? Write exactly what would display when you execute the statements. Module main() Declare Integer a = 5 Declare Integer b = 2 Declare Integer c = 3 Declare Integer result = 0 Display "The value of result is" Display result Set result = a + b * c - a Display "Changed value is: ", result End Module
Show the output of the following code segment. int count=0;                         for (int i=2; i <=...
Show the output of the following code segment. int count=0;                         for (int i=2; i <= 4; i++ ) {                                     StdOut.println(i);                                     for (int j=1; j <3; j++) {                                                 count++;                                                 StdOut.println(i +" " + j +" "+ count);                                     }                         } count i j I print 0 2 1 3 1 1 1 2 3 2 2 3 3 3 3 4 3 Show the output of the function call statements shown.             double x =...
C++ program. Please explain how the code resulted in the output. The code and output is...
C++ program. Please explain how the code resulted in the output. The code and output is listed below. Code: #include <iostream> #include <string> using namespace std; int f(int& a, int b) {    int tmp = a;    a = b;    if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }    b = tmp;    return b;    return a; } int main() {    int a...
a. What will be the output of LINE A in code “a” and output of code...
a. What will be the output of LINE A in code “a” and output of code “b”? Also write reasons for the outputs. a. #include <sys/types.h> #include <stdio.h> #include <unistd.h> int value = 3; int main() { pid_t pid; pid = fork(); if (pid = = 0) {/* child process */} value += 233; return 0; } else if (pid > 0) {/* parent process */} wait(NULL); printf(“PARENT: value = %d”, value); /* LINE A */ return 0; } }...
1. What does the following program do? 2. What output does the program produce? 3. Examine...
1. What does the following program do? 2. What output does the program produce? 3. Examine the program code carefully. Is the program functioning correctly? 4. If you do not think the program is working correctly, describe why? 5. Include one screenshot of the program's output. C++ PROGRAM: #include <iostream> #include <pthread.h> #include <stdlib.h> int count; void* myFunction(void* arg) { int actual_arg = *((int*) arg);    for(unsigned int i = 0; i < 10; ++i) { count++; std::cout << "Thread...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT