In: Computer Science
C++ Language
Implement a two-dimensional image using a nested loop.
1) Create an integer constant DIM and set it equal to 5. 2) Use a nested loop to print the output in terms of DIM.
Example output:
***** *---* *---* *---* *****
Hi...
The following program is written and tested on online compiler jdoodle.
IF any doubts please comment...
From the question it is not clear that the pattern should be in a single line or in matrix form. So I have provided the code for single line. if it should be in matrix form u can uncomment the line 21 as shown in the screenshot. if still any doubt please comment.
code:
#include <iostream>
using namespace std;
int main() {
/* integer constant DIM and set it equal to 5 */
int DIM = 5;
/* variables to iterate throught the loop */
int i,j;
/* nested loop to print the pattern */
for(i=0; i<DIM; i++){
for(j=0; j<DIM; j++){
// check for the values where there is "*" to be printed
if(i==0 || j==0 || i==4 || j==4){
cout << '*'; // print star at the lines
}
else{
cout << "_"; // please check here given is underscore "_" or "-"
}
}
// cout <<"\n"; //un comment this line if your output should have new line after each line
}
}
output screenshot:
Thank you....! Please comment if any doubt