I do not know where I could begin. Tried to read my book but I have no ideas
Even if it is wrong post some code that you have done so I know where you are at and I can work from there. kemort has given you a good start to work with.
I may be late but I figured it out the first shape! (I am also a beginner and taught my self functions and parameters yesterday using solo-learn and Bucky's C++)
#include <iostream>
usingnamespace std;
int lLength(int x){
int a = 1;
do {
cout<<a<<" ";
a++;
if (a == x){
cout << "\n";
x--;
lLength(x);
}
} while (a < x);
return x;
}
int main()
{
cout << "Put a number in to declare the length: " << endl;
int leng;
cin >> leng;
lLength(leng);
}
but It prints "1" one extra more, here is the output if x is 10:
Hi, Dorby.
Your recursive function works fine, but the fact is that if “a” starts with an initial value of 1, at least a “1” will be printed on screen, since the statement cout<<a<<" ";
comes before the control-flow instruction while (a < x);
It doesn’t seem you need any further help to find a solution to this issue, but please post again if you are stuck.
And khoavo87 too: if you are not happy about the help received, just ask again.
I am still stuck on the code you provide above. Sorry, cause I took this class online and this is the first assignment that I am having big problem. Could everybody give me more details about the code ? Which one should I put in the x- letter and for "Put a number in to declare the length: ", which number should I put in there ?
but I am still working on shape 3, does everyone give me ideas, and I would like to ask that if I want to combine three shapes for one C++ program, is it impossible ?
For shape 3 you might like to consider breaking it down to 3 sub-shapes, the first and last being the same. And yes you can combine all of the 3 shapes into one program. Try it, just copy and paste each as a separate section the run ...
#include <iostream>
usingnamespace std;
//First shape
int lLength(int x){
int a = 1;
do {
if (a < x){cout<<a<<" ";}
a++;
if (a == x){
cout << "\n";
x--;
lLength(x);
}
} while (a < x);
return x;
}
int main()
{
cout << "Put a number in to declare the length: " << endl;
int leng;
cin >> leng;
lLength(leng);
// 2nd Shape
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= 13; j++)
cout << '*';
cout << endl;
}
return 0;
}
#include <iostream>
usingnamespace std;
//First shape
int lLength(int x){
int a = 1;
do {
if (a < x){cout<<a<<" ";}
a++;
if (a == x){
cout << "\n";
x--;
lLength(x);
}
} while (a < x);
return x;
}
int main()
{
cout << "Put a number in to declare the length: " << endl;
int leng;
cin >> leng;
lLength(leng);
// 2nd Shape
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= 13; j++)
cout << '*';
cout << endl;
}
// 3rd Shape
int size;
cout << "please enter the size of the hollow square:" << endl;
cin >> size;
if (size < 0) {
cout << "Please enter a postive number: " << endl;
cin >> size;
}
// This outer for-loop prints 1 row of output per iteration
else
{
for (int row = 0; row < size; row++) {
// This inner for-loop prints 1 column per iteration
for (int col = 0; col < size; col++) {
if (row == 0 || row == (size - 1))
{
if (col == 0 || col == size - 1)
cout << "=";
else {
cout << "=";
}
}
elseif (col == 0 || col == (size - 1))
cout << "*";
else
cout << " ";
}
// After printing all columns, print the newline character
cout << endl;
}
}
return 0;
}