Complete the program so that it takes in 4 integers (a, b, c, and d) as arguments, stores them in the 2D array nums, and then prints their row, column, and total sums. The program should print the integers according to this formula:
(a) (b) (a+b)
(c) (d) (c+d)
(a+c) (b+d) ((a+b)+(c+d)+(a+c)+(b+d))
// (a+b) is the first row's sum
// (c+d) is the second row's sum
// (a+c) is the first column's sum
// (b+d) is the second column's sum.
// ((a+b)+(c+d)+(a+c)+(b+d)) is the total sum of all rows and columns



Answer :

The program should print the integers according to this formula is

nums[0][0]=a;

nums[0][1]=b;

nums[1][0]=c;

nums[1][1]=d;

nums[0][2]= nums[0][0] + nums[0][1]; //a+b

nums[1][2]= nums[1][0] + nums[1][1]; //c+d

nums[2][0] = nums[0][0] + nums[1][0]; //a+c

nums[2][1] = nums[0][1] + nums[1][1]; //b+d

//sum = (a+b) + (c+d) + (a+c) + (b+d)

nums[2][2] = nums[0][2] + nums[1][2] + nums[2][0] + nums[2][1];

The nums array is 2 dimensional as shown below.

row, column column 0 column 1 column 2

row 0 a b a+b

row 1 c d c+d

row 2 a+c b+d (a+b)+(c+d)+(a+c)+(b+d)

The first index of an array always starts from 0.

Thus, nums[rows][columns] is used to access each element of the array.

The sum of first row elements is to be stored in the last index of row0, that is in nums[1][2].

Thus we equate nums[0][2] = nums[0][0] + nums[0][1]

Similarly, for 2nd row; nums[1][2] = nums[1][0] + nums[1][1]

The sum of first column is to be stored in last index of column0.

Thus, nums[2][0] = nums[0][0] + nums[1][0]

and for 2nd column,

nums[2][1] = nums[0][1] + nums[1][1].

Thus, the answer is--  

-------------------------------------------------------------------------------------------------------------------------------------

nums[0][0]=a;

nums[0][1]=b;

nums[1][0]=c;

nums[1][1]=d;

nums[0][2]= nums[0][0] + nums[0][1]; //a+b

nums[1][2]= nums[1][0] + nums[1][1]; //c+d

nums[2][0] = nums[0][0] + nums[1][0]; //a+c

nums[2][1] = nums[0][1] + nums[1][1]; //b+d

//sum = (a+b) + (c+d) + (a+c) + (b+d)

nums[2][2] = nums[0][2] + nums[1][2] + nums[2][0] + nums[2][1];

To learn more about  program visit:https://brainly.com/question/11023419

#SPJ4

Other Questions