for loop question

hi , so this is the question just very confused how to start and end , also please explain me what every line does thank you

input
2
output
#
#**#

(What I have tried)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
using namespace std;

int main() {

  int n=0;

  cin >> n;

  for (int i=0; i<n; i++){}
    
  
  
  }
  return 0; 
}
Last edited on
your for loop is empty.
your input and output have no sensible connection. what does 11 give?
you have extra {}s probably caused by misaligning the one on the end of main or the screwy pair off the for loop.

without more info we can't help you, though. surely they said more than that.
okay so I tried this and my output after typing 2 for n is **#**, BUT I want to have the # infront and at the end of it like #**#**#... how do I do this??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std; 
int main() {
 
int n;
cin >> n;

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

for (int j=0; j<i; j++)
cout << "#"; 
for (int k=0; k<n; k++)
cout << "*";

}


 return 0;
}



EDIT:Actually it has to be like this
user puts in number: 2
output is
#
#**#

OR
user puts in number: 4
output is
#
#****#
#****#****#
#****#****#****#
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
using namespace std;

string operator * ( int n, string s ){ return n ? s + (n-1) * s : ""; }

int main()
{
   int n;
   cout << "Enter n: ";   cin >> n;
   string part( n, '*' );
   for ( int i = 1; i <= n; i++ ) cout << '#' + ( i - 1 ) * ( part + '#' ) << '\n';
}


Enter n: 4
#
#****#
#****#****#
#****#****#****#
try to break it down into something simpler that is easy to do.
you need to print n lines.
the first line is special, but we can work around it with an initial condition.
string s = "#";
for(int i = 0; i < n; i++)
{
cout << s << endl; //prints # the first time.
s+= "****#"; //next time will be #****# and the next after that #****#****# and so on
}

that is for the 4 case, though. once you see that, you can extend it to repalce "****#" with a generated string that has the right number of * in it, which is what lastchance is doing above.
Last edited on
Here are two slightly different ways to accomplish this. In the first approach, we print hash marks for each hashIterator, testing if this is the last hash mark in which case we print a newline and break the control flow. Unless we broke out (at the last hashCount for this line), we print the spacers/fillers "***" between hash marks.
In the second approach we test if this is not the first hashIterator (i.e. not the first hash mark on this line) in which case we first print the spacers/fillers "***" After this, we print the hash mark. It all boils down to how you choose to think about the problem.
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
#include <iostream>

int main(void)
{
int n=7, hashCount, hashIterator, asteriskIterator;

std::cout << "This is my go to approach:" << std::endl;
for( hashCount=1; hashCount<=n; hashCount++ )
	{
	for( hashIterator=1; hashIterator<=hashCount; hashIterator++ )
		{
		std::cout << "#";
		if( hashIterator == hashCount )
			{
			std::cout << std::endl;
			break;
			}
		std::cout << "***";
		}
	}

std::cout << "Other option, if you don't like jumpy control flow:" << std::endl;
for( hashCount=1; hashCount<=n; hashCount++ )
	{
	for( hashIterator=1; hashIterator<=hashCount; hashIterator++ )
		{
		if( hashIterator > 1 )
			std::cout << "***";
		std::cout << "#";
		}
	std::cout << std::endl;
	}

return 0;
}
Consider this loop:
for( hashCount=1; hashCount<=n; hashCount++ )
One shouldn't write loops like this without a reason to do so. The better way is to consistently write
for( hashCount=0; hashCount < n; ++hashCount )
Dijkstra wrote about this; his essay is required reading:
https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html

int n=7, hashCount, hashIterator, asteriskIterator;
These variables were given descriptive, if verbose names, except for n, the only one whose meaning isn't obvious at a glance.

Since n is a constant, it should be constexpr:
int constexpr n_lines = 7;
Last edited on
try to break it down into something simpler that is easy to do.

This.

The input is N.
There will be N lines.
Every line starts with a '#'.
After that first character there is a repeating element (RE).
What is in a RE? N stars (*) and one hash (#).

Lets look at output, when N=3:
# + 0*RE
# + 1*RE
# + 2*RE
In other words the first line has no RE and last line has (N-1) RE's.
If you print the lines with a loop, then you can get the number of RE's on line from the loop counter.
Topic archived. No new replies allowed.