A program has been written per the following directions: Take two lists of 5 pos
ID: 3695885 • Letter: A
Question
A program has been written per the following directions:
Take two lists of 5 positive integers each and places them in two arrays called "rowList" and "columnList". Afterwards, generate 5 rectangles of asterisks, each with dimensions taken from the previous two arrays provided. The 1st elements of "rowList" and "columnList" would generate the first rectangle, the 2nd elements of "rowList" and"columnList" would generate the second rectangle, and so on. The input values for "rowList" and "columnList" should only be between 2 and 7. The program must implement and use a function called "printRectangle", which takes two input parameters (one for row numbers, and one for column numbers), and generates a rectangle with the dimension specified by the parameters (row and column).
When run, the output is to read as follows:
SAMPLE RUN:
Enter 5 positive integers for rowList (2 to 7)
0: 3
1: 7
2: -2 Rejected!
2: 5
3: 4
4: 4
Enter 5 positive integers for columnList (2 to 7)
0: 9 Rejected!
0: 7
1: 6
2: 2
3: 4
4: 2
Rectangles generated
3 rows 7 columns
*******
*******
*******
7 rows 6 columns
******
******
******
******
******
******
******
5 rows 2 columns
**
**
**
**
**
4 rows 4 columns
****
****
****
****
In writing this program, two errors are reflected in the output. In using the input integers shown in the sample run, the first rectangle is expected to be 3 rows 7 columns. Instead of 3 rows, only 2 rows were shown and created for the first rectangle generated. Additionally, the number 0 is reflected under each rectangle. Please review the following program and correct all errors;
#include <iostream>
using namespace std;
int printRectangle (int a, int b){
int rows = 0;
int columns = 0;
while (rows < a){
columns = 0;
while (columns < b){
cout << "*";
columns = (columns + 1);
}
cout << endl << endl;
rows = (rows + 1);
}
}
int main() {
int columnList[5];
int rowList[5];
int counter = 0;
int x = 0;
int answer;
cout << "Enter 5 positive integers for rowList (2 to 7)" << endl;
while (counter < 5){
cout << counter << ": ";
cin >> rowList[x];
if ((rowList[x] < 2) || (rowList[x] > 7)){
cout << rowList[x] << " Rejected!" << endl;
}
else{
cout << rowList[x] << endl;
counter = (counter + 1);
}
x = (x + 1);
}
cout << endl << "Enter 5 positive integers for columnList (2 to 7)" << endl;
counter = 0;
x = 0;
while (counter < 5){
cout << counter << ": ";
cin >> columnList[x];
if ((columnList[x] < 2) || (columnList[x] > 7)){
cout << columnList[x] << " Rejected!" << endl;
}
else{
cout << columnList[x] << endl;
counter = (counter + 1);
}
x = (x+1);
}
cout << endl << endl << "Rectangles generated" << endl << endl;
x = 0;
int i = 0;
int j = 0;
while (x < 5){
while ((rowList[i] < 2) || (rowList[i] > 7)){
i = (i + 1);
}
while ((columnList[j] < 2) || (columnList[j] > 7)){
j = (j + 1);
}
cout << rowList[i] << " rows " << columnList[j] << " columns" << endl << endl;
answer = printRectangle(rowList[i],columnList[j]);
cout << answer;
i = (i + 1);
j = (j + 1);
x = (x + 1);
}
return 0;
}
Explanation / Answer
In main counter initialized with '0' and it gives in the cout<<counter<<":". if number of rows <2 only rejected condition occur on those situation only counter works properly.Remaining loops contain counter value only 0 increment doesn't perform.
give condition >=
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.