c+中的嵌套循环+;和数组 我用C++编写这个程序,无法确定我做错了什么。这个嵌套循环应该打印一个包含行和列的矩阵,由于某种原因,当请求用户输入时,它会被固定在第0行和第0列。提前谢谢 #include <iostream> using namespace std; int main () { //program to print the input values in rows and columns of two dimensional array in revserse roder using loop //decalre the vraibles and array int array [3][3],rows,cols,maxrows = 3,maxcols = 3; //use for loop to intiiate the array and input values from users for (rows = 0; rows < maxrows;rows = rows++ ) { for (cols = 0; cols < maxcols;cols = cols++ ) { cout << " Enter the value for array location : " << " [ " << rows << " , " << cols << " ] " ; cin >> array [rows][cols] ; } } //display the values entered in array starting with bottom row first and then the rest for ( rows = 0 ; rows < maxrows ; rows ++ ) { for ( cols = 0 ; cols < maxcols ; cols ++ ) { cout << " The values that were entered in this array starting with bottom row are " << array [rows][cols] ; } } } #包括 使用名称空间std; int main() { //使用循环打印revserse roder中二维数组行和列中的输入值的程序 //贴花的vraibles和阵列 int数组[3][3],行,列,maxrows=3,maxcols=3; //使用for循环初始化数组并从用户输入值 对于(行=0;行
是不明确的,因为在获取值后,右侧会递增,然后分配给左侧c+中的嵌套循环+;和数组 我用C++编写这个程序,无法确定我做错了什么。这个嵌套循环应该打印一个包含行和列的矩阵,由于某种原因,当请求用户输入时,它会被固定在第0行和第0列。提前谢谢 #include <iostream> using namespace std; int main () { //program to print the input values in rows and columns of two dimensional array in revserse roder using loop //decalre the vraibles and array int array [3][3],rows,cols,maxrows = 3,maxcols = 3; //use for loop to intiiate the array and input values from users for (rows = 0; rows < maxrows;rows = rows++ ) { for (cols = 0; cols < maxcols;cols = cols++ ) { cout << " Enter the value for array location : " << " [ " << rows << " , " << cols << " ] " ; cin >> array [rows][cols] ; } } //display the values entered in array starting with bottom row first and then the rest for ( rows = 0 ; rows < maxrows ; rows ++ ) { for ( cols = 0 ; cols < maxcols ; cols ++ ) { cout << " The values that were entered in this array starting with bottom row are " << array [rows][cols] ; } } } #包括 使用名称空间std; int main() { //使用循环打印revserse roder中二维数组行和列中的输入值的程序 //贴花的vraibles和阵列 int数组[3][3],行,列,maxrows=3,maxcols=3; //使用for循环初始化数组并从用户输入值 对于(行=0;行,c++,arrays,dev-c++,C++,Arrays,Dev C++,是不明确的,因为在获取值后,右侧会递增,然后分配给左侧 foo.c:26: warning: operation on ‘rows’ may be undefined 实际上,它说一些编译器可能会给出不同的结果,例如使其与 rows = rows 如果这样做,它将导致一个无限循环。(顺便说一下,对于g++,值确实会增加)。您已经有了答案,但我想我应该指出变量声明的样式。即,int array[3][3],rows,cols,maxrows=3,maxcols=3; 这是不好的,因为它不是非常
foo.c:26: warning: operation on ‘rows’ may be undefined
实际上,它说一些编译器可能会给出不同的结果,例如使其与
rows = rows
如果这样做,它将导致一个无限循环。(顺便说一下,对于g++,值确实会增加)。您已经有了答案,但我想我应该指出变量声明的样式。即,
int array[3][3],rows,cols,maxrows=3,maxcols=3;
这是不好的,因为它不是非常可读。行和列变量的类型在严格意义上是不正确的,请参阅为什么将其用作数组索引时大小更好。此外,还可以说明为什么您应该支持增量前而不是增量后,除非您有充分的理由不使用(++i而不是i++),但在这种特殊情况下,这并不重要,因为运算符没有重载。此外,适当使用常量可以使代码更可读,并消除某些类型的错误。最后,尽快为变量提供一个合理的值,以消除未定义的行为
#include <iostream>
using namespace std;
int main ()
{
//program to print the input values in rows and columns of two dimensional array in revserse roder using loop
//decalre the vraibles and array
int array [3][3] = {};
const size_t maxrows = 3,
const size_t maxcols = 3;
//use for loop to intiiate the array and input values from users
for (size_t rows = 0; rows < maxrows; ++rows )
{
for (size_t cols = 0; cols < maxcols; ++cols )
{
cout << " Enter the value for array location : " << " [ " << rows << " , " << cols << " ] " ;
cin >> array [rows][cols] ;
}
}
//display the values entered in array starting with bottom row first and then the rest
for ( size_t rows = 0; rows < maxrows ; ++rows)
{
for ( size_t cols = 0; cols < maxcols ; ++cols)
{
cout << " The values that were entered in this array starting with bottom row are " << array [rows][cols] ;
}
}
}
#包括
使用名称空间std;
int main()
{
//使用循环打印revserse roder中二维数组行和列中的输入值的程序
//贴花的vraibles和阵列
int数组[3][3]={};
常量大小\u t最大行数=3,
const size\u t maxcols=3;
//使用for循环初始化数组并从用户输入值
对于(行大小=0;行数<最大行数;++行数)
{
用于(大小\u t cols=0;cols coutrows=rows++
是多余的,只需写++rows
。什么意思被卡住了?我怎么会错过这个。谢谢这里有一个关于循环和递增的指南-