關閉。這個問題需要更加集中。它目前不接受答案。
想改善這個問題嗎?更新問題,使其僅通過編輯這篇文章來關注一個問題。
15 小時前關閉。
改進這個問題我在二維陣列上做了一個乘法表,但我也想改變這個代碼并對我輸入的 5 個值進行。
我會輸入:
2.5 3.0 4.6 6.3 8.1
2.5
3.0
4.6
6.3
8.1
它將乘以 2.5 * 2.5 等。
int tab[5][5];
for(int i=1; i<=5; i )
{
for(int y=1; y<=5; y )
{
tab[i-1][y-1]=i*y;
cout << tab[i-1][y-1] << " | ";
}
cout << endl;
}
有關如何執行此操作的任何提示?
uj5u.com熱心網友回復:
好的,現在假設這個 2D 陣列始終是正方形,并且列值與行值相同(如您在示例中所示)。
您將需要存盤我們想要相乘的這些值。我們稱這個陣列為 values x。
您不必對當前代碼進行太多更改,但i*y我們想要做的不是x[i] * x[y]。注意:您不能從 1、開始i和y從零開始回圈。此外,您還需要在索引時去掉“-1”tab[i][y]
哦,我差點忘了。如果要使用小數,int則不能使用。使用float來代替。您可能需要使用我將在下面展示的一些技巧將它們四舍五入為 1 位小數:
float tab[5][5];
float x[5]; // You will have to give x the 5 values that you want to multiply
for(int i=0; i<=4; i )
{
for(int y=0; y<=4; y )
{
tab[i][y] = x[i] * x[y]; // This can have many decimals!
tab[i][y] = roundf(tab[i][y] * 10) / 10; // This will multiply with 10 to shift the number 1 decimal place, then we round it to zero decimals, then we divide it with 10, to add 1 decimal. You can change to 100 if you want 2 decimals
cout << tab[i][y] << " | ";
}
cout << endl;
}
希望這可以幫助!^_^
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/391008.html
下一篇:我不明白為什么break不起作用
