Q.1 #include<iostream> using namespace std; int main() { int sum, k, i, j; int x
ID: 3817037 • Letter: Q
Question
Q.1
#include<iostream>
using namespace std;
int main() {
int sum, k, i, j;
int x[][4]={1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 3, 2, 1, 7, 1};
sum = 0;
for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; ++j)
if(x[i][j] > x[j][i])
sum += x[i][j];
cout << "the sum is = " << sum << endl;
system("pause");
return 0;
}
OUTPUT=41
HOW TO TRACE THIS PROGRAM I DID NOT UNDERSTAND HOW WE GOT 41 DOES X[J][I] BECOME A different array or what?
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Q2.
int main() {
int sum, k, i, j;
int x[][4]={1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 3, 2, 1, 7, 1};
sum = 0;
for (i = 1; i <= 3; i++)
for (j = 0; j <= 3; ++j)
if(x[i][j] > x[i-1][j])
sum++;
cout << "the sum is = " << sum << endl;
system("pause");
return 0;
ALSO THIS ONE THE ANSWER IS 6 HOW? PLEASE CLEAR EXPLANATION
Explanation / Answer
Q1,
#include<iostream>
using namespace std;
int main() {
int sum, k, i, j; //Declares the variables k, i, and j.
int x[][4]={1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 3, 2, 1, 7, 1}; //Defines the array x.
sum = 0; //Assigns 0 to sum.
for (i = 0; i <= 3; i++) //This loop runs for i values of 0, 1, 2, and 3.
for (j = 0; j <= 3; ++j) //This loop runs for j values of 0, 1, 2, and 3.
if(x[i][j] > x[j][i]) //If the (i, j) positioned element is greater than its transpose counterpart.
sum += x[i][j]; //Add it to sum.
//Now, here is the loop values explained:
The matrix looks like:
1 2 3 4
5 6 7 8
9 8 7 3
2 1 7 1
i j x[i, j] x[j, i] Condition CumulativeSum
0 0 1 1 F 0
0 1 2 5 F 0
0 2 3 9 F 0
0 3 4 2 T 4
1 0 5 2 T 9
1 1 6 6 F 9
1 2 7 8 F 9
1 3 8 1 T 17
2 0 9 3 T 26
2 1 8 7 T 34
2 2 7 7 F 34
2 3 3 7 F 34
3 0 2 4 F 34
3 1 1 8 F 34
3 2 7 3 T 41
3 3 1 1 F 41
cout << "the sum is = " << sum << endl; //Therefore, this statement will print value 41.
system("pause");
return 0;
}
Q2:
int main() {
int sum, k, i, j; //Declares the variables k, i, and j.
int x[][4]={1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 3, 2, 1, 7, 1}; //Defines the array x.
sum = 0; //Assigns 0 to sum.
for (i = 1; i <= 3; i++) //This loop runs for i values of 1, 2, and 3.
for (j = 0; j <= 3; ++j) //This loop runs for j values of 0, 1, 2, and 3.
if(x[i][j] > x[i-1][j]) //If the (i, j) positioned element is greater than its previous row, previous same column values.
sum++; //Increment counter.
//Now, here is the loop values explained:
The matrix looks like:
1 2 3 4
5 6 7 8
9 8 7 3
2 1 7 1
i j x[i, j] x[i-1, j] Condition CumulativeSum
1 0 5 1 T 1
1 1 6 2 T 2
1 2 7 3 T 3
1 3 8 4 T 4
2 0 9 5 T 5
2 1 8 6 T 6
2 2 7 7 F 6
2 3 3 8 F 6
3 0 2 9 F 6
3 1 1 8 F 6
3 2 7 7 F 6
3 3 1 3 F 6
cout << "the sum is = " << sum << endl; //Therefore, this statement will print value 6.
system("pause");
return 0;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.