gpt4 book ai didi

c++ - for循环不通过数组C++的表递增

转载 作者:太空宇宙 更新时间:2023-11-04 15:59:05 25 4
gpt4 key购买 nike

<分区>

我已经分别测试了我的数组,以确保它可以正常工作并保存所有值,但由于某种原因,当我通过 for 循环运行它时,它只打印出 10.95 的 3 行 3 列,我不知道不明白为什么它不从我的表中提取其余值。

这是作业:

Write, compile, and run a C++ program to input the following values into an array named prices: 10.95, 16.32, 12.15, 8.22, 15.98, 26.22, 13.54, 6.45, and 17.59 After the data has been entered, have your program display the values in 3 rows and 3 columns.

这是我写的代码:

#include <iostream>
#include <iomanip>
using namespace std;

const int COLS = 3;
const int ROWS = 3;

int main()
{
const int num_items = 9;
float prices[num_items];

cout << "Enter the prices of your " << num_items << " items: ";
cin >> prices[0];
cin >> prices[1];
cin >> prices[2];
cin >> prices[3];
cin >> prices[4];
cin >> prices[5];
cin >> prices[6];
cin >> prices[7];
cin >> prices[8];

float table[ROWS][COLS] = {{prices[0], prices[1], prices[2]},
{prices[3], prices[4], prices[5]},
{prices[6], prices[7], prices[8]}};

cout << "The prices you have entered are:\n";

for (int x = 0; x < ROWS; x++)
{
for (int y = 0; y < COLS; y++)
{
cout << setw(6) << table[ROWS][COLS] << " ";
}
cout << endl;
}
return0;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com