for loop ++1 increment

print out 8 rows and 8 columns using the increment

Page 1 of 1

9 Replies - 829 Views - Last Post: 15 October 2009 - 12:34 PM Rate Topic: -----

#1 dadon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 25-September 09

for loop ++1 increment

Posted 15 October 2009 - 05:18 AM

I want to print out a checkboard that will look like this when you compile it. using the increment is a most.

It has 8 rows and 8 columns.
  ----------------------- 
  | | | | | | | | | | | | | 
 ----------------------- 
  | | | | | | | | | | | | | 
----------------------- 
  | | | | | | | | | | | | | 
 ----------------------- 
  | | | | | | | | | | | | 
----------------------- 
  | | | | | | | | | | | | | 
 ----------------------- 
  | | | | | | | | | | | | | 
----------------------- 
  | | | | | | | | | | | | | 
 ----------------------- 
  | | | | | | | | | | | | 


This is the code i have been trying to use in printing it out
 cout<<"8 rows and 8 columns"<<endl;

for (int cntr =8 rows; cntr=cntr++)endl;
{
cout<<cntr<<"-";
if (cntr % 8==)


give me an example of the right code to use....please, if you can .

Is This A Good Question/Topic? 0
  • +

Replies To: for loop ++1 increment

#2 Smurphy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 35
  • View blog
  • Posts: 367
  • Joined: 07-July 08

Re: for loop ++1 increment

Posted 15 October 2009 - 06:21 AM

Let me just get you started by showing what a for loop should look like.
for( int i = 0; i < Variable; i++)
{
	 //Do whatever here
}


Now how this works is you give the for loop three things. That first variable is a counter. (i = 0) It can really store whatever but for our purposes 0. The second part is your condition (i < Variable) WHILE it is true you continue it can also be any other logical thing like <=, >=, >. The third thing is your incremental (i++) Each time the loop goes it will add 1 to i until the middle condition is true. Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 ajwsurfer  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 373
  • Joined: 24-October 06

Re: for loop ++1 increment

Posted 15 October 2009 - 06:47 AM

This is a quick write and I didn't debug it or test it in any way, but it should get you close to what you are looking for.

void printRow(char ch, int len) {
  for (int i = 0; i < len; i++) 
	cout << ch; 
  cout << endl;
}

void printAlternatingRow(char ch, int len) {
  bool flag = true;
  for (int i = 0; i < len; i++) {
	if (true == flag) {
		cout << ch;
		flag = false;
	} else {
	  cout << ' ';
	  flag = true;
	}	  
  cout << endl;
}

void printBoard() {
  int numberOfRows = 17;
  int numberOfColumns = 25;
  bool flag = true;
  for (int i = 0; i < numberOfRows; i++) {
	if (true == flag) {
	  printRow('-', numberOfColumns);
	  flag = false;
	} else {
	  printAlternatingRow('|', numberOfColumns);
	  flag = true;
	}	
}  


This post has been edited by ajwsurfer: 15 October 2009 - 06:49 AM

Was This Post Helpful? 0
  • +
  • -

#4 dadon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 25-September 09

Re: for loop ++1 increment

Posted 15 October 2009 - 07:52 AM

View PostSmurphy, on 15 Oct, 2009 - 05:21 AM, said:

Let me just get you started by showing what a for loop should look like.
for( int i = 0; i < Variable; i++)
{
	 //Do whatever here
}


Now how this works is you give the for loop three things. That first variable is a counter. (i = 0) It can really store whatever but for our purposes 0. The second part is your condition (i < Variable) WHILE it is true you continue it can also be any other logical thing like <=, >=, >. The third thing is your incremental (i++) Each time the loop goes it will add 1 to i until the middle condition is true. Hope this helps.




  This what i came up with but can not compile because of error from line 28.

This is the code, please try and compile for you know what i am talking about.

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main ()
{
 
	  //variable declaration
	  
	 int numberOfRows = 8;
	  int numberOfColumns = 8;
	  bool flag = true;
	 
	for (int i = 0; i < numberOfRows; i++) {
		if (true == flag) {
		  ('-', numberOfColumns);
		  flag = false;
		} else {
		  ('|', numberOfColumns);
		  flag = true;
		}	
{	 
	 //Freeze output window and halt
		cout << endl;
		system("Pause");
		return 0;  
	}
	



[code]
This is what i came up with, and please compile and see what i am talking about.


#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main ()
{

//variable declaration

int numberOfRows = 8;
int numberOfColumns = 8;
bool flag = true;

for (int i = 0; i < numberOfRows; i++) {
if (true == flag) {
('-', numberOfColumns);
flag = false;
} else {
('|', numberOfColumns);
flag = true;
}
{
//Freeze output window and halt
cout << endl;
system("Pause");
return 0;
}




[code]
Was This Post Helpful? 0
  • +
  • -

#5 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 119
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: for loop ++1 increment

Posted 15 October 2009 - 09:08 AM

You're not *that* far away.


View Postdadon, on 15 Oct, 2009 - 06:52 AM, said:

	for (int i = 0; i < numberOfRows; i++) {


You're iterating over the rows. But you also have to iterate over the columns. What you want to do is for each line draw each column, and depending on the line draw either "--", or "| ".

View Postdadon, on 15 Oct, 2009 - 06:52 AM, said:

('-', numberOfColumns);


This will obviously do nothing (I'm guessing you took that from ajwsurfer's code). Here, you want to print the character, so do that.

Quote

This what i came up with but can not compile because of error from line 28.


Make sure you balance the curly braces correctly. Pay extra attention as to where you put them (that's the whole point of the exercise); in particular, think of where the "cout << endl;" should be. Also, if you use the "flag" to switch between dashes and pipes, make sure the flag is set for the line, not after every character.
Was This Post Helpful? 0
  • +
  • -

#6 dadon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 25-September 09

Re: for loop ++1 increment

Posted 15 October 2009 - 10:22 AM

It si not compiling why? please, somebody help me pls

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main ()
{
 
      //variable declaration
      
      int numberOfRows = 8;
      int numberOfColumns = 8;
      bool flag = true;
     
    for (int i = 0; i < numberOfRows; i++) {
        if (true == flag) {
          ('--', numberOfRows);
          flag = false;
        }  else {
     for (int i = 0; i < numberOfColumns ; i++) { 
          if (true == flag) { 
        ('|', numberOfColumns);
        flag = false;
        }else{     
    
     //Freeze output window and halt
        cout << endl;
        system("Pause");
        return 0;  
    }
   

Was This Post Helpful? 0
  • +
  • -

#7 ajwsurfer  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 373
  • Joined: 24-October 06

Re: for loop ++1 increment

Posted 15 October 2009 - 10:37 AM

Try this:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;



void printRow(char ch, int len) {
  for (int i = 0; i < len; i++)
	cout << ch;
  cout << endl;
}

void printAlternatingRow(char ch, int len) {
  bool flag = true;
  for (int i = 0; i < len; i++) {
	if (true == flag) {
		cout << ch;
		flag = false;
	} else {
	  cout << ' ';
	  flag = true;
	}	  
  }	
  cout << endl;
}

void printBoard() {
  int numberOfRows = 17;
  int numberOfColumns = 25;
  bool flag = true;
  for (int i = 0; i < numberOfRows; i++) {
	if (true == flag) {
	  printRow('-', numberOfColumns);
	  flag = false;
	} else {
	  printAlternatingRow('|', numberOfColumns);
	  flag = true;
	}   
  }	
}  

int main ()
{
	printBoard();
	//Freeze output window and halt
	cout << endl;
	//system("Pause");
	return 0;
}



;)
Was This Post Helpful? 0
  • +
  • -

#8 ajwsurfer  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 373
  • Joined: 24-October 06

Re: for loop ++1 increment

Posted 15 October 2009 - 10:50 AM

You could do it this way also, but it is way more confusing. By separating each problems solution into a separate function the code is much easier to deal with all the way around.

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;



int main () {
  int numberOfRows = 17;
  int numberOfColumns = 25;
  bool flag = true;
  for (int i = 0; i < numberOfRows; i++) {
	if (true == flag) {
	  for (int j = 0; j < numberOfColumns; j++) cout << '-';
	  cout << endl;
	  flag = false;
	} else {
	  bool flagSecondLevel = true;
	  for (int k = 0; k < numberOfColumns; k++) {
		if (true == flagSecondLevel) {
		  cout << '|';
		  flagSecondLevel = false;
		} else {
		  cout << ' ';
		  flagSecondLevel = true;
		}
	  }
	  cout << endl;
	  flag = true;
	}	 
  }	
  cout << endl;	  
  //Freeze output window and halt
  cout << endl;
  //system("Pause");
  return 0;
}


Was This Post Helpful? 0
  • +
  • -

#9 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 119
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: for loop ++1 increment

Posted 15 October 2009 - 11:00 AM

View Postdadon, on 15 Oct, 2009 - 09:22 AM, said:

It si not compiling why? please, somebody help me pls


I'm ready to help, but you have to read people's comments, otherwise you won't get anything out of this.

1. You are missing lots of closing curly braces.
2. This doesn't mean anything and is not valid code:

('--', numberOfRows);


What are you trying to do here? This is lifted off somewhere else, but you haven't thought through it.

Again, think of where you're setting the flag value.

View Postajwsurfer, on 15 Oct, 2009 - 09:50 AM, said:

You could do it this way also, but it is way more confusing. By separating each problems solution into a separate function the code is much easier to deal with all the way around.



Well, you could also do it this way, that's far much simpler:


#include <iostream>
#include <cstdlib>


int main() {
  const int MAX_COL = 8;
  const int MAX_ROW = 8;

  for (int row = 0; row < MAX_ROW*2; ++row) {
	for (int col = 0; col < MAX_COL; ++col) {
	  if (row%2 == 0) {
	std::cout << "--";
	  } else {
	std::cout << "| ";
	  }
	}
	std::cout << std::endl;
  } 
}


Was This Post Helpful? 0
  • +
  • -

#10 Hellbroth  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 189
  • Joined: 15-August 09

Re: for loop ++1 increment

Posted 15 October 2009 - 12:34 PM

#include<iostream>

using namespace std;
int main(void)
{
	for(int i=0; i<8; i++)
	{
	cout << "---------------\n" <<endl;
	cout << "||||||||||||||||\n" <<endl;
	}

} 

This post has been edited by Hellbroth: 15 October 2009 - 12:35 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1