Number Patterns are killing me.

This is a discussion on Number Patterns are killing me. within the C++ Programming forums, part of the General Programming Boards category; For a programming homework assignment, we are using nested loops to create number patterns, I thought i was pretty good ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    1

    Number Patterns are killing me.

    For a programming homework assignment, we are using nested loops to create number patterns, I thought i was pretty good at this, but these problems are killing me because I cannot figure out the formula to get the start values.

    The patterns are these:
    the first
    5 7 9 11 10 8 6 4
    3 6 9 8 5 2
    2 6 5 1

    the second
    10 8 6 4 5 7 9 11
    8 5 2 3 6 9
    5 1 2 6

    third
    2 6 5 1
    3 6 9 8 5 2
    5 7 9 11 10 8 6 4

    fourth
    5 1 2 6
    8 5 2 3 6 9
    10 8 6 4 5 7 9 11

    I have everything but the start values figured out for instance the second one my program looks like this.
    Code:
    //Inner Loop 1 - right hand pattern
    //Start Value:  THIS IS WHAT I CAN'T FIGURE OUT on any of them.
    //Number of iterations: 5 - row
    //Adjustment: -row - 1
    
    //Inner Loop 2 - left hand pattern
    //Start Value: number + row + 2
    //Number of iterations:  5 - row
    //Adjustment:  row + 1
    
    
       
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    #include <iomanip>
    using namespace std;
    
    
    int main()
    {
       int row, col, number;
    
       for (row = 1; row <= 3; row++)
       {
          number = THIS IS WHAT I CAN'T FIGURE OUT;
          for (col = 1; col <= 5 - row; col++)
          {
             //Display correct number
             cout << setw(5) << number;
    
    
             //Adjust number
             number -= row - 1;
          } //End col loop
    
          number = number + row + 2;
          for (col = 1; col <= 5 - row; col++)
          {
             //Display correct number
             cout << setw (5) << number;
    
             //Adjust number
             number += row + 1;
          }//end column loop
          cout << endl;
       }  //End row loop
       return 0;
    }

  2. #2
    Dweeb dojha00's Avatar
    Join Date
    Feb 2012
    Location
    Global
    Posts
    23
    I know u can solve them but u don't want to do so ...
    btw i tried 1st and code is below..
    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
        int i,var,num,j,fix=5;    
        var =4;
        for(i=1;i<=3;i++)
        {
            num=fix;
            for(j=1;j<=var;j++)
            {
                cout<<num<<" ";
                num+=(i+1);
            }
            num-=(i+1);
            for(j=1;j<=var;j++)
            {
                num--;
                cout<<num<<" ";
                num-=(i);
            }
            cout<<endl;
            var-=1;
            fix=fix-(3-i);
            
        }
        return 0;
    }
    What is life??

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    What about
    Code:
    int startNumbers[3] = { 5, 3, 2 };
    and you just index this array in your loop.

    FWIW, counting from 0 is often easier.
    Code:
        for ( i = 0 ; i < 3 ; i++ ) {
            int j = 5 - 2*i + i/2;
            printf("%d %d\n", i, j);
        }
    This generates 5, 3, 2, if that is the point of the exercise.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Displaying a set of patterns
    By liins in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2010, 12:39 PM
  2. Design patterns in C
    By sashaKap in forum C Programming
    Replies: 2
    Last Post: 04-26-2009, 08:32 AM
  3. Printing Patterns
    By ferniture in forum C Programming
    Replies: 11
    Last Post: 11-17-2008, 09:00 PM
  4. Creating Patterns
    By incognito in forum Game Programming
    Replies: 5
    Last Post: 03-16-2003, 08:02 AM
  5. Patterns
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-29-2002, 04:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21