0
Write a program that reads 2 integers and draws a rectangle of corresponding dimensions.
modify the program such that in all casses the rectangle is always in horizontal position. meaning when the integers are switched the rectangle remains the same.
12 Answers
+ 3
All you would have to do to make sure the rectangle is always drawn horizontally is to add a simple if-statement to check if 'x', which denotes the height, is bigger than 'y', which is the length of the rectangle (tip: try to use clearer variable names), and if that is true, you simply swap their values. This ensures the length is always at least as great as the height (or bigger if we don't take a square into account), and the rectangle will be drawn accordingly.
+ 11
share ur attempt first
+ 3
please guys anyone to solve this for me
+ 2
Its your code:
https://code.sololearn.com/cA062nf98e4i/?ref=app
now what you want?
+ 2
Muhammad Hussein If x> y just replace the numbers.
+ 1
Better if u try it and if error comes we can helpđ
+ 1
include <iostream>
using namespace std;
int main()
{
int x,y,i,j;
cout<<"Enter two integers: "<<endl;
cin>>x>>y;
for(i=1; i<=x; i++)
{
for(j=1; j<=y; j++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
+ 1
okay thats my attempt.. i can print the rectangle but the modification part is where when you switch the inputs the rectangle is always in horizontal position i cant figure out
+ 1
the program will print a rectangle like this.. lets you enter 2 and 4
****
****
then when you switch 4 and 2 it print
**
**
**
**
Now on the part of modification, you modify the code to print just same position even when you switch the numbers.. i. e
2 & 4
****
****
4 & 2
****
****
it should remain the same.
//i dont know how to do this part
//maybe we can use if function
+ 1
do we have else in the if statement or we dont have
0
Why it won't happen ? It should be Muhammad Hussein .What do you want?
0
Here's my attempt to your challenge. Written in python
https://code.sololearn.com/c1pkOB6qYyk1/?ref=app