I need to produce following pattern using two for loops.
a
aa
aaa
aaaa
aaaaa
aaaaaa
aaaaaaa
I have try this:
#include <iostream>
using namespace std;
int main()
{
int a;
a = 7;
for (int i = 1;i <= a;i++) {
cout << "" << endl;
for (int i = 1;i <= a;i++) {
cout << "*";
}
}
}
But the result is 7 characters in seven rows :(
For starters the task can be done using only one loop. For example
#include <iostream>
#include <iomanip>
int main()
{
while ( true )
{
const char c = '*';
std::cout << "Enter a non-negative number (0 - exit): ";
unsigned int n;
if ( not ( std::cin >> n ) or ( n == 0 ) ) break;
std::cout << '\n';
for ( unsigned int i = 0; i < n; i++ )
{
std::cout << std::setw( i + 2 ) << std::setfill( c ) << '\n';
}
std::cout << '\n';
}
return 0;
}
The program output might look like
Enter a non-negative number (0 - exit): 7
*
**
***
****
*****
******
*******
Enter a non-negative number (0 - exit): 6
*
**
***
****
*****
******
Enter a non-negative number (0 - exit): 5
*
**
***
****
*****
Enter a non-negative number (0 - exit): 4
*
**
***
****
Enter a non-negative number (0 - exit): 3
*
**
***
Enter a non-negative number (0 - exit): 2
*
**
Enter a non-negative number (0 - exit): 1
*
Enter a non-negative number (0 - exit): 0
As for your code then the inner loop outputs exactly 7 characters '*'
for (int i = 1;i <= a;i++) {
cout << "*";
}
So what you do is what you get.
You could write the inner loop for example the following way
for (int j = 0;j < i; j++) {
cout << "*";
}
Related
I was tasked with this problem for homework for my c++ class and I can't figure it out.
The task is: Create a program that will create a pattern in which is a pyramid. The user should enter the maximum number of rows to be output. Use a while loop that confirms the number of rows is between 1 and 9 inclusive. Next 1 should be output in the first row, 222 output in the second row, 33333 should be output in the third row, etc. For example if the user entered 7 the following would be output.
The code I have now does this almost exactly, instead of outputting, for example 222 for the second row, it outputs 2 2
Here is what my code looks like:
#include <iostream>
using namespace std;
int main()
{
int rows, count = 0, count1 = 0, k = 0;
cout << "Please enter the number of rows." << endl;
cin >> rows;
while (rows > 9)
{
cout << "That is an invalid selection, please choose up to 9 rows." << endl;
cin >> rows;
}
for (int i = 1; i <= rows; ++i)
{
for (int space = 1; space <= rows - i; ++space)
{
cout << " ";
++count;
}
while (k != 2 * i - 1)
{
if (count <= rows - 1)
{
cout << i << " ";
++count;
}
k++;
}
count1 = count = k = 0;
cout << endl;
}
}
Any help is appreciated, I'm assuming it should just be a small tweak.
This loop
while (k != 2 * i - 1)
{
if (count <= rows - 1)
{
cout << i << " ";
++count;
}
k++;
}
count1 = count = k = 0;
does not make sense. For example the variable count1 is never used except the statement
count1 = count = k = 0;
So it is unclear what is the purpose to define this variable.
Using manipulators from the header <iomanip> you can write a pyramid using only one loop.
Here is a demonstrative program
#include <iostream>
#include <iomanip>
int main()
{
while ( true )
{
const int MAX_HEIGHT = 9;
std::cout << "Enter the height of a pyramid not greater than "
<< MAX_HEIGHT << " (0 - exit): ";
int height;
if ( not ( std::cin >> height ) or ( height <= 0 ) ) break;
if ( MAX_HEIGHT < height ) height = MAX_HEIGHT;
std::cout << '\n';
for ( int i = 0; i < height; i++ )
{
std::cout << std::setw( height - i ) << std::setfill( ' ' ) << i + 1;
std::cout << std::setw( 2 * i + 1 ) << std::setfill( char( i + '1' ) ) << '\n';
}
std::cout << '\n';
}
return 0;
}
Its output might look the following way
Enter the height of a pyramid not greater than 9 (0 - exit): 1
1
Enter the height of a pyramid not greater than 9 (0 - exit): 2
1
222
Enter the height of a pyramid not greater than 9 (0 - exit): 3
1
222
33333
Enter the height of a pyramid not greater than 9 (0 - exit): 4
1
222
33333
4444444
Enter the height of a pyramid not greater than 9 (0 - exit): 5
1
222
33333
4444444
555555555
Enter the height of a pyramid not greater than 9 (0 - exit): 6
1
222
33333
4444444
555555555
66666666666
Enter the height of a pyramid not greater than 9 (0 - exit): 7
1
222
33333
4444444
555555555
66666666666
7777777777777
Enter the height of a pyramid not greater than 9 (0 - exit): 8
1
222
33333
4444444
555555555
66666666666
7777777777777
888888888888888
Enter the height of a pyramid not greater than 9 (0 - exit): 9
1
222
33333
4444444
555555555
66666666666
7777777777777
888888888888888
99999999999999999
Enter the height of a pyramid not greater than 9 (0 - exit): 0
Assignment:
Write a program that prompts the user for an integer value representing the height of a triangle. The program should then print out a triangle of O’s of that height, with a vertically aligned right edge.
My problem:
I have figured out the code to execute a normal triangle, but I am having some difficulty on writing code to have spacing before my "0's" to make it aligned to the right.
#include <iostream>
using namespace std;
int main()
{
int triHeight;
int c = 0;
int r = 0;
int k = 0;
cout << "Enter the triangle height: " << endl;
cin >> triHeight;
for (c = 0; c <= triHeight; c = c+1)
{
for (r = 0; r < c; r = r + 1)
{
cout << "0";
}
for (k = 0; k <= c; k = k - 1)
{
cout << " ";
}
cout << endl;
}
system("pause");
return 0;
}
You have two main problems :
you write the spaces after the 0 rather than before to have the 0 indented
for (k = 0; k <= c; k = k - 1) never ends up to the possible effect of an overflow
I also encourage you to check the result of >> to be sure a valid integer was enter so like if (!(cin >> triHeight)) cerr << "invalid height" << endl; else { ... }
A right way close to yours using loops for all is (supposing you want a pyramid) :
#include <iostream>
using namespace std;
int main()
{
int triHeight;
cout << "Enter the triangle height: " << endl;
if (!(cin >> triHeight))
cerr << "invalid height" << endl;
else {
for (int h = 1; h <= triHeight; h += 1) {
for (int s = triHeight - h; s != 0; s -= 1)
cout << ' ';
for (int z = 2*(h-1)+1; z >0; z -=1)
cout << '0';
cout << endl;
}
}
}
Compilation and execution :
/tmp % g++ -pedantic -Wextra -Wall c.cc
/tmp % ./a.out
Enter the triangle height:
5
0
000
00000
0000000
000000000
/tmp %
If you want half a pyramid :
#include <iostream>
using namespace std;
int main()
{
int triHeight;
cout << "Enter the triangle height: " << endl;
if (!(cin >> triHeight))
cerr << "invalid height" << endl;
else {
for (int h = 1; h <= triHeight; h += 1) {
for (int s = triHeight - h; s != 0; s -= 1)
cout << ' ';
for (int z = 0; z < h; z += 1)
cout << '0';
cout << endl;
}
}
}
Compilation and execution :
/tmp % g++ -pedantic -Wall -Wextra c.cc
/tmp % ./a.out
Enter the triangle height:
5
0
00
000
0000
00000
/tmp %
You can also do not make the two internal loops by yourself :
#include <iostream>
using namespace std;
int main()
{
int triHeight;
cout << "Enter the triangle height: " << endl;
if (!(cin >> triHeight))
cerr << "invalid height" << endl;
else {
for (int h = 1; h <= triHeight; h += 1)
cout << string(triHeight - h, ' ') << string(h, '0') << endl;
}
}
Compilation and execution :
/tmp % g++ -pedantic -Wall -Wextra c.cc
/tmp % ./a.out
Enter the triangle height:
5
0
00
000
0000
00000
/tmp %
that solution is shorter but creates temporary strings
The logic of your program is wrong. At first you are trying to output the symbol 'O'
for (r = 0; r < c; r = r + 1)
{
cout << "0";
}
and after that you are trying to output spaces
for (k = 0; k <= c; k = k - 1)
{
cout << " ";
}
Moreover this loop is invalid because the variable k is decremented starting from 0.
The program can be written using only one loop and standard i/o manipulators declared in the header <iomanip>
Here is a demonstrative program
#include <iostream>
#include <iomanip>
int main()
{
while ( true )
{
const char c = 'O';
std::cout << "Enter the height of a triangle (0 - exit): ";
int height = 0;
if ( not ( std::cin >> height ) or ( height <= 0 ) ) break;
std::cout << '\n';
for ( int i = 0; i < height; i++ )
{
std::cout << std::setw( height - i ) << std::setfill( ' ' ) << c;
std::cout << std::setw( i + 1 ) << std::setfill( c ) << '\n';
}
std::cout << '\n';
}
return 0;
}
The program output might look the following way
Enter the height of a triangle (0 - exit): 1
O
Enter the height of a triangle (0 - exit): 2
O
OO
Enter the height of a triangle (0 - exit): 3
O
OO
OOO
Enter the height of a triangle (0 - exit): 4
O
OO
OOO
OOOO
Enter the height of a triangle (0 - exit): 5
O
OO
OOO
OOOO
OOOOO
Enter the height of a triangle (0 - exit): 6
O
OO
OOO
OOOO
OOOOO
OOOOOO
Enter the height of a triangle (0 - exit): 7
O
OO
OOO
OOOO
OOOOO
OOOOOO
OOOOOOO
Enter the height of a triangle (0 - exit): 8
O
OO
OOO
OOOO
OOOOO
OOOOOO
OOOOOOO
OOOOOOOO
Enter the height of a triangle (0 - exit): 9
O
OO
OOO
OOOO
OOOOO
OOOOOO
OOOOOOO
OOOOOOOO
OOOOOOOOO
Enter the height of a triangle (0 - exit): 10
O
OO
OOO
OOOO
OOOOO
OOOOOO
OOOOOOO
OOOOOOOO
OOOOOOOOO
OOOOOOOOOO
Enter the height of a triangle (0 - exit): 0
The simplest way to do this is to use the built in features of the std::iostream:
#include <iostream>
#include <iomanip>
#include <string>
int main()
{
int triHeight;
int c = 0;
std::cout << "Enter the triangle height: \n";
std::cin >> triHeight;
std::cout << std::setfill(' ') << std::right;
for (c = 0; c < triHeight; c++)
{
std::cout << std::setw(triHeight) << std::string(c+1,'0') << '\n';
}
return 0;
}
You can get back to a left aligned triangle by simply changing std::right to std::left.
Background and issue:
I have created a diamond shape program for class, but I'm having issues with an even amount of rows for the diamond. I'm thinking there may be something wrong with my logic. It seems to be whenever I use an even amount of rows for the diamond shape, it does not show up as an even amount of rows but odd.
I've tried different possible "solutions", but they didn't work.
For example, I changed for (int b = 0; b < asterisk; b++) to for (int b = 0; b <= asterisk; b++) It displayed the correct number of rows, however it was no longer much of a proper diamond shape. It also (obviously) affected the odd-numbered row diamonds so they don't look like proper diamonds either.
I'm completely stuck and would definitely appreciate a nudge in the right direction.
Here is my code:
#include <iostream>
using namespace std;
int main() {
int i, j, space, asterisk;
do
{
cout << "Enter the number of rows desired to make a diamond pattern (0 to quit): ";
cin >> i;
j = (i - 1) / 2;
for (int z = 0; z < i; z++)
{
space = abs(j - z);
asterisk = i - 2 * space;
for (int a = 0; a < space; a++)
cout << " ";
for (int b = 0; b < asterisk; b++)
cout << "*";
for (int c = 0; c < space; c++)
cout << " ";
cout << endl;
}
} while (i > 0);
cout << "Goodbye!" << endl;
}
Thank you very much!
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
int i, j, space, asterisk, is_even;
do
{
cout << "Enter the number of rows desired to make a diamond pattern (0 to quit): ";
cin >> i;
is_even = (i % 2 == 0) ? 1 : 0;
//Above line uses ternary operator to assign is_even flag to 1 if the number is even and 0 if it is not.
j = (i - 1) / 2;
for (int z = 0; z < i; z++)
{
space = abs(j - z);
asterisk = (is_even) ? i - 2 * space - 1 : i - 2 * space; //Change 1
for (int a = 0; a < space; a++)
cout << " ";
//Change 2.STARTS
if(space == 0 && is_even ){
for (int b = 0; b < asterisk; b++)
cout << "*";
cout<<endl;
}
//Change 2.ENDS
for (int b = 0; b < asterisk; b++)
cout << "*";
//for (int c = 0; c < space; c++)
// cout << " ";
//You dont need to add the spaces at the end of each line.
cout << endl;
}
} while (i > 0);
cout << "Goodbye!" << endl;
}
Output ::
Enter the number of rows desired to make a diamond pattern (0 to quit): 1
*
Enter the number of rows desired to make a diamond pattern (0 to quit): 2
*
*
Enter the number of rows desired to make a diamond pattern (0 to quit): 3
*
***
*
Enter the number of rows desired to make a diamond pattern (0 to quit): 4
*
***
***
*
Enter the number of rows desired to make a diamond pattern (0 to quit): 5
*
***
*****
***
*
Enter the number of rows desired to make a diamond pattern (0 to quit): 6
*
***
*****
*****
***
*
Enter the number of rows desired to make a diamond pattern (0 to quit): 7
*
***
*****
*******
*****
***
*
Enter the number of rows desired to make a diamond pattern (0 to quit): 8
*
***
*****
*******
*******
*****
***
*
Enter the number of rows desired to make a diamond pattern (0 to quit): 0
Goodbye!
You could add an if else statement after getting the input from the user. Then you can make a decision based on that input for how you would want to display your diamond.
Pseudo code:
{
if ( odd ) {
// do it this way
} else { // even
// do it this way
}
}
I have to input a value in the program and keep dividing it by 4 until it reaches the number 0. But when I run it, it doesn't stop at 0, it keeps repeating 0 forever. What is wrong with the code?
#include <iostream>
using namespace std;
int main(){
double input;
cout << "Enter an Integer: ";
cin >> input;
cout << input << "/ 4 ";
do
{
input = input / 4;
if (input >= 0)
cout <<" = "<< input << endl;
cout <<input << " /4";
}
while ((input >= 0) || (input != 0));
return 0;
}
Here are my three cents.:)
#include <iostream>
int main()
{
const long long int DIVISOR = 4;
while ( true )
{
std::cout << "Enter an Integer (0 - Exit): ";
long long int n;
if ( not ( std::cin >> n ) or ( n == 0 ) ) break;
std::cout << std::endl;
do
{
std::cout << n << " / " << DIVISOR;
n /= DIVISOR;
std::cout << " = " << n << std::endl;
} while ( n );
std::cout << std::endl;
}
return 0;
}
The program output might look like
Enter an Integer (0 - Exit): 1000
1000 / 4 = 250
250 / 4 = 62
62 / 4 = 15
15 / 4 = 3
3 / 4 = 0
Enter an Integer (0 - Exit): 0
Basically I just started doing C++ again after a while because I need to (Degree sorta commands it) and I have been tasked with a simple task of writing a simple program that would take a function and use 2 integer inputs (N and M), returning a double output (S). In one part I am asked to to use a loop to display values for S all the way up to N=10 from N=0 for the value M=10
Ive run into a problem where the return give the value "5" for every N up to 10.
This is the code: (do not mind the comments)
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
//Function, Part A
double func_18710726(int N, int M)
{
double S = 0;
for (int n = 1; n <= N; n++)
for (int m = 1; m <= M; m++)
{
S = S + (sqrt(m*n)+exp(sqrt(m))+ exp(sqrt(n)))/(m*n + 2);
}
return S;
}
//Part B
double func_18710726(int, int);
using namespace std;
int main()
{
int N, M;
double S;
//Part B1
do {
cout << "Enter Value of N for N > 0 and an integer" << endl;
cin >> N;
} while (N <= 0);
do {
cout << "Enter value of M for M > 0 and an integer" << endl;
cin >> M;
} while(M <= 0);
//Part B2
S = func_18710726(N, M);
cout << "The Summation is ";
cout << fixed << setprecision(5) << S << endl;
//Part B3
ofstream output;
output.open("Doublesum.txt");
M = 1;
for (int n = 1; n <= 10; n++)
{
S = func_18710726(n, M);
cout << "The summation for N = " << n << " is ";
cout << fixed << setprecision(5) << 5 << endl;
output << fixed << setprecision(5) << 5 << endl;
}
output.close();
return 0;
}
The output gives me:
Enter Value of N for N > 0 and an integer
1
Enter value of M for M > 0 and an integer
2
The Summation is 4.20696
The summation for N = 1 is 5
The summation for N = 2 is 5
The summation for N = 3 is 5
The summation for N = 4 is 5
The summation for N = 5 is 5
The summation for N = 6 is 5
The summation for N = 7 is 5
The summation for N = 8 is 5
The summation for N = 9 is 5
The summation for N = 10 is 5
--------------------------------
Process exited after 2.971 seconds with return value 0
Press any key to continue . . .
Any help as to why this is happening is much appreciated.
The Question itself
I am sorry if I posted this in the wrong place, if I do, Mods please go easy on me :)
This line:
cout << fixed << setprecision(5) << 5 << endl;
has 5 (five) as its output - you want S (esss)
Probably S is not such a great name for a variable (neither is l)