我正在使用嵌套的for-loops打印出一個指數數字表的項目。用戶指定要打印的行數和功率數量。例如,如果用戶指定2行和3個權力,程序應該打印1,1,1和2,4,9(2^1,2,3等)。我應該注意到這是針對課堂的,我們不允許使用cmath,否則我會使用pow()。我似乎無法找出嵌套for循環中的正確函數,它可以更改基本值和指數值。這是迄今爲止我所擁有的。謝謝你的幫助!使用C++創建權力表
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int r, p, a;
cout << "The program prints a table of exponential powers.\nEnter the number of rows to print: ";
cin >> r;
cout << "Enter the number of powers to print: " ;
cin >> p;
cout << endl;
for (int i = 1 ; i <= r; i++)
{
cout << setw(2) << i;
for (int q = 1; q <= i; q++)
{
a = (q * q); //This only works for static numbers...
cout << setw(8) << a;
}
cout << endl;
}
}
您的預期模式,第一行中的'1 1 1'和第二行中的'2 4 9'沒有意義。第二行中是否指「2 4 8」? '2^1 2^2 2^3'? –
嘗試更簡單的方法:打印*一行*長度可變的程序。提示:不要單獨計算每個功率。 – Beta
[工作代碼](http://coliru.stacked-crooked.com/a/57e7a0eaf621bb08)。 –