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

Write a program that prompts the user for a positive number. The program then pr

ID: 3938439 • Letter: W

Question

Write a program that prompts the user for a positive number. The program then prints the pattern given below. Sample Run: "E\WORKSharjahTEACHINGN C++ programminghw2_ex1-Copyexe" Enter number of rows: 5 The number pattern is: 0 1 2 3 4 1 0 1 2 3 2 1 0 1 2 3 2 1 0 1 4 3 2 1 0 process returned 0(Times 0) execution time: 1.390 s press any key to continue "E\WORKSharjahTEACHINGN C++ programminghw2_ex1-Copyexe" Enter number of rows: 7 The number pattern is: 0 1 2 3 4 5 6 1 0 1 2 3 4 5 2 1 0 1 2 3 4 3 2 1 0 1 2 3 4 3 2 1 0 1 2 5 4 3 2 1 0 1 6 5 4 3 2 1 1 process returned 0(Times 0) execution time: 0.646 a press any key to continue

Explanation / Answer

#include <iostream>
using namespace std;
int main()
{
int n,i,j,t; // 'n' is for positive number , 'i','j' is for loop, 't' is temp variable
cout << "Enter the number of rows : ";
cin>>n;
if(n<1){
cout<< "Please enter positive number" <<endl;
return 0;
}
cout<<"The number pattern is : "<<endl;   
for(i=0; i<n; i++){
t=i;
for(j=0; j<n; j++)
{
if(j == 0)
{
cout<<t<<" ";
}
else
{
t = t-1;
cout<<abs(t)<<" ";
}
}
cout<<endl;
}
return 0;
}

Here's the explaination:
* Prepare two loop, one for rows and one for columns
* keep the row's number in temporary variable 't' and iterate the column's
* In each column's iteration, check if its the first column then print the
temp variable else decrement temp variable by 1 and prints its absolute value

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