My tutor said that my output is different from his expected output.(As shown in the photo attached). So, can someone give me the code the produce the three expected answers in the photo?
where's your code ... it'd take me only a few seconds to figure out in your code where you went wrong instead of me redoing the whole thing for you.
so in the down one you had an infinite loop. Really you're using a matrix to draw the shapes instead of printing spaces a and symbols. I changed the matrix part to have a \0 instead of a space but that won't really fix it.
Really you need to stop printing when you hit the last symbol and put a newline there. That will involve a much larger rewrite.
Answers & Comments
Verified answer
where's your code ... it'd take me only a few seconds to figure out in your code where you went wrong instead of me redoing the whole thing for you.
so in the down one you had an infinite loop. Really you're using a matrix to draw the shapes instead of printing spaces a and symbols. I changed the matrix part to have a \0 instead of a space but that won't really fix it.
Really you need to stop printing when you hit the last symbol and put a newline there. That will involve a much larger rewrite.
#include <iostream>
using namespace std;
int main( )
{
cout << "Triangle size: ";
int h, i, j;
if ( !(cin >> h) || h < 2 || h > 20 ) return 0;
char *m= new char[h*h];
for ( i= 0; i < h; i++ )
for ( j= 0; j < h; j++ )
m[i*h+j]= j < h-i-1? '\0' : '&';
cout << "Triangle orientation [r, d, u, l]: ";
char en;
if ( !(cin >> en) ) return 0;
switch ( en )
{
case 'l':
for ( i= 0; i < h; i++ )
{
for ( j= 0; j < h; j++ )
cout << m[j*h+i];
cout << endl;
}
while ( --i > 0 )
{
for ( j= 0; j < h; j++ )
cout << m[j*h+i-1];
cout << endl;
}
break;
case 'r':
for ( i= 0; i < h; i++ )
{
for ( j= 0; j < h; j++ )
cout << m[(h-j-1)*h+i];
cout << endl;
}
while ( --i > 0 )
{
for ( j= 0; j < h; j++ )
cout << m[(h-j-1)*h+i-1];
cout << endl;
}
break;
case 'u':
for ( i= 0; i < h; i++ )
{
for ( j= 0; j < h; j++ )
cout << m[i*h+j];
while ( --j > 0 )
cout << m[i*h+j-1];
cout << endl;
}
break;
case 'd':
for ( i= 0; i < h; i++ )
{
for ( j= 0; j < h; j++ )
cout << m[(h-i-1)*h+j];
while ( --j > 0 )
cout << m[(h-i-1)*h+j-1];
cout << endl;
}
break;
default:
break;
}
delete[] m;
return 0;
}