Question: Write a program in C++ that displays the main diagonal of its 2D array with commas but avoiding to put a comma in the last

Write a program in C++ that displays the main diagonal of its 2D array with commas but avoiding to put a comma in the last number.

Example:

1 2 3

4 5 6

8 9 7

output: 1, 5, 7 (Notice that the last number does not have any comma)

This is the code I have so far:

#include

int main(void) { int myArray[][4] = { {1,2,3,4}, {5,6,7,8}, {7,9,3,8} }; int width = 4, height = 3;

for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { std::cout << myArray[i][j] << ' '; } std::cout << std::endl; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!