Multi-Dimension table.

Hello. I write a code with 4 column and 5 row.
The 10 row print correct but i don't know how to
print the column. Here's the code and the output of my codes.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream.h>
#include <conio.h>

main(){
clrscr();
int table [5][4],i,j;
        cout << "N\tN2\tN3\tN4";
        for (i=1; i<6; i++){
        cout << i;
        cout << endl;
        }
        getche();
}


Output:

N     N2     N3     N4
1
2
3
4
5


The output i want is:

N     N2     N3     N4
1    1       1        1
2    4       8        16
3    9      27       81 
4   16     64       256
5   25     125     625


The formula is;
Row 1: N * N, N * N2, N * N3, N * N4
Same with the other row.
Last edited on
Lines 9 is what you write to one row. For the header you do write four words separated by tabulators. For the other rows you should do the same. You do know what values you do want there.
For the column part is what i want to know how to do it. Can you start it?
You have almost everything, you simply have to add more thing to your cout. You could do something like this:
1
2
3
4
        for (i=1; i<6; i++){
            cout << i << "\t" << pow(i,2) << "\t" << pow(i,3);
            cout << endl;
        }


Or better, you could have a second loop inside your loop:
1
2
3
4
5
6
        for (i=1; i<6; i++){
            for (j=1; j<5; j++){
                cout << pow(i,j) << "\t";
            }
            cout << endl;
        }

Thank so much aleonard.
I'm trying to print arrays using printf but there's an error.

1
2
3
4
5
6
7
8
printf ("n\tN2\tN3\N4\n");
for (i=1; i<11; i++){
for (j=1; j<5; j++){
prntf (pow(i,j),"\t");
}
getche();
}
}


Incompatible type conversion in line 4.
Last edited on
Why did you change to using printf instead of ostream? Did you read what parameters printf expects?
Just trying to use printf. Is it possible?
I think you should read how to use printf if you plan to use it. In you case you would need to write something like this:

printf("%d\t", pow(i,j));

Read the doc of printf if you don't understand this line. Or, simply use cout, since you're doing c++ and it i much easier to use cout :)
Well the printf("%g\t",pow(i,j)); works but the column/row is only 1 line ( 1 1 1 1 ) and when i hit enter the 2nd row/column appear but it replace the first row/column. What i want is after i run the program the 10 row and 5 column will appear. Thank you for your reply aleonard.
You do call getche() in the outer loop. What does that function do?

aleonard's example prints a newline character in the outer loop.
Topic archived. No new replies allowed.