Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started