|
-
Oct 31st, 2006, 06:14 AM
#1
Thread Starter
Fanatic Member
Logic
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.
Last edited by x-ice; Nov 1st, 2006 at 03:56 PM.
-
Oct 31st, 2006, 06:36 AM
#2
Re: Logic
Code:
for i = 1 to 5:
string s = '';
for j = 1 to i:
append j to s;
repeat j
print s + newline;
repeat i
-
Oct 31st, 2006, 08:26 AM
#3
Re: Logic
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
-
Oct 31st, 2006, 08:28 AM
#4
Re: Logic
Up to you. I like to minimise I/O operations. It doesn't matter when it's pseudocode.
-
Oct 31st, 2006, 10:53 AM
#5
Thread Starter
Fanatic Member
Re: Logic
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;
}
Last edited by x-ice; Oct 31st, 2006 at 03:15 PM.
-
Oct 31st, 2006, 07:55 PM
#6
Re: Logic
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.
My usual boring signature: Nothing
 
-
Oct 31st, 2006, 07:56 PM
#7
Re: Logic
Oh yeah, if you HAVE to have two loops, then have the second loop do nothing. That would be fast!
My usual boring signature: Nothing
 
-
Nov 1st, 2006, 03:55 PM
#8
Thread Starter
Fanatic Member
Re: Logic
 Originally Posted by Shaggy Hiker
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.
-
Nov 1st, 2006, 06:31 PM
#9
Re: Logic
I doubt substring would be faster; it might even be slower.
-
Nov 2nd, 2006, 11:22 PM
#10
Re: Logic
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.
Last edited by Shaggy Hiker; Nov 2nd, 2006 at 11:26 PM.
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|