+ 2
How i can display this programmed?????
A B D E D A C D D A B D E
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.
+ 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.
+ 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.
+ 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;
}
+ 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.
0
@Jay add semicolons at last two lines...!
0
@Abhishek Thanks!
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...!
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?
0
@Aditya Thank you so much...!