Results 1 to 10 of 10

Thread: Logic

  1. #1

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Resolved 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.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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

  3. #3
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    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
    Show Appreciation. Rate Posts.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Logic

    Up to you. I like to minimise I/O operations. It doesn't matter when it's pseudocode.

  5. #5

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    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.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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:
    1. dim st1 as string = "0123456789"
    2. dim x as integer
    3.  
    4. for x=0 to 10
    5.  'print out st1.Substring(10-x) or something close to that
    6. 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

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  8. #8

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: Logic

    Quote 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.

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Logic

    I doubt substring would be faster; it might even be slower.

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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
  •  



Click Here to Expand Forum to Full Width