C++: Displaying characters - c++

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

#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.

You can check the position in a loop for every line. This combined with a 3 operand operator could solve this with two output statement.
for(int i = 0; i < 4; i++){
for(int k = 0; k <= 8-i; k++)
cout << k<i ? ' ' : '#';
cout << endl;
}
Leading space characters will be there anyway, You can not make an "empty" space instead of them.

I don't know if it's about the "art" of not using cout << ' ', but in that case there is another neat solution:
#include <iostream>
int main() {
for (int i = 0; i < 4; ++i, std::cout << '\n') {
for (int j = 0; j < 8-i; ++j) {
std::cout << (char)('#' - 3*(j<i));
}
}
}
And, another, even more obscure version that uses the ASCII representation rather than writing ' ' explicitly, using only one for-loop:
int main() {
for (int i = 1; i < 37; ++i) {
std::cout << (char)(35-3*((i%9)<=((i-1)/9)) - 3*((i%9)>=(9-(i-1)/9)) \
- 22*(!(i%9)) );
}
}
I do not say this is good programming practice, but maybe it helps someone in realizing - again - that it's in the end all numbers and registers.

Related

How to properly understand the carriage return (a.k.a. \r)?

I have seen this carriage return example online about a loading effect but I am failing to understand it properly. Why does it have to be 2 \rLoading and not one? Can someone explain it to me?
for (int j = 0; j < 3; j++) {
cout << "\rLoading \rLoading";
for (int i = 0; i < 3; i++) {
cout << ".";
sleep(300);
}
}
The first section
\rLoading____
is printed to have the string "Loading" and three empty spaces at the beginning of the line. The next carriage return then sets the cursor to the beginning of the line. Then
Loading
is printed again, but the cursor is now directly behind the word, at the first of the three spaces. Now here:
for (int i = 0; i < 3; i++) {
cout << ".";
sleep(300);
}
three dots are printed in an interval of 300 seconds each into the places, where the three dots are.
This whole procedure is iterated three times, so the main purpose of the three blanks of the first "Loading" is, to delete the dots from the previous iteration.
The key is that \r will not clear characters which were printed on the screen earlier. So the first \rLoading act as a display eraser.
In fact you can use 10 spaces instead of the Loading , but you must count it accurately, which is not intuitional.
The following is the optimized code, which can be directly compiled and run on an modern x86 & linux machine. You can try to delete one of the \rLoading and see what will happen for easily understanding.
#include <iostream>
#include <unistd.h>
int main(int argc, char* argv[]) {
for (int j = 0; j < 3; j++) {
std::cout << "\rLoading \rLoading" << std::flush;
// std::cout << "\r \rLoading" << std::flush; // same effect
for (int i = 0; i < 3; i++) {
std::cout << "." << std::flush;
sleep(1);
}
}
std::cout << std::endl;
return 0;
}
Two promotions:
a std::flush is needed or you will not see the effects
300 seconds is too long -> 1 seconds

Modifying previous line c++

I am currently trying to make the Conway's game of life in c++ for a school project. The problem is I don't know how to make the board more "dynamic". How can I return several lines to change the output I made before? Right now I have to print various boards which isn't exactly aesthetic.
Looking this up on previous questions I could only find "/r" which helps if the board is only one line, but in my case it isn't.
Edit: Added code sample (if it helps).
for (int temp = 0; temp < iterations; temp++){
for(long long i = 0; i < height; ++i){
for(long long j = 0; j < width; ++j){
if (CurrentGen[i][j]){
cout << "â– " << " ";
}
else{
cout << "." << " ";
}
}
cout << "\n";
}
cout << "---------------------------------------" << endl;
CurrentGen = NextGen(CurrentGen, height, width);
sleep(1);
}
This will end up being more than complicated solution for the system dependent code.
However the easy and quick solution is for visually having the same line being updated is to create many empty lines for every loop iteration.
for (int n = 0; n < 50; n++)
{
std::cout << "\n";
}
As I said earlier this is the best C++ could do when there is no system information available, using only the standard commands.
There are other ways to do it such as:
system call to clear the terminal
but this method not safe
The standard way to print end lines
Using the ncurses library #include <curses.h>
Advantage: it is a cross-platform
Disadvantage: cannot be mixed with standard I/O

Trying to create a better loop for naming array elements

I have started studying arrays and have just started making some practice but I am having some problems with using loops to name the elements inside of a specific array.
I was trying to make this piece of code that assigned the numbers from 1 up to 12(to resemble the months of the year) to the ints inside of the array, this is what I came up with:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int array[12];
for (int i = 0; i < 12;) {
cout << "Month number " << i + 1 << endl;
array[i] = (i++);
}
return 0;
}
What I don't like about this is the fact that I had to leave the increment/decrement space inside of the for loop empty. I had initially tried making the code look something like this:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int array[12];
for (int i = 0; i < 12; i++) {
cout << "Month number " << i + 1 << endl;
array[i] = i++;
}
return 0;
}
But this way, even if the first element of the array came out correct, the subsequent ones didn't. I think the reason for this is that the i++ in the last statement of the loop makes the value of i increment but I couldn't find a way around it without having to add another line with i-- or doing as I did in the first code I posted.
Could anyone offer me a hand in understanding how to make it so that i can store the value of i, incremented by one, inside of that specific array element, without incrementing it for the whole loop(if it is possible)?
I know there are ways around it, just like I showed in the first code i posted, but it's something that's bugging me and so I would like to make it more visually pleasing.
Please, keep in mind that I am just a beginner :)
Thanks in advance for the answers, and sorry for the long question.
Edit: Apparently, coding like this:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int array[12];
for (int i = 0; i < 12; i++) {
cout << "Month number " << i + 1 << endl;
array[i] = i + 1;
}
cout << array[4] << endl;
return 0;
}
makes it so that the program works correctly and looks like I wanted, but I can't comprehend why it does :(
Edit 2: Apparently, as UnholySheep pointed out, I missed on the fact that + 1 does not modify the value of the integer, while ++ does.
Thanks to everyone that answered and explained how ++ and +1 work!
Simply do i+1 again.
for (int i = 0; i < 12; i++)
{
cout << "Month number " << i + 1 << endl;
array[i] = i + 1;
}
Now it's obvious you actually want to start at 1 and go to 12, so this seems somewhat better with less repetition:
for (int i = 1; i <= 12; i++)
{
cout << "Month number " << i << endl;
array[i-1] = i;
}
EDIT: As for your edit, the reason why this works is because i++ operator works on the particular i variable. It increments that existing i by one, making it so that the next time you access i, it will be 1 more than it was before.
Writing i+1, on the other hand, creates a completely new, temporary, variable (actually a constant). So when you write
array[i] = i+1;
you're saying that you want i to remain unchanged, but you want to create a new number, one bigger than i, and put that new number into the array.
You can even write it out longer to be completely explicit:
int newNumber = i+1;
array[i] = newNumber;
for (int i = 0; i < 12; i++) {
cout << "Month number " << i + 1 << endl;
array[i] = i+1;
}
No reason to increment i in the loop

using arrays to solve an equation

so i wrote this code in c++ to solve this equation (x+y+z=30) where each of these variables has a limited amount of possible of values (1,3,5,7,9,11,13,15) so repetition is allowed and here's my code :
#include <iostream>
using namespace std;
int main()
{
int x[8]={1,3,5,7,9,11,13,15};
int y[8]={1,3,5,7,9,11,13,15};
int z[8]={1,3,5,7,9,11,13,15};
for (int i=0; i<8; i++)
{
for (int j=0; j<8; j++)
{
for (int k=0; k<8; j++)
{
if (x[i]+y[j]+z[k]==30)
{
cout << x[i] << "\n" << y[j] << "\n" << z[k]<< "\n"<< endl;
break;
}
}
}
}
}
now i don't know if this is the right way to approach it (I'm a beginner) but still this program did okay since it gave set of three number that did equal to 30 but it didn't stick to the possible values e.g (7,22,1), now that what you see their is the best i could come up with other attempts or fixes just made things worse e.g crashing or what so ever.
if you could help that would be great and most importantly tell me where i went wrong as this whole purpose of this is to learn not solve the problem.
thank you so much in advance !
You are using break statement which only breaks one of the loops. You have nested loops in your program, so i would recommend you to use goto: instead.
for (int i=0; i<8; i++)
{
for (int j=0; j<8; j++)
{
for (int k=0; k<8; j++)<----- it should be k++
{
if (x[i]+y[j]+z[k]==30)
{
goto stop;
}
}
}
}
stop:
cout << x[i] << "\n" << y[j] << "\n" << z[k]<< "\n"<< endl;
I actually ran the code and there is 2 more problems:
As mentioned on the answer below, it 3 odd numbers never add up to 30;
variables i , j and k need to be global variables. So initialize them before using in loops. Then it should work perfectly(if the number isn't even).
I don't see 22 in the values you initialized the arrays with. You also can just use one array with the possible values; 3 arrays are not needed.
I see that you only have odd integers as possible values. 3 odd integers can never sum up to an even integer like 30, so there is no solution to your problem as stated. The one solution you provided has 22 as one value, an even integer.

Printing elements of an array in C++, and inner versus outer initialization of variable [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I'm having a super simple problem, but I also had a question, so I figured I'd post both.
First of all, I'm not sure what's not working about this program cycling through an array and out putting the values. What's happening now, is that it just outputs the last value in the array.
int myArray[10] = {0,1,1,2,3,5,8,13,21,34};
int i = 0;
for( i = 0; i < 9; i++);
{
cout << i + 1 << '\t' << myArray[i] << endl;
}
that's not working for some reason, but I also wanted to know why, if I initialize the i variable inside the for loop, it says myArray[i] inside of the for loops, at the cout, isn't initialized at all. Peculiar to me.
I just tested the following code and it worked:
#include <iostream>
using namespace std;
int main() {
int myArray[10] = {0,1,1,2,3,5,8,13,21,34};
for(int i = 0; i < 10; i++) {
cout << i + 1 << '\t' << myArray[i] << endl;
}
}
Notice the syntactical changes that were made. Also you were previously leaving out the last element in the array.
What's important to note is that a semicolon is a null statement. By placing a semicolon after a for loop, you are executing the null statement (i.e. do nothing) for the total number of iterations through the loop. Afterwards, the code inside the curly braces is run as if it were regular code in the body of your method. That's why only the last iteration was printing, because the for loop had "done nothing" for all the other iterations but still incremented i. Therefore, when the code within the braces ran, it did so for the last expected iteration.
for( i = 0; i < 9; i++);
// ^here
Remove the extra semicolon. The original code in your question is equivalent to:
for( i = 0; i < 9; i++)
{
//do nothing
}
{
cout << i + 1 << '\t' << myArray[i] << endl;
}
Now you can declare i inside the for loop:
for( int i = 0; i < 9; i++)
{
cout << i + 1 << '\t' << myArray[i] << endl;
}
You have an extra semicolon here:
for( i = 0; i < 9; i++); // there should be no semi-colon at the end of this line.
// ^ remove this!
What's happening is that the compiler iterates the loop completely, running the "empty" loop body (that extra semicolon). After the loop is done, it reaches your cout statement.
for( i = 0; i < 9; i++);
// ^ THIS TINY PIECE OF ABOMINATION
{
cout << i + 1 << '\t' << myArray[i] << endl;
}
Remove it.
The block below the for statement isn't associated with it because the for is actually associated with a null statement (indicated by the ; immediately after the for()).
You should also put your declaration of i inside for
for(int i = 0; i < 9; i++) {
...
That way, you limit its scope and makes you less prone to some errors.