So I have a nested for loop here, what I need to be able to do is have each row do something else, but have there be a total of 5 rows and 5 columns. Every video / example that I've come across is generally printing a pattern out that is of * or some other symbol.
#include <iostream>
#include <conio.h>
void pattern();
usingnamespace std;
int main()
{
pattern();
_getch();
return 0;
}
void pattern() {
for (int row = 1; row <= 5; row++) {
for (int b = 2; b <= 10; b++) {
if (b % 2 == 0) {
cout << b << " ";
}
}
cout << endl;
}
}
So just as an idea, I would need to have a second row do something like: print out numbers 3-15 incremented by 3 each time. I wouldn't have any issue coding this part, just simply implementing it. I'm just having issues making my second row independent. I've tried a few different times, and it ends up doing printing out my numbers multiple times. If anyone knows of any videos that would help to better explain this, I would greatly appreciate it. Thank you.
#include <iostream>
#include <conio.h>
void pattern(); //Function prototype
usingnamespace std;
int main()
{
pattern(); //Function call
_getch(); //Allows program to remain open
return 0;
}
/*********************************************
This function creates a pattern
with 4 rows, first row increments by 2
second row by 3, third by 4, and fourth by 5.
Each ending number in rows are incremented by 5.
*********************************************/
void pattern() {
int step = 2; //Sets first number = 2
int end = 10; //Sets first ending number = 10
for (int i = 0; i < 4; i++) { //Sets limit for 4 rows
for (int b = step; b <= end; b += step) { //Increments b by step until <= end
cout << b << " ";
}
cout << endl; //Spacing
end += 5; //Increment end before going to outer for loop
step++; //Increment step before going to outer for loop
}
}
I was trying to go about it the completely wrong way. I was attempting to make a for loop for each line.