Problem while reading and printing an array 读取和打印数组时出现问题

huangapple go评论93阅读模式
英文:

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 &lt;iostream&gt;
using namespace std;

int main(){
	int number;
	cout&lt;&lt;&quot;Enter a array_size&quot;&lt;&lt;endl;
	cin&gt;&gt;number;

	//Reading and printing array
	int array[number];
	int i=1;
	while(i&lt;=number){
		cin&gt;&gt;array[i];
		i++;
	}
	//printing array
	int j=0;
	while(j&lt;=number){
		cout&lt;&lt;array[j]&lt;&lt;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 &lt;iostream&gt;
#include &lt;vector&gt;

int main()
{
    int number;
    std::cout &lt;&lt; &quot;Enter a array_size&quot; &lt;&lt; std::endl;
    std::cin &gt;&gt; number;

    // Read:
    std::vector&lt;int&gt; array(number);
    for (auto&amp; elem : array)
    {
        std::cin &gt;&gt; elem;
    }

    // Print:
    for (auto const &amp; elem : array)
    {
        std::cout &lt;&lt; elem &lt;&lt; std::endl;
    }
}

Note that auto elem : array (without &amp;) 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开始。


还有许多其他帖子描述了这些问题。

数组索引始终从0开始

为什么访问超出边界的数组不会报错

英文:

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.

Array index always start at 0

Why accessing out of bound array does not give any error

答案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&lt;number){  // should be less than only since you start with zero.
    cin&gt;&gt;array[i];
    
    i++;

}
//printing array
int j=0;
while(j&lt;number){   // should be less than only since you start with zero.
    cout&lt;&lt;array[j]&lt;&lt;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 &lt; number) {
    cin &gt;&gt; array[i];
    i++;
  }

  //printing array
  int j = 0;
  while (j &lt; number) {
    cout &lt;&lt; array[j] &lt;&lt; endl;
    j++;
  }

It's more idiomatic to use a for loop here:

  //Reading and printing array
  int array[number];

  for (int i = 0; i &lt; number; i++) {
    cin &gt;&gt; array[i];
  }

  //printing array
  for (int i = 0; i &lt; number; i++) {
    cout &lt;&lt; array[j] &lt;&lt; endl;
  }

huangapple
  • 本文由 发表于 2023年5月17日 15:44:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定