For this problem you will display the ASCII table from 0 to 255. The first 32 AS
ID: 3627785 • Letter: F
Question
For this problem you will display the ASCII table from 0 to 255. The first 32 ASCII values (0 to 31) are non-printable, so the value NP was hardcoded for those values. Outputting the char value of a decimal is easy in C++. All you need to do is cast the int as a char. For example:cout << char(33);
will display ‘!’ (without the single quotes) to the monitor. In the solution that created the above output, I used iomanip for formatting. To create the multiple rows and columns (i.e. the table configuration), you will need to use nested for loops. Since the console can only print across one line at a time and cannot go back to a line once it is displayed, you have to display the table by rows, NOT columns. This means you have to calculate row 0, column 0; row 0, column 1; row 0, column 2; …; row 0, column N before inserting a new line and going to row 1.
Let‘s say you have two constants for your table:
const int MAX_ROWS = 43;
const int MAX_COLS = 6;
Let’s also say that your outer for loop is for rows with an index of row and your inner for loop is for columns with an index of col. The row, column index is then
row + col * MAX_ROWS
For example, if you are on row 32 and column 4 then the calculation would be 32 + 4 * 43 which is 204. Remember that the first row is row 0 and the first column is column 0 (0-based indexing). You will have to experiment with the formatting and layout of the table.
Explanation / Answer
please rate - thanks
you didn't provide the table
hope this is good
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{const int MAX_ROWS = 43;
const int MAX_COLS = 6;
const int MAX_VAL = 255;
int row,col,num;
cout<<"NUM CHAR NUM CHAR NUM CHAR NUM CHAR NUM CHAR NUM CHAR ";
for(row=0;row<MAX_ROWS;row++)
{for(col=0;col<MAX_COLS;col++)
{num=row+MAX_ROWS*col;
if(num<=MAX_VAL)
{cout<<setw(3)<<right<<num<<setw(4)<<right;
if(num<32)
cout<<"NP";
else
cout<<char(num);
cout<<" ";
}
}
cout<<endl;
}
cout<<" Note:NP is a non printing character ";
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.