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;
}