Small tweak to my c++ pyramid assignment? How should I do it? - c++

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

Related

Write a C++ program which will print (half pyramid) pattern of natural numbers

The task:
Write a C++ program which will print (half pyramid) pattern of natural numbers.
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
I have tried using this code, but its not giving the output:
#include <iostream>
using namespace std;
int main()
{
int rows, i, j;
cout << "Enter number of rows: ";
cin >> rows;
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= i; j++)
{
cout << j << " ";
}
cout << "\n";
}
return 0;
}
OUTPUT:
Enter number of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
j in your inner loop is the 1 based index of the element in the current row (1,2,3 etc.).
Instead of printing it, you should print a counter that is increased over all the iterations.
Something like:
#include <iostream>
int main()
{
int rows, i, j;
std::cout << "Enter number of rows: ";
std::cin >> rows;
int n = 1;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
std::cout << n << " ";
n++; // for next iteration
}
std::cout << "\n";
}
return 0;
}
Output example:
Enter number of rows: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
A side note: better to avoid using namespace std - see here Why is "using namespace std;" considered bad practice?.

Produce following asterisk using two for loops

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

Spacing for Right aligned Triangle

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.

Dividing a number in loop until it satisfies a condition = 0

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

C++ unexpected array output

//Page 215, #2, by Jeremy Mill
//program taken in 7 values, displays them, and then sorts them from highest to lowest, and displays them in order.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//Define the variable we will need
const int arraySize = 6;
double dailySales[arraySize];
//Now let's prompt the user for their input
for (int a=0 ; a <= arraySize; a++ )
{
cout << "Please enter sale number " << a+1 << " :";
cin >> dailySales[a];
}
//Now we display the output of the array
cout << "\n\nSale Number" << setw( 13 ) << "Value" << endl;
for ( int i =0; i <= arraySize; i++ )
cout << setw( 5 ) << i << setw( 14 ) << dailySales[ i ] << endl;
//Now we sort using a bubble sort
for(int b = 0; b<=arraySize; b++)
for(int c = arraySize-1; c>=b; c--) {
if(dailySales[c-1] > dailySales[c]) { // if out of order
// exchange elements
int t = 0;
t = dailySales[c-1];
dailySales[c-1] = dailySales[c];
dailySales[c] = t;
cout << "it ran";
}
}
cout << "Now we can display the array again! \n\n\n" << endl << dailySales[6] << endl;
//Now we display the output of the sorted array
cout << "\n\nSale Number" << setw( 13 ) << "Value" << endl;
for ( int d = 0; d <= arraySize; d++ )
cout << setw( 5 ) << d << setw( 14 ) << dailySales[ d ] << endl;
cin.clear(); //clear cin
cin.sync(); //reinitialize it
cout << "\n\nPress Enter to end the program\n\n"; //display this text
cin.get(); //pause and wait for an enter
return 0;
} // end main
Output:
Please enter sale number 1 :1
Please enter sale number 2 :2
Please enter sale number 3 :3
Please enter sale number 4 :4
Please enter sale number 5 :5
Please enter sale number 6 :6
Please enter sale number 7 :7
Sale Number Value
0 1
1 2
2 3
3 4
4 5
5 6
6 7
Now we can display the array again!
7
Sale Number Value
0 1
1 2
2 3
3 4
4 5
5 6
6 2.97079e-313
Press Enter to end the program
Why is it that the last 'value' is not 7, but that number in sci. notation??
Be carreful when looping through your array. The loop has to start from 0 to size-1.
So instead of using less-than-or-equal:
for (int a = 0; a <= arraySize; a++)
You should use less-than:
for (int a = 0; a < arraySize; a++)
The array size is declared to be 6 elements and then 7 elements [0...6] are placed in it.
Redeclare array size to be 7 and then change all the <= to < in the loops.