Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following function prints a natural number sequence across the diagonal of a

ID: 3862657 • Letter: T

Question

The following function prints a natural number sequence across the diagonal of an n by n square matrix. Debug the code to fix all the compilation and run-time errors, so that the code generates the desired output. For instance, when the num value passed to the function is 5, the output would look like the following.

1****

*2***

**3**

***4*

****5

void naturalDiagonal(int num);
for (int i=0; i < num - 1; i++)
{
for (int j=1; j <= number; j++)
{
if i == j-1
{
cout << i;
}
else
{
cout << "*";
}
cout << endl;
}
}
}

Explanation / Answer

please find the final code

void naturalDiagonal(int num){
for (int i=0; i < num - 1; i++)
{
cout << endl;
for (int j=1; j <= num; j++)
{
if (i == j-1)
{
cout << i;
}
else
{
cout << "*";
}
}
}
cout<<endl;
}

i have made below changes

1)  for (int j=1; j <= number; j++) is changed to for (int j=1; j <= num; j++)

2) added a new cout statement at the second last line

3) also added cout<<endl; after the first for loop

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote