Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ pattern problem

Status
Not open for further replies.

Panchovia

Programmer
May 6, 2010
48
CW

The following code should produce the following pattern it doesn't.
XXXXX
XXXXX
XXXXX
XXXXX

what code must be added to get the correct result?

#include <iostream>
#include <conio.h>
#include <process.h>
#include <string.h>
using namespace std;

int main()
{
int i,j;


for( i=1 ; i <=5 ;i++ ){

for( j=1 ; j <=5 ;j++)

cout<<" x" <<endl;
}
system ("pause");

return 0;
 
You only need to output endl once every 5th character
Code:
for( i=1 ; i <=5 ;i++ ){
   for( j=1 ; j <=5 ;j++)
      cout<<" x";
   cout <<endl;
}
Nothing wrong with the way you have coded but if you are indexing through arrays, you might consider a slightly different way of coding
Code:
for( i=0 ; i < 5 ;i++ ){
   for( j=0 ; j < 5 ;j++)
      cout<<" x";
   cout <<endl;
}
The normal pattern is to go from start while the iterator is less than one iteration past the end.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top