-
outputting a ASCII chart
hi, I want to know how to output an ASCII chart. I know how to output a chart like this:
1 2 3 4 5 6 7 8 9 10...
but how do I output a chart like this?:
1 6 11
2 7 12
3 8 13
4 9 14
5 10 15...
any suggestions? I know there are 256 ASCII characters and I don't want to use a matrix or arrays for this (just need to output the chart to save memory). thanks for any help.
-
Code:
#include <iostream>
using namespace std;
int main ( void )
{
unsigned char ch;
for ( int i = 0; i < 16; i++ ) {
ch = i;
for ( int j = 0; j < 16; j++ ) {
cout<< ch <<" "<<flush;
ch += 16;
}
cout<<endl;
}
return EXIT_SUCCESS;
}
-Prelude
-
Try to write out in your native language exactly what you would do if you were the computer. Then try to write the code.
there are 256 symbols with ASCII values from 0-255. That could correspond to 16 by 16 chart. the program will manipulate ints but display characters via the technique called casting. If you think of the table as grid with each element of the grid having an x and a y coordinate (x, y) and each element of the grid having a single value associated with it (result); then:
(0, 0) would be 0,
(0, 1) would be 16;
(0, 2) would be 32;
(0, 3) would be 48 ;
(1, 0) would be 1,
(2, 0) would be 2
etc.
With a little thought (that's all I'm capable of at the moment) this corresponds to a general equation of result = x + y*16.
Now try to change that set of english instructions (or whatever you come up with) into code:
int x;
int y;
int result;
for(x = 0; x < 16; x++)
{
for(y = 0; y < 16; y++)
{
result = x + (y * 16);
cout << (char)result << ' ';//or whatever casting method you use
}
cout << endl;
}
now put that into a program, compile, and evaluate. If lucky, you have desired effect. If not....well, you should be able to take it from here.