Author Topic: Nested Loops  (Read 5194 times)

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
Nested Loops
« on: November 30, 2005, 08:18:02 PM »
This thread is for examples of nested loops
Analyze. Design. Develop. Debug. Deploy. Maintain.

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
Simple nested loop
« Reply #1 on: November 30, 2005, 08:24:08 PM »
Code: [Select]

#include <iostream>
using namespace std;

int main()
{
    cout << "-- LOOP INSIDE A LOOP --" << endl;
    cout << "This program demonstrates the use of " << endl;
    cout << "nested loops" << endl << endl;
   
    for (int j=0;j<10;j++)
    {
        for(int i=0;i<5;i++)
        {
            cout << "*";
        }
        cout << endl;
    }
   
    cout << endl;
    system("PAUSE");
    return 0;
}
Analyze. Design. Develop. Debug. Deploy. Maintain.

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
More nested loops
« Reply #2 on: November 30, 2005, 08:26:09 PM »
Code: [Select]

#include <iostream>
using namespace std;

void spaces(int count)
{
    for (int i=0;i<count;i++)
    {
        cout << " ";
    }    
}

int main()
{
    cout << "-- LOOP INSIDE A LOOP --" << endl;
    cout << "This program demonstrates the use of " << endl;
    cout << "nested loops" << endl << endl;
   
    for (int j=0;j<5;j++)
    {
        for(int i=0;i<=j;i++)
        {
            cout << "*";
        }
        cout << endl;
    }

    cout << endl;
   
    for (int j=0;j<5;j++)
    {
        for(int i=0;i<(5-j);i++)
        {
            cout << "*";
        }
        cout << endl;
    }

    cout << endl;
   
    for (int j=0;j<5;j++)
    {    
        spaces(4-j);
        for(int i=0;i<=j;i++)
        {
            cout << "*";
        }
        cout << endl;
    }            
     
    cout << endl;

    for (int j=0;j<5;j++)
    {
        spaces(j);
        for(int i=0;i<(5-j);i++)
        {
            cout << "*";
        }
        cout << endl;
    }

    cout << endl;
    system("PAUSE");
    return 0;
}
Analyze. Design. Develop. Debug. Deploy. Maintain.

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
Triangle
« Reply #3 on: November 30, 2005, 08:27:21 PM »
Code: [Select]

#include <iostream>
using namespace std;

void spaces(int count)
{
    for (int i=0;i<count;i++)
    {
        cout << " ";
    }    
}

int main()
{
    cout << "-- TRIANGLE --" << endl;
    cout << "Prints a triangle of asterisks when given the size " << endl;
    cout << "of the base." << endl;
    int size;
    cout << "# of asterisks in the base (must be an odd integer) : " << endl;
    cin >> size;
   
    for (int i = 1; i <= size; i+=2)
    {
        spaces( (size-i)/2 );
        for (int j=0; j<i; j++)
        {
            cout << "*";
        }
        cout << endl;
    }
       
    system("PAUSE");
    return 0;
}
Analyze. Design. Develop. Debug. Deploy. Maintain.

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
Diamond
« Reply #4 on: November 30, 2005, 08:28:19 PM »
Code: [Select]

#include <iostream>
using namespace std;

void spaces(int count)
{
    for (int i=0;i<count;i++)
    {
        cout << " ";
    }    
}

int main()
{
    cout << "-- DIAMOND --" << endl;
    cout << "Prints a diamond of asterisks." << endl;
    int size;
    cout << "# of asterisks in the middle (must be an odd integer) : ";
    cin >> size;
   
    for (int i = 1; i <= size*2; i+=2)
    {
        if (i <= size)
        {
            spaces( (size-i)/2 );
            for (int j=0; j<i; j++)
            {
                cout << "*";
            }
            cout << endl;
        }
        else
        {
            spaces( (i-size)/2 );
            for (int j=0; j<2*size-i; j++)
            {
                cout << "*";
            }
            cout << endl;              
        }
    }
       
    system("PAUSE");
    return 0;
}
Analyze. Design. Develop. Debug. Deploy. Maintain.