I have an exercise where i have to print the following text using two For loops in C\C++.
1
12
123
1234
12345
I am struggling to think of a way to do this, any ideas?
p.s. I am not asking for code or a solution, just a possible method.
Thanks.
Printable View
I have an exercise where i have to print the following text using two For loops in C\C++.
1
12
123
1234
12345
I am struggling to think of a way to do this, any ideas?
p.s. I am not asking for code or a solution, just a possible method.
Thanks.
Code:for i = 1 to 5:
string s = '';
for j = 1 to i:
append j to s;
repeat j
print s + newline;
repeat i
Hey Pena, no need for a string here:
Code:for i = 1 to 5:
for j = 1 to i:
print j;
repeat j
print newline;
repeat i
Up to you. I like to minimise I/O operations. It doesn't matter when it's pseudocode. ;)
thanks guys. I wrote this C++ code for it:
PHP Code:#include <iostream>
using namespace std;
int main(void)
{
for(int count=1;count<=5;count++)
{
for(int count1=1;count1<=count;count1++)
{
cout << count1;
}
cout << endl;
}
return 0;
}
Do you know the maximum length of the series? If so, it would probably be a bit faster to work the problem in reverse
VB Code:
dim st1 as string = "0123456789" dim x as integer for x=0 to 10 'print out st1.Substring(10-x) or something close to that next
One loop, but it uses string manipulations. There should be a faster way with C++, since you can use probably do something faster than the substring.
Oh yeah, if you HAVE to have two loops, then have the second loop do nothing. That would be fast!
This was a question on a worksheet that my programming lecturer set, so we had to do it the way he wanted us to.Quote:
Originally Posted by Shaggy Hiker
I doubt substring would be faster; it might even be slower.
Substring isn't necessarily fast, but no string function is. Since this is C/C++, there could be a better way with pointer arithmetic. Substring was mostly...well, actually, it was mostly because I overlooked that bit about the language being C/C++, but ignore that. Basically, I was suggesting a "technique that got a portion of a string". The idea is that if you have the maximum size of the string, then it would be faster to take the full string, which contains all the desired strings to begin with, and repeatedly chop it down, rather than a loop in a loop design.
The whole thing is irrelevant if you have the wrong teacher, though. You could have the first loop do something, and the second loop do nothing. The second loop would be particularly fast, as the compiler would recognize that it is meaningless, and optimize it out of the code (ideally).
When you get right down to it, the FASTEST technique that would technically suit, would be to create an array of strings:
"0"
"01"
"012"
etc.
Then output each member of the array in a separate line, and write two dummy loops that do nothing and get optimized away.
A step slower, but more thorough, using one loop, would be to recognize that this array need only contain ten strings to cover all possible sequences. Therefore, if you only need the base ten, loop through the array outputting the strings, and use a dummy loop for the second loop.