+ 2

write a program to obtain the following pattern A D C B E F G H I P O N M L K J # # # # # # # # # J K L M N O P I H G F E B C D A

23rd Sep 2016, 4:58 PM
Meena
Meena - avatar
9 ответов
+ 3
Just did this on the bus. output is right but obviously this is way too much code. this way, it'd be easier to just do this: cout<<"A"<<endl<<"DCB"<<endl<<"EFGHI"<<endl<<"PONMLKJ"<<endl<<"#########"<<endl<<"JKLMNOP"<<endl<<"IHGFE"<<endl<<"BCD"<<endl<<"A"; The above will yield the desired output in one cout statement but that'd be cheating really.
24th Sep 2016, 2:32 PM
Andres Mar
+ 2
#include <iostream> using namespace std; int main() { int A = 64; int x = 1; int y = 2; int z = 1; bool b = true; char ch; while (x>=1) { if (x==9) { cout<<"#########"<<endl; A+=1; y = -2; x += y; b = !b; z=-1; } else if (b) { int B = A + x * z; while (A!=B) { A+=z; ch=A; cout<<ch; } cout<<endl; x += y; b = !b; } else if (!b) { int B = A; A+=x*z; while(B!=A) { ch=A; cout<<ch; A-=z; } cout<<endl; A+=x*z; x+=y; b=!b; } } return 0; }
24th Sep 2016, 2:22 PM
Andres Mar
+ 2
Comments part 4 Now b is false, so the second else if statement starts running. Here, we want to count backwards, so now we copy A into B B = A; then, we want to count DOWN x steps so we take A and add x A += x; and count back to B in another while loop: while(A !=B) //as long as A and B are not equal { ch = A ; //get ASCII value of A cout<<ch; // write ch x times A -= 1 //A minus one } we have counted backwards, so after the loop, A is the same as before, so we add x back to A A += x; then, x += y; //x + 2 or after x==9 x-2 b = !b; // set b back to true and that's it really. just one more thing. after x==9 we want to count backwards. so whenever we did B = A + x ... A + 1 ..... or B =A ... A += x ... A - 1 in the loops, to count back to 65 after x==9, all + becomes - and all - becomes + like this: B = A - x ... A - 1 ... B = A + x ... A + 1 for this, we add one last variable z int z = 1; and set it to minus one in the if(x==9) z = -1; // we could also write z *= -1; now in the else if parts, we use z wherever we need + and - to change when we count back down. B += x; //becomes B +=x*z; //B += x*z is the same as B = B + (x*z) , z is always 1 or -1, when z is one, it doesn't do anything, x*1 is still x. when counting back down, z is minus one so x* -1 = -x , now B + (-x ) equals B - x , so B+=x*z with z = -1 is the same as B -= x That should explain it all, in case anyone was wondering. I've been in a car accident recently and now I've way too much time on my hands so I'm more than happy to answer any questions
25th Sep 2016, 3:46 PM
Andres Mar
+ 1
/************************ Comment section: A 'char' really holds a number that corresponds to a character on the ASCII table. Here, we need cpiral Letters A - P , ASCII code for A is 65, B is 66 and so on. (# is 35 but I didn't use it in the code) To output an A we can convert a number like this: int Num = 65; char ch; ch = Num; cout<<ch; So now we need this: 65 68 67 66 69 70 71 72 73 80 79 78 77 76 75 74 # # # # # # # # # 74 75 76 77 78 79 80 73 72 71 70 69 66 67 68 65
25th Sep 2016, 1:21 PM
Andres Mar
+ 1
Comments part 2 ...so I need to start with a line of one (65), then three (68 67 66) then five characters, so starting with ONE, every new line has TWO more characters, we do this: int x = 1; // starting with ONE int y = 2; // in the code do x += y so its 2 more but, every second row counts backwards (68 67 66) AND after x == 9 (the line with nine characters) we want the lines to go back to 7, then 5 , 3 and finally one character. ALSO after the line with 9 characters, we count backwards to 65. the line with x==9 is different too because in that line there are no Letters, just a '#', nine times. so after we declared our variables (int A = 64; char ch; int x= 1; int y = 2, there will be more later) the first thing we do is check wether the line is the one with nine characters: if(x==9) { //code... } what we do here is cout<<"# # # # # # # # #"<<endl; to write the nine '#' characters Then, y = -2; remember we start with x=1 and then we add y=2 so the number of characters in every line is two more. now at x==9, y becomes -2. the same code that we used to count upwards (x += y) will now count back for us, because x + -2 is like x-2. so now we get 1 3 5 7 9 //write ######### , y = -2 to count back 7 5 3 1 -1 -3 -5 ... ...now the loop goes on forever, first x+y counts up in steps of two, then at x==9, y becomes -2 and we count down forever. so the first thing after declaring our variables should be while(x>=1) { //all code goes here } this way, our code will run only as long as x is one or more than one, and as soon as x is negative the code stops. then inside the while loop we have three parts: checking if x==9 and two more things.
25th Sep 2016, 2:09 PM
Andres Mar
+ 1
comments part 3 we've checked for (x==9). Now we need the remaining code. Every other line needs to be reversed. We can add a boolean variable bool b = true; b can only have two values, true and false. so if we do this if(x==0) {//code} else if(b) { //some code b = !b; } else if(!b) { //some code b = !b; } this way, we start with b = true, so the first else if statement with else if(b) is run. inside the first else if, we do b = !b. this means, if b is true, it becomes false, if b is false, it becomes true. so inside the first else if, b is set to false. the second else if will run if b is false else if (!b) and inside that else if , b is set to true, so the two will run one after the other as long as x is one or greater, and the first if statement will run if x is 9. In the first else if, we will do the normal counting, in the second, the backwards counting. so in the else if (b) We take x (the number of characters in the line) then we make a temporary variable int B, that only exists inside the else if. We want to count up from A, so we take A plus the number of steps we want to count (x) and put the result in B int B = A + x; then while (A!=B) //while A and B are different { A +=1; //A plus one ch = A; //turn A into an ASCII character cout<<ch; //write ch } after the loop has run x times, we do cout<<endl; //new line x += y; // x plus two, or minus two after x==9 b = !b; change b from true to false
25th Sep 2016, 3:44 PM
Andres Mar
0
OVO
23rd Sep 2016, 9:57 PM
Nicholas Andrew Gutierrez
Nicholas Andrew Gutierrez - avatar
0
Andres pls ans my remaining questions too
25th Sep 2016, 5:57 PM
Meena
Meena - avatar
0
1 is the answer
24th Jul 2021, 6:09 AM
Judson Leo
Judson Leo - avatar