For looping

I'm trying to teach myself for looping.

I want this program to input a odd number and make a square outlined in two's and inside have 0's and then a star in the center.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
using namespace std;
int main()
{
 int z=0,
     i,
 	 j,
 	 k,
 	 star,
 	 g=2,
 	 number,
 	 a=0;

 cout << "Please enter a odd number between 0-20." << endl;
 cin >> number;

 while(number % 2 != 1 && number < 20 && number > 0)
		 {
     cout << "Invalid input.Enter an odd number between 0-20: " << endl;
     cin >> number;
 }
 for (number/number; number%2; number--)
 {

	 for (j=number-4; j>z; j--)
	        {
	            cout << g;
	        }
	 for (i=number; i-1; i--)
	 {
		 cout<<g << endl;
	 }


	 for (k=number-2; k>z; k--)
	        {

		     cout << a;
	        }

	 for (star=number; star>=1 && star/2; star++)
	     {
	         cout << "*";
	     }
	 for (j=number; j>z; j--)
	        {
	            cout << g;
	        }
	 number= number-1;
 }


I need to know what the 3 parts of the for looping mean. Please use vary basic language.
for loop have following sintax:
for(init-statement; condition; loop-statement) body; init-statement executes once before loop as whole. Usually you will place loop variable initialization here (int i = 0)
condition is checked each time when loop is about to run again. if it is true, body of the loop executes, if false — statement after loop executes. (i < 10)
loop-statement executes after each iteration. Usually you will change loop variable here. (++i)
body is a statement (or several statements encased in curly brackets) which executes on each iteration. (std::cout << '*';)

if you combine all examples on parts of the loop ypu will get:
1
2
for(int i = 0; i < 10; ++i)
    std::cout << '*';

it will print ten '*' symbols in a row.

for loop also can be seen as:
1
2
3
4
5
6
7
{
    init-statement;
    while(condition) {
        body;
        loop-statement;
    }
}
The for loop

Its format is:

for (initialization; condition; increase) statement;

and its main function is to repeat statement while condition remains true, like the while loop. But in addition, the for loop provides specific locations to contain an initialization statement and an increase statement. So this loop is specially designed to perform a repetitive action with a counter which is initialized and increased on each iteration.

It works in the following way:

1. initialization is executed. Generally it is an initial value setting for a counter variable. This is executed only once.

2. condition is checked. If it is true the loop continues, otherwise the loop ends and statement is skipped (not executed).

3. statement is executed. As usual, it can be either a single statement or a block enclosed in braces { }.

4. finally, whatever is specified in the increase field is executed and the loop gets back to step 2.

http://www.cplusplus.com/doc/tutorial/control/



According to this, can I use both i++ and ++i inside a for() in the same meaning?
No difference?

 
for(int i = 0; i < 10; ++i) std::cout << '*';


 
for(int i = 0; i < 10; i++) std::cout << '*';
Here's the most updated code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <fstream>

using namespace std;


int main()
{
   const int TWO=2,
		     ZERO=0;
   int number;

   ofstream out_file;

   out_file.open ("prog6_out_acb95.txt");

   if(!out_file)
     {
	   cout << "Output file failed to open.";
	   return 1;
      }

   cout << "Please enter a odd number between 0-20." << endl;
   cin >> number;


   for (int outline=0; outline<=number-1; outline++) //rows
   {


	 for (int sides=0; sides<=number-1; sides++) //columns
	   {
		 if(outline==0 || outline == number-1)
		 	   {
		 		 out_file << TWO;
		 	   }


		 else if (sides==0 || sides==number-1)
		 {
	     out_file << TWO;
		 }
		 else
		 {
		  out_file << ZERO;
		   if (outline !=number && outline==(int)number/2)
		  		 {
		  			 out_file << "*";
		  		 }
		 }
	   }

   out_file <<endl;


   }
   out_file.close();
   return 0;
}




the out come is for example if I enter 5:
22222
20002
20*0*0*2
20002
22222

I just want one star in the middle though.
I fixed that for you:
1
2
3
4
5
6
7
//...   SKIPPED
                out_file << TWO;
            } else if (sides == number / 2 && outline == number/2) {
                out_file << "*";
            } else
                out_file << ZERO;
        }//inner for loop end; 

Some notes for you:
a) (int)number/2 will be parsed as ((int)number) / 2
b) However it is an integer division, so result will always be integer.
c) In C++ you should use static_cast<int>(variable), not C-style (int)variable
Last edited on
Topic archived. No new replies allowed.