How i can display this programmed????? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How i can display this programmed?????

A B D E D A C D D A B D E

29th Nov 2016, 2:03 PM
Nikhil
Nikhil - avatar
10 Answers
+ 5
Hey, it is better with logic #include <iostream> #define x(a) static_cast<char>(a) using namespace std; int main() { int i,j; for(i=0;i<4;i++) { for(j=65;j<70;j++) { if(j==67&&i==0) continue; if(i==2&&j==67) continue; cout << x(j) << " "; if(i==1&&j==65) { j=j+1; cout << " "; } if(i==1&&j==68) { break; } } if(i==2) break; cout << "\n"; cout << " D \n"; } return 0; } You can also create that with three for loop. Anyway, hope you like it.
29th Nov 2016, 4:02 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 4
Hey, Abhishek. You can use static_cast for downward casting if you know there is an object. I have you it because at 65 it will give me A not anything else. But When you try for other lIke 256, which is not a ASCII character. It will show you an undefined behavior. Yeap, you are right on that but, here I know the object, so no need for run time checking. Moreover, sorry for late reply.
30th Nov 2016, 2:42 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 2
Hey, here #include <iostream> using namespace std; int main() { char a,b,c,d,e; int i; a='A'; b='B'; d='D'; e='E'; for(i=0;i<5;i++) { if(i==1||i==3) { a=b=c=e=' '; } else if(i==2) { b=' '; d='C'; e='D'; } cout << a << " " << b << " "<< d <<" "<< e; a='A'; b='B'; d='D'; e='E'; cout << "\n"; } return 0; } I know it is bit silly, but I am trying with different method. Please wait for that if I did not make it I will reply.
29th Nov 2016, 2:27 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 2
#include <iostream> using namespace std; int main (){ cout <<"A B D E" << endl ; cout <<" D" << endl ; cout <<"A C D" << endl; cout <<" D" << endl; cout <<"A B D E" ; return 0; }
29th Nov 2016, 2:37 PM
Jay
Jay - avatar
+ 2
Hey, Abhishek. You can use dynamic_cast. Where you don't know the object is available. I am talking about downcast. If your argument is not polymorphic then you cannot use dynamic_cast. Like struct Base { }; struct Derived : Base { }; int main() { Derived d; Base *b = &d; dynamic_cast<Derived*>(b); // Invalid } Moreover, It is used when you need to check at run time and if object is not available it will return a null pointer. But for upcast both are good option.
30th Nov 2016, 4:04 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
0
@Jay add semicolons at last two lines...!
29th Nov 2016, 3:54 PM
Abhishek Kumar
Abhishek Kumar - avatar
0
@Abhishek Thanks!
29th Nov 2016, 3:55 PM
Jay
Jay - avatar
0
@Aditya Kumar Pandey,@AllSoloLearners It is good to think about some alternative ways and @Aditya you have created a nice logical solution. It's obvious that this piece of code is to encourage others to think beyond the conventional ways and it is not concerned with time space complexity, but I have one question, as "static_cast" is not preferred for downward casting because of its unpredictable behavior in such situations should we use it in this case or should we go for "dynamic_cast" ? or am I forgetting something about this casting? It would be great if you or anyone else can help me out in it. I will surely go through text and other information sources but your precious comments are expected...!
29th Nov 2016, 4:54 PM
Abhishek Kumar
Abhishek Kumar - avatar
0
@Aditya Kumar Pandey: Thank you for your reply...! But Aditya could you please tell me if dynamic cast is more appropriate in this situation or something else...It would be really great if we will discuss this casting methodology here....It will be of a great help for me as well as for others who may be interested in it. And one more thing, I'm new at solo learn so want to know if we could add tags in our comments or codes so as it would be displayed in concerned sections as well?
30th Nov 2016, 2:52 AM
Abhishek Kumar
Abhishek Kumar - avatar
0
@Aditya Thank you so much...!
30th Nov 2016, 5:25 AM
Abhishek Kumar
Abhishek Kumar - avatar