I'm learning and improving my programming skills from "Think Like a programmer" book and I was asked to display this kind of pyramid.
########
######
####
##
I did it with this code
for(int i = 0; i < 4; i++){
for(int k = 0; k < i; k++)
cout << ' ';
for(int j = 0; j < 8 - i * 2; j++)
cout << '#';
cout << '\n';
}
But... the questions was "Using the same rule as the shapes programs from earlier in the chapter (only two output statements—one that outputs the hash mark and one that outputs an end-of-line), write a program that produces the following shape:"
I'm not sure, but is it possible to display something like this with only 2 statements and without using space character?
edit.
Thanks for an answer guys. But according to author I should do this with only cout << '#'
and cout << '\n'
. And here is my point, because it seems that manipulating with some methods or functions is not an option.
Write a program that uses only two output statements, cout << "#" and cout << "\n", to produce a pattern of hash symbols shaped like a ... Of Course with use of loops :P
c - How to display character array? - Stack Overflow How to display character array? #include<stdio.h> int main () { char string [] = {1, 2, 3}; char* my_pointer = string; printf ("%c", *my_pointer); } Am expecting the ASCII character '1' to be displayed on screen. ASCII character '1' is 49 with binary representation as 00110001
In order to represent characters, the computer has to map each integer with a corresponding character using a numerical code. The most common numerical code is ASCII, which stands for American Standard Code for Information Interchange. How to declare characters? To declare a character in C, the syntax:
However, the char type is integer type because underneath C stores integer numbers instead of characters.In C, char values are stored in 1 byte in memory,and value range from -128 to 127 or 0 to 255. In order to represent characters, the computer has to map each integer with a corresponding character using a numerical code.
C uses char type to store characters and letters. However, the char type is integer type because underneath C stores integer numbers instead of characters.In C, char values are stored in 1 byte in memory,and value range from -128 to 127 or 0 to 255.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
for(int i = 0; i < 4; i++) {
cout << setw(8-i) << string(8-i*2, '#') << endl;
}
return 0;
}
Unfortunately, the answer to this question is much simpler: author error. Those first few questions should have been worded to allow the use of a single space output as well (cout << " ";). I don't know how we all missed this in editing, and I apologize for the confusion this has caused. In general, any exercise that is intended to force the reader to go "digging" will be clear on that point.
By the way, this and other issues from the first edition are discussed in the document that can be found on my site here. If you have further questions or problems with the book, please do contact me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With