英文:
Problem while reading and printing an array
问题
I'm a newbie and started to code a couple of days ago. So today I started with arrays and have some doubts. I'm reading an array by taking input from the user and displaying it. Everything is running fine, but on the output screen, I'm having a garbage value, and I don't know why. Please help me.
#include <iostream>
using namespace std;
int main(){
int number;
cout << "Enter an array_size" << endl;
cin >> number;
// Reading and printing array
int array[number];
int i = 0; // Changed from 1 to 0
while(i < number){ // Changed from <= to <
cin >> array[i];
i++;
}
// Printing array
int j = 0;
while(j < number){ // Changed from <= to <
cout << array[j] << endl;
j++;
}
}
Expected output: Reading from the user: 3
1
2
3
// Displaying
1
2
3
Actual result: Reading from the user: 3
1
2
3
The issue with "16" showing in the output is because of incorrect array indices and loops. I've made the necessary changes in the code above to fix the issue.
英文:
I'm a newbie and started to code couple of days ago. So today started with array and have some doubts. I'm reading a array by taking input from user and displaying it everything s running fine but on ouput screen I'm having a garbage value and dont know why. Please help me.
#include <iostream>
using namespace std;
int main(){
int number;
cout<<"Enter a array_size"<<endl;
cin>>number;
//Reading and printing array
int array[number];
int i=1;
while(i<=number){
cin>>array[i];
i++;
}
//printing array
int j=0;
while(j<=number){
cout<<array[j]<<endl;
j++;
}
}
Expected output: Reading from user: 3
1
2
3
//displaying
1
2
3
Actual result: Reading from user: 3
1
2
3
16
1
2
3
Why 16 is showing?
答案1
得分: 2
Here's the translated content:
正如评论中提到的,C++ 不支持可变长度数组(VLA) - 请参阅这里:https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard。
还提到过数组索引从0开始,而不是1(https://stackoverflow.com/questions/2289548/array-indexing-starting-at-a-number-not-0)。
您可以使用 std::vector
和 范围-based 循环 来避免这些问题(后者自C++11以来可用)。
您的案例的示例代码:
#include <iostream>
#include <vector>
int main()
{
int number;
std::cout << "输入数组大小:" << std::endl;
std::cin >> number;
// 读取:
std::vector<int> array(number);
for (auto& elem : array)
{
std::cin >> elem;
}
// 打印:
for (const auto& elem : array)
{
std::cout << elem << std::endl;
}
}
请注意,auto elem : array
(没有 &
)不会起作用,因为 auto
单独不会推断引用,而会复制元素。
一个小提示: 最好避免使用 using namespace std
- 请参阅:https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice。
英文:
As mentioned in the comments, c++ does not support VLA (variable length arrays) - see here: https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard.
It was also already mentioned that array indices start at 0, not 1 (https://stackoverflow.com/questions/2289548/array-indexing-starting-at-a-number-not-0).
You can avoid these issues using std::vector
and ranged-based loops (the latter is available since c++11).
Code example for you case:
#include <iostream>
#include <vector>
int main()
{
int number;
std::cout << "Enter a array_size" << std::endl;
std::cin >> number;
// Read:
std::vector<int> array(number);
for (auto& elem : array)
{
std::cin >> elem;
}
// Print:
for (auto const & elem : array)
{
std::cout << elem << std::endl;
}
}
Note that auto elem : array
(without &
) would not have worked because auto
by itself would not infer a reference and instead copy the element.
A side note: better to avoid using namespace std
- see: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice.
答案2
得分: 0
以下是您要翻译的内容:
有两个问题与您的代码相关。
首先,int array[number];
不符合标准的C++,因为number
不是一个常量表达式。
其次,在C++中,数组索引从0
开始,而不是从1
开始。
还有许多其他帖子描述了这些问题。
英文:
There are 2 problems with your code.
First int array[number];
is not standard c++ as number
is not a constant expression.
Second, array indices start as 0
instead of at 1
in c++.
There are also many other posts describing these problems.
答案3
得分: -1
The array should always start at index 0. Update your starting index and the conditions.
// 读取并打印数组
int array[number];
int i=0; // 应该从零开始。
while(i<number){ // 应该仅小于,因为从零开始。
cin>>array[i];
i++;
}
// 打印数组
int j=0;
while(j<number){ // 应该仅小于,因为从零开始。
cout<<array[j]<<endl;
j++;
}
英文:
The array should always start at index 0. Update your starting index and the conditions.
//Reading and printing array
int array[number];
int i=0; // should start with zero.
while(i<number){ // should be less than only since you start with zero.
cin>>array[i];
i++;
}
//printing array
int j=0;
while(j<number){ // should be less than only since you start with zero.
cout<<array[j]<<endl;
j++;
}
答案4
得分: -2
数组索引从0开始,而不是从1开始。
纠正后的代码:
// 读取和打印数组
int array[number];
int i = 0;
while (i < number) {
cin >> array[i];
i++;
}
// 打印数组
int j = 0;
while (j < number) {
cout << array[j] << endl;
j++;
}
在这里更习惯使用for循环:
// 读取和打印数组
int array[number];
for (int i = 0; i < number; i++) {
cin >> array[i];
}
// 打印数组
for (int i = 0; i < number; i++) {
cout << array[i] << endl;
}
英文:
Array indexes start at 0, not at 1.
Corrected code:
//Reading and printing array
int array[number];
int i = 0;
while (i < number) {
cin >> array[i];
i++;
}
//printing array
int j = 0;
while (j < number) {
cout << array[j] << endl;
j++;
}
It's more idiomatic to use a for loop here:
//Reading and printing array
int array[number];
for (int i = 0; i < number; i++) {
cin >> array[i];
}
//printing array
for (int i = 0; i < number; i++) {
cout << array[j] << endl;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论