i need to write a program in C++ which will ask the user for two integer values–
ID: 3630702 • Letter: I
Question
i need to write a program in C++ which will ask the user for two integer values–a starting value and an ending value. It will then display a chart for the products of every combination of the numbers from the starting value to the ending value.the program must ensure that the ending value is greater or equal to the starting value. It will continue to ask for a new ending value until an acceptable value is entered.
the program will utilize two new formatting functions to aid in the display of the chart. The functions are cout.width(), which allows for the width of the next field that is printed to be set. The other function is cout.fill(), which sets the fill character when the width of the field is greater than the amount of data that is to be displayed. You are required to use both of these functions.
The functions can be used like this:
cout.width(5);
cout.fill('-');
cout << 10 << endl;
cout.width(5);
cout << 9 << endl;
cout.fill(' ');
cout.width(5);
cout << 12 << endl;
output:
---10
----9
12
It is important to note that with these functions, the width is applied only to the next field that will be printed, while the fill is applied to all fields that have a width applied. It is certainly acceptable to set the fill to one value, then change it later to a different value.
general case
Please enter a starting value: 1
Please enter an ending value: 7
| 1 2 3 4 5 6 7
-----------|----------------------------------------------------------------------
1 | 1 2 3 4 5 6 7
2 | 2 4 6 8 10 12 14
3 | 3 6 9 12 15 18 21
4 | 4 8 12 16 20 24 28
5 | 5 10 15 20 25 30 35
6 | 6 12 18 24 30 36 42
7 | 7 14 21 28 35 42 49
Please enter a starting value: 11
Please enter an ending value: 7
Please enter an ending value: -1
Please enter an ending value: 11
| 11
-----------|----------
11 | 121
Please enter a starting value: -3
Please enter an ending value: 0
| -3 -2 -1 0
-----------|----------------------------------------
-3 | 9 6 3 0
-2 | 6 4 2 0
-1 | 3 2 1 0
0 | 0 0 0 0
The formatting is obviously crucial to your success on this assignment. All numbers are printed with a width of 10 set. There are no tabs used in the display of the output. The “row headers” also have a width of 10 for the number that is displayed, but are followed by a single space and a ‘|’. This means that the first row begins with 11 spaces followed by a ‘|’ and the second row has 11 dashes followed by a ‘|’.
You are required to declare an integer constant for the FIELD_WIDTH and use this constant when calling cout.width(). You are also required to declare a character constant called FILL_CHAR and use it when calling cout.fill() when you are drawing the horizontal line separator.
i need to to declare and use an integer constant called FIELD_WIDTH.
i need to declare and use a character constant called FILL_CHAR.
i need to make use of the cout.width() function.
i need to make use of the cout.fill() function.
i need to use at least one for loop and one do-while loop.
Explanation / Answer
please rate- thanks
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{const int FIELD_WIDTH=10;
const char FILL_CHAR='-',blank=' ';
int row,col,i;
int start,end;
do
{cout<<"Please enter a starting value: ";
cin>>start;
cout<<"Please enter an ending value: ";
cin>>end;
}while(end<=start);
cout<<" |";
for(i=start;i<=end;i++)
{cout.width(FIELD_WIDTH);
cout<<i;
}
cout<<endl;
cout.fill(FILL_CHAR);
cout<<"----------|";
for(i=start;i<=end;i++)
{cout.width(FIELD_WIDTH);
cout<<FILL_CHAR;
}
cout<<endl;
cout.fill(blank);
for (row=start;row<=end;row++)
{cout.width(FIELD_WIDTH);
cout<<row<<"|";
{for(col=start;col<=end;col++)
{cout.width(FIELD_WIDTH);
cout<<row*col;
}
cout<<endl;
}
}
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.