I have a program that prints a table of exponents. It works completely except the columns of numbers aren't lined up right. For example, with input 5 and 4 the program is supposed to print:
#include <iostream>
#include <iomanip>
// this one always prints 1 before the *CORRECT RESULT* then leaves off the final result at the other end
usingnamespace std;
int pow(int b, int e) {
if (e == 0) {
return 1;
}
else {
return b * pow(b,e-1);
}
}
// if putting the table code in pow doesn't work, insert table function here
int table(int var, int yar) {
for (int r = 0; r < var; r++) {
cout << left;
for (int c = 1; c <= yar; c++) {
cout << " ";
cout /*<< left*/ << setw(6) << pow(r + 1, c);
}
cout << endl;
}
return 1;
}
int main() {
int rows;
int columns;
cout << "This program prints a table of exponential powers." << endl;
cout << "Enter the number of rows to print: ";
cin >> rows;
cout << "Enter the number of powers to print: ";
cin >> columns;
table(rows, columns);
cout << endl;
pow(rows, columns);
}
I went searching online for advice on this which is what directed me to put in the << left, which I didn't know existed. Changing << left to right FIXES the problem, only the output is shifted several spaces to the right.
Can someone please help? This is for an assignment which has a very picky online grader so it has to be exactly as it expects.
int table(int var, int yar) {
for (int r = 0; r < var; r++) {
for (int c = 1; c <= yar; c++) {
cout << setfill (' ')<< setw(6)<< pow(r + 1, c);
}
cout << endl;
}
return 1;
}
#include <iostream>
#include <iomanip>
// this one always prints 1 before the *CORRECT RESULT* then leaves off the final result at the other end
usingnamespace std;
int pow(int b, int e) {
if (e == 0) {
return 1;
}
else {
return b * pow(b,e-1);
}
}
// if putting the table code in pow doesn't work, insert table function here
void table(int var, int yar) {
for (int r = 0; r < var; r++) {
cout << r+1;
for (int c = 1; c <= yar; c++) {
/* cout << " "; */
cout << setw(2);
cout << setfill(' ') << setw(7) << pow(r + 1, c);
}
cout << endl;
}
return;
}
int main() {
int rows;
int columns;
cout << "This program prints a table of exponential powers." << endl;
cout << "Enter the number of rows to print: ";
cin >> rows;
cout << "Enter the number of powers to print: ";
cin >> columns;
table(rows, columns);
cout << endl;
pow(rows, columns);
}
#include <iostream>
#include <iomanip>
// this one always prints 1 before the *CORRECT RESULT* then leaves off the final result at the other end
usingnamespace std;
int pow(int b, int e) {
if (e == 0) {
return 1;
}
else {
return b * pow(b,e-1);
}
}
// if putting the table code in pow doesn't work, insert table function here
void table(int var, int yar) {
for (int r = 0; r < var; r++) {
// cout << r+1;
for (int c = 1; c <= yar; c++) {
/* cout << " "; */
// cout << setw(2);
cout << setfill(' ') << setw(8) << pow(r + 1, c);
}
cout << endl;
}
return;
}
int main() {
int rows;
int columns;
cout << "This program prints a table of exponential powers." << endl;
cout << "Enter the number of rows to print: ";
cin >> rows;
cout << "Enter the number of powers to print: ";
cin >> columns;
table(rows, columns);
cout << endl;
pow(rows, columns);
}
This program prints a table of exponential powers.
Enter the number of rows to print: 5
Enter the number of powers to print: 10
1 1 1 1 1 1 1 1 1 1
2 4 8 16 32 64 128 256 512 1024
3 9 27 81 243 729 2187 6561 19683 59049
4 16 64 256 1024 4096 16384 65536 262144 1048576
5 25 125 625 3125 15625 78125 390625 1953125 9765625