Accelerated C++ Exercise 2.4 - c++

I've been following Accelerated C++ for a couple of weeks now but I have been stuck at exercise 2.4 for a while and finally I thought I found it out but after trying giving it different dimensions I found out that it doesn't really work and I don't really understand why
The code initially prints a framed message, in this particular exercises I was supposed to change how the code prints the blanks from one character at a time into writing all the planks at once
here is the code:
// [2-0, 2-4] Exercises
#include<iostream>
#include<string>
// saying what standard-library names we use
using std::cout; using std::endl;
using std::cin; using std::string;
int main()
{
// asking for the name
cout << "Please enter your first name: ";
// reading the name
string name;
cin >> name;
// building the message that we intend to write
const string greeting = "Hello, " + name + "!";
// 2.2 & 2.3 asking for inpadY
cout << "Please enter the number of padY (Vertical padding): ";
// 2.2 & 2.3 reading the inpadY
int inpadY;
cin >> inpadY;
// 2.2 & 2.3 asking for inpadX
cout << "Please enter the number of padX (Horizontal padding): ";
// 2.2 & 2.3 reading the inpadX
int inpadX;
cin >> inpadX;
// the number of planks surrounding the greeting
// 2.2 & 2.3 added inpadY as the number of planks;
const int padY = inpadY;
// 2.2 & 2.3 added inpadX
const int padX = inpadX;
// 2.4 pad size
const int pad = inpadX + inpadY;
// the number of rows and columns to write
const int rows = padY * 2 + 3;
const string::size_type cols = greeting.size() + padX * 2 + 2;
// 2.4 creating a padding string left and right and top and bottom
const string LeftRightPad(padY, ' ');
const string TopBottomPad(cols - 2, ' ');
// write a blank line to separate the output and the input
cout << endl;
// write rows rows of output
// invariant: we have written r rows so far
for (int r = 0; r != rows; ++r) {
string::size_type c = 0;
// invariant: we have written c characters so far in the current row
while (c != cols) {
// is it time to write the greeting?
if (r == padY + 1 && c == padX + 1)
{
cout << greeting;
c += greeting.size();
} else {
// are we on the border?
if (r == 0 || r == rows - 1 ||
c == 0 || c == cols - 1)
{cout << "*";
++c;}
else
// 2.4 typing out the spaces at once
{cout << LeftRightPad;
c += LeftRightPad.size();}
}
}
cout << endl;
}
return 0;
}
Edited to have the input and output
Please enter your first name: Estrogen
Please enter the number of padY (Vertical padding): 2
Please enter the number of padX (Horizontal padding): 2
**********************
* *
* *
* Hello, Estrogen! *
* *
* *
**********************
Process returned 0 (0x0) execution time : 3.281 s
Press any key to continue.
Please enter your first name: Estrogen
Please enter the number of padY (Vertical padding): 2
Please enter the number of padX (Horizontal padding): 5
****************************
* *
* *
* *
* *
* *
****************************
Process returned 0 (0x0) execution time : 5.098 s
Press any key to continue.
Please enter your first name: Estrogen
Please enter the number of padY (Vertical padding): 3
Please enter the number of padX (Horizontal padding): 2
**********************
*
*
*
*
*
*
*
**********************
Process returned 0 (0x0) execution time : 4.333 s
Press any key to continue.
Update: I've rewritten the code and the output is an infinite loop of asterisks here is the new code
#include<iostream>
#include<string>
using std::string; using std::endl;
using std::cout; using std::cin;
int main()
{
cout << "Please enter your first name: ";
string name;
cin >> name;
const string message = "Hello, " + name + "!";
cout << "Enter the length: ";
int length;
cin >> length;
cout << "Enter the height: ";
int height;
cin >> height;
const int rows = height * 2 + 3;
const string::size_type cols = message.size() + length * 2 + 2;
const string TopBottom(cols, '*');
const string Blank(cols - 2, ' ');
const string messageblank(cols - 3 - message.size(), ' ');
cout << endl;
for (int r = 0; r != rows; ++r) {
string::size_type c = 0;
while (c != cols) {
if ( r == height + 1 && c == length + 1)
{
cout << messageblank << message << messageblank;
c += Blank.size();
} else
if (r == 0 && c == 0 || r == rows - 1 && c == cols -1)
{
cout << TopBottom;
c += TopBottom.size();
} else
if ( r != 0 && c == 0 || r != rows -1 && c == cols - 1)
{
cout << "*";
++c;
} else
cout << Blank;
c += Blank.size();
}
cout << endl;
}
return 0;
}
Thank you guys for help in advance

Unless the code is supposed to be written this way, I would propose another, row by row approach:
print_frame_row(cols);
for (int i = 0; i < padY; ++i)
print_v_padding(cols);
print_greeting(padX, greeting);
for (int i = 0; i < padY; ++i)
print_v_padding(cols);
print_frame_row(cols);
where
void print_frame_row(int cols)
{
std::cout << std::string(cols, '*') << '\n';
}
void print_v_padding(int cols)
{
const std::string h_padding(cols - 2, ' ');
std::cout << '*' << h_padding << "*\n";
}
void print_greeting(int padX, const std::string &msg)
{
const std::string h_padding(padX, ' ');
std::cout << '*' << h_padding << msg << h_padding << "*\n";
}
This way you have a simpler logic, and need not worry about counting columns or deciding when to write each character.

ok so it took me 3 days but I finally figured it out here is the working code
#include<iostream>
#include<string>
using std::string; using std::endl;
using std::cout; using std::cin;
int main()
{
cout << "Please enter your first name: ";
string name;
cin >> name;
cout << "Enter the length: ";
int length;
cin >> length;
cout << "Enter the height: ";
int height;
cin >> height;
const string message = "Hello, " + name + "!";
const int rows = height * 2 + 3;
const string::size_type cols = message.size() + length * 2 + 2;
const string TopBottom(cols, '*');
const string Blank(cols - 2, ' ');
const string messageblank(length, ' ');
cout << endl;
for (int r = 0; r != rows; ++r) {
string::size_type c = 0;
while (c != cols) {
if ( r == height + 1 && c == 0)
{
cout << "*" << messageblank << message << messageblank << "*";
c += TopBottom.size();
} else
if (r == 0 && c == 0 || r == rows - 1 && c == 0)
{
cout << TopBottom;
c += TopBottom.size();
} else
if ( c == 0 && r != 0 || c == 0 && r != rows - 1)
{
cout << "*" << Blank << "*";
c += TopBottom.size();
}
}
cout << endl;
}
return 0;
}

Related

Print a numerical hollow square pattern using recursive functions in c++

I've been trying to print a square numerical pattern like this one:
12345
2 4
3 3
4 2
54321
But I can't seem to find the correct way to code a recursive function to do it.
I've only managed to print a numerical triangle, but the square has been killing me.
#include <iostream>
using namespace std;
void print_row(int no, int val)
{
if (no == 0)
return;
cout << val << " ";
print_row(no - 1, val);
}
void pattern(int n, int num)
{
if (n == 0)
return;
print_row(num - n + 1, num - n + 1);
cout << endl;
pattern(n - 1, num);
}
int main()
{
int n = 5;
pattern(n, n);
}
output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Code to output a square shape pattern using recursion.This is as far as I've gotten to try and print a numerical square, I don't know how to print it out with ascending and descending numbers.
#include <iostream>
#include <string>
using namespace std;
void line( int row, int num )
{
if ( row < 0 ) return;
char c = row * ( num - row - 1 ) ? ' ' : '*';
cout << '*' << string( num - 2, c ) << "*\n";
line( row - 1, num );
}
int main()
{
int n;
cout << "Input side (n > 2): "; cin >> n;
line( n - 1, n );
return 0;
}
output:
*****
* *
* *
* *
*****
I took the time to built this two functions. Try to understand them though, you probably are on a test and this topic might be helpful in the future.
You just call square(n), with n being the number you want to use for the square.
void print_sides(int n, int col) {
if (col == 0) return;
print_sides(n, col - 1);
if(col != 1) cout << col << string(2*(n - 1)-1, ' ') << (n - col) + 1 << "\n" ;
}
void square(int n, int col = 0, bool sides = true) {
if (col >= n) {
return;
}
else {
if (sides) {
cout << col + 1 << " ";
square(n, col + 1, true);
}
else {
square(n, col + 1, false);
cout << col + 1 << " ";
}
if (col == n - 1 && sides == true) {
cout << "\n";
print_sides(n, col);
square(n, 0, false);
}
}
}
This is one of the ways to solve this question.
The pattern function prints the hollow square pattern while the printSubRows is responsible for generating the middle portion.
void printSubRows(int n,int i,int c) {
if (c==n-2) return;
//adjust the space according to your requirements
cout << i << " " << n-i+1 << endl;
printSubRows(n,i+1,c+1);
}
void pattern(int n) {
//prints the first row
for (int i=1;i<=n;i++) cout << i;
cout << endl;
//recursive function to print the middle part
printSubRows(n,2,0);
//prints the last row
for (int i=n;i>0;i--) cout << i;
cout << endl;
}

C++: How do I make this shape from this code?

I am trying to make this shape from the code below. I'm confused as to how to make it print the 2nd row, second to last star without it skipping and printing the extra space before printing the star. Once that is figured out would the bottom half, when the stars expands back out, would the code be similar to the top half? I have tried a couple combinations of code between c and r but I have been stuck with what I currently.
---------------------- //row 0
* *| //row 1
* * * *| //row 2
* * * * * *|
* * * * * * * *|
* * * * * * * * * *|
* * * * * * * * * * *|
* * * * * * * * * *|
* * * * * * * *|
* * * * * *|
* * * *|
* *|
----------------------
#include <iostream>
using std::cout; using std::cin; using std::endl;
int main() {
cout << "Enter a positive odd number less than 40: ";
int num = 0;
int z = 1;
for (int a = 0; a < 3; ++a)
{
cin >> num;
if (num < 38 && num > 0 && num % 2 == 1)
{
cout << "Thank you!" << endl << endl;
for (int r = 0; r < num; ++r) //outer loop/rows
{
for (int c = 0; c < num; ++c) //inner loop/columns
{
if (r == 0) cout << "--"; //top of square
else if (c >= r + r - c && c < num - 1)
cout << " ";
//else if (c == num - 1) cout << "*|";
else if (r == num - 1) cout << "--"; //bottom of square
else if (c == num - 1) cout << "*|"; //right side of square
else if (r > c) cout << "* ";
}
cout << endl;
}
break;
}
else cout << "Please enter a positve odd number that is less than 40!" << endl;
}
cout << endl;
}
I just took two variables left=0 & right=num-1 and increased left & decreased right till r<=num/2, after that i reversed the process,when the col <= left or col >=right I printed *.
I hope it will be easy to understand.
Here is the code:
#include <iostream>
using std::cout; using std::cin; using std::endl;
int main() {
cout << "Enter a positive odd number less than 40: ";
int num = 0;
int z = 1;
for (int a = 0; a < 3; ++a)
{
cin >> num;
if (num < 38 && num > 0 && num % 2 == 1)
{
cout << "Thank you!" << endl << endl;
int left=0,right=num-1;
//for printing top line
for(int i = 0; i < num; i++) cout<<"- ";
cout<<"-"<<endl;
for (int r = 0; r < num; ++r) //outer loop/rows
{
//printing columns
for(int c = 0; c < num; c++)
{
if(c <= left || c >= right)
cout<<"* ";
else
cout<<" ";
}
if(r >= num/2) //checking for half of the rows
{
left--;right++;
}
else
{
left++;right--;
}
cout<<"|"<<endl;
}
//for printing last additional line
for(int i = 0; i < num; i++) cout<<"- ";
cout<<"-"<<endl;
break;
}
else cout << "Please enter a positve odd number that is less than 40!" << endl;
}
cout << endl;
}
This approach does it the math way.
Furthermore it draws a full frame with plus-chars at the edges.
Give it a try.
#include <iostream>
#include <cmath>
using std::cout; using std::cin; using std::endl;
int main() {
cout << "Enter a positive odd number less than 40: ";
int num = 0;
int z = 1;
for (int a = 0; a < 3; ++a) {
cin >> num;
if (num < 40 && num > 0 && num % 2 == 1) {
cout << "Thank you!" << endl << endl;
int center = ceil(num / 2.0);
for (int r = 0; r <= num+1; ++r) { //outer loop/rows
for (int c = 0; c <= num+1; ++c) { //inner loop/columns
if (r == 0 || r == num+1) {
if (c == 0 || c == num+1)
cout << "+"; // corner
else
//top or botton of square between corners
if (c == center)
cout << "-";
else
cout << "--";
}
else if (c == 0 || c == num+1) {
cout << "|"; // left or right frame
} else {
// inner part
if ((center-std::abs(center-r)) >= center-std::abs(center-c))
if (c < center)
cout << "* ";
else if (c > center)
cout << " *";
else
cout << "*";
else
if (c == center)
cout << " ";
else
cout << " ";
}
}
cout << endl;
}
} else
cout << "Please enter a positve odd number that is less than 40!" << endl;
}
cout << endl;
}
Just another way (with some more user input checking):
#include <iostream>
#include <string>
#include <limits>
#include <sstream>
using std::cout;
using std::cin;
using std::string;
const auto ssmax = std::numeric_limits<std::streamsize>::max();
const int max_dim = 40;
const int max_iter = 3;
int main() {
cout << "Enter a positive odd number less than " << max_dim << ": ";
int num = 0, counter = 0;
while ( counter < max_iter ) {
cin >> num;
if ( cin.eof() )
break;
if ( cin.fail() ) {
cout << "Please, enter a number!\n";
cin.clear();
cin.ignore(ssmax,'\n');
}
if ( num < max_dim && num > 0 && num % 2 ) {
cout << "Thank you!\n\n";
//top line
string line(num * 2, '-');
cout << line << '\n';
for ( int r = 0, border = num - 1; r < num; ++r ) {
cout << '*';
for ( int c = 1; c < num; ++c ) {
if ( (c > r && c < border) || (c < r && c > border) )
cout << " ";
else
cout << " *";
}
// right border
cout << "|" << '\n';
--border;
}
//bottom line
cout << line << '\n';
++counter;
} else {
cout << "Please, enter a positive odd number that is less than 40!\n";
}
}
cout << std::endl;
}
Or my favorite:
// top line
string line = string(num * 2, '-') + '\n';
cout << line;
// inside lines
int r = 0, border = ( num - 1 ) * 2;
string inside = string(border + 1, ' ') + "|\n";
// top
while ( r < border ) {
inside[r] = '*';
inside[border] = '*';
r += 2;
border -= 2;
cout << inside;
}
// center line
inside[r] = '*';
cout << inside;
// bottom
while ( border > 0 ) {
inside[r] = ' ';
inside[border] = ' ';
r += 2;
border -= 2;
cout << inside;
}
//bottom line
cout << line;

Convert integers to characters [duplicate]

This question already has answers here:
How to convert a number to string and vice versa in C++
(5 answers)
Closed 9 years ago.
I am trying to get my program to print letters instead of numbers. I used char c = static_cast<char>(N); to attempt to do this but it wont work, instead it prints character images that are not (a-z) How can I get the numbers to be printed as letters?
#include <cstdlib>
#include <iostream>
using namespace std;
// Function getUserInput obtains an integer input value from the user.
// This function performs no error checking of user input.
int getUserInput()
{
int N(0);
cout << endl << "Please enter a positive, odd integer value, between (1-51): ";
cin >> N;
if (N < 1 || N > 51 || N % 2 == 0)
{
cout << "Error value is invalid!" << "\n";
cout << endl << "Please enter a positive, odd integer value, between (1-51): ";
cin >> N;
system("cls");
}
cout << endl;
return N;
} // end getUserInput function
// Function printDiamond prints a diamond comprised of N rows of asterisks.
// This function assumes that N is a positive, odd integer.
void printHourglass(int N)
{
char c = static_cast<char>(N);
for (int row = (N / 2); row >= 1; row--)
{
for (int spaceCount = 1; spaceCount <= (N / 2 + 1 - row); spaceCount++)
cout << ' ';
for (int column = 1; column <= (2 * row - 1); column++)
cout << c;
cout << endl;
} // end for loop
// print top ~half of the diamond ...
for (int row = 1; row <= (N / 2 + 1); row++)
{
for (int spaceCount = 1; spaceCount <= (N / 2 + 1 - row); spaceCount++)
cout << ' ';
for (int column = 1; column <= (2 * row - 1); column++)
cout << c;
cout << endl;
} // end for loop
// print bottom ~half of the diamond ...
return;
} // end printDiamond function
int main()
{
int N = 1;
while (N == 1)
{
printHourglass(getUserInput());
cout << endl;
cout << "Would you like to print another hourglass? ( 1 = Yes, 0 = No ):";
cin >> N;
}
} // end main function
The letters are not numbered with A starting at 1 or anything like that. You're likely on an ASCII/UTF-8 system. So, in printHourglass, replace cout << N with
cout << static_cast<char>('A' + count - 1);
C functions, itoa
C++, using stringstream
boost::lexical_cast
Actually for your case, you can directly print it out. cout << N

Is there a way to observe values of variables in each iteration?

I'm new to learning how to code and I was wondering if there's a way to see values during each iteration of a loop. Here's a code I'm trying to understand. I know some of it but as it gets deeper, I get confused.
#include <iostream>
#include <string>
using std::cin; using std::endl;
using std::cout; using std::string;
int main()
{
cout << "Please enter your first name: ";
string name = "Jae";
const string greeting = "Hello, " + name + "!";
const int pad = 1;
const int rows = pad * 2 + 3;
const string::size_type cols = greeting.size() + pad * 2 + 2;
cout << endl;
for (int r = 0; r != rows; ++r) {
string::size_type c = 0;
while (c != cols) {
if (r == pad + 1 && c == pad + 1) {
cout << greeting;
c += greeting.size();
} else {
if (r == 0 || r == rows - 1 ||
c == 0 || c == cols - 1)
cout << "*";
else
cout << " ";
++c;
}
}
cout << endl;
}
}
You can debug your code. If you debug, you can see values during each iteration of a loop
if r is your iterator, write inside the loop this line:
cout << r;
Google is your friend.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide
Simply you can execute your program line-by-line by pressing F10 key. And hovering mouse over variables shows their current value.

While() Loop to create a border of asterisks around text?

Okay, I'm new at programming and decided to jump into this book called Accelerated C++. I'm only on the second chapter and I tried following the exercise, which is to create a program that asks for your name and then output it with a frame around it and padding.
When I execute it, it doesn't seem to be moving onto the next row. I'm guessing it's to do with my while() loop but I'm too dumb to figure out what it is exactly
// ask for a person's name, and greet the person
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::string;
int main()
{
// fetch name
cout << "Please enter your first name: ";
string name;
cin >> name;
// message
const string greeting = "Hello, " + name + "!";
// padding
const int pad = 1;
//desired rows/columns
const int rows = pad * 2 + 3;
const string::size_type cols = greeting.size() + pad * 2 + 2;
// seperate output from input
cout << std::endl;
// invariants
int r = 0;
string::size_type c = 0;
while (r != rows) {
while(c != cols) {
if (r == 0 || r == rows -1 || c == 0 || c == cols -1) { // if in bordering column or row
cout << "*"; //output *
} else {
if (r == pad + 1 && c == pad + 1) { //if on row for greeting
cout << greeting; // write greeting
c += greeting.size(); // adjust invariant
} else {
cout << " ";
}
}
++c;
}
++r;
cout << std::endl;
}
return 0;
}
Consider moving the column counter c to nearer where you use it, then as tuckermi says it will start at 0 for each row.
while (r != rows) {
string::size_type c = 0;
while(c != cols) {
At the bottom of your outer loop you need to reset the variable c to zero, otherwise it keeps its old value and won't re-enter the inner loop.
A good way to accomplish this is to move the definition/initialization of the variable into the beginning of the outer loop. That way c will be reinitialized before you start the inner loop each time.
You're almost there.
You need to clear c each row, and you need to take one off the greetings.size() to make it format properly (accounting for the fact you will be incrementing it later in the loop)
// ask for a person's name, and greet the person
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::string;
int main()
{
// fetch name
cout << "Please enter your first name: ";
string name;
cin >> name;
// message
const string greeting = "Hello, " + name + "!";
// padding
const int pad = 1;
//desired rows/columns
const int rows = pad * 2 + 3;
const string::size_type cols = greeting.size() + pad * 2 + 2;
// seperate output from input
cout << std::endl;
// invariants
int r = 0;
while (r != rows) {
string::size_type c = 0;
while(c != cols) {
if (r == 0 || r == rows -1 || c == 0 || c == cols -1) { // if in bordering column or row
cout << "*"; //output *
} else {
if (r == pad + 1 && c == pad + 1) { //if on row for greeting
cout << greeting; // write greeting
c += (greeting.size()-1); // adjust invariant
} else {
cout << " ";
}
}
++c;
}
++r;
cout << std::endl;
}
return 0;
}
http://ideone.com/mb9InW
Apart from reseting the variable c in the outer loop, you are not getting padding between the asterick and the message. So for that include the below code just after where you are printing the message.
for(int i = 0;i<pad;i++)
{
cout<<" ";
}