[英]Incorrect Array usage in C++
So I am supposed to make a function that takes an array and "Deletes" the even number and only spits out the odd numbers. 所以我应该做一个函数,它接受一个数组并“删除”偶数,只吐出奇数。 We have been given part of the code, which is the following:
我们已经获得了部分代码,如下所示:
int arr1[] = {1, 4, 7, 1, 2, 10, 1};
int size = sizeof(arr1) / sizeof(int);
keepOdd(arr1, size);
for (int i = 0; i < size; i++)
cout << arr1[i] << " ";
cout << endl << "Totally " << size << " values left.";
I took that and expanded on it and I have come up with: 我接受了它并对其进行了扩展,然后我想到了:
void keepOdd(int[], int);
void keepOdd(int& arr, int& size) {
int counter, newarr[size];
counter = 0;
for (int i = 0; i < size; i++) {
if (arr[i] % 2 == 1) {
counter++;
newarr[i]= arr[i];
}
}
size = counter;
for (int i = 0; i < size; i++)
arr[i]=newarr[i];
}
int main () {
int arr1[] = {1, 4, 7, 1, 2, 10, 1};
int size = sizeof(arr1) / sizeof(int);
keepOdd(arr1, size);
for (int i = 0; i < size; i++)
std::cout << arr1[i] << " ";
std::cout << endl << "Totally " << size << " values left.";
}
But my code doesn't run at all. 但是我的代码根本无法运行。
Here is the error message: 这是错误消息:
extra credit.cpp: In function 'void keepOdd(int&, int&)':
extra credit.cpp:15:13: error: invalid types 'int[int]' for array subscript
if (arr[i] % 2 == 1) {
^
extra credit.cpp:17:23: error: invalid types 'int[int]' for array subscript
newarr[i]= arr[i];
^
extra credit.cpp:22:9: error: invalid types 'int[int]' for array subscript
arr[i]=newarr[i];
^
extra credit.cpp: In function 'int main()':
extra credit.cpp:36:14: error: 'endl' was not declared in this scope
std::cout << endl << "Totally " << size << " values left.";
^~~~
extra credit.cpp:36:14: note: suggested alternative:
iostream:39:0,
from extra credit.cpp:1:
ostream:590:5: note: 'std::endl'
endl(basic_ostream<_CharT, _Traits>& __os)
^~~~
I have to use arrays but I don't see how I am supposed to properly use one. 我必须使用数组,但看不到如何正确使用数组。
The problem is here: 问题在这里:
void keepOdd(int& arr, int& size) {
^^^^^^^^
The type of the first argument of your function with the name arr
is "reference to an integer". 函数名称为
arr
的第一个参数的类型是“对整数的引用”。
On this line within the function: 在函数内的这一行:
newarr[i]= arr[i];
^^^^^^
You apply the subscript operator on the referenced integer. 您对引用的整数应用下标运算符。 There is no such operator where both the left hand side argument (
arr
) and right hand side argument ( i
) are integers. 没有这样的运算符,左侧参数(
arr
)和右侧参数( i
)都是整数。 As such, the program is ill-formed. 这样,程序格式不正确。 Hence the compiler error:
因此,编译器错误:
error: invalid types 'int[int]' for array subscript newarr[i]= arr[i];
The same problem with the argument is cause for the other type-mismatch errors as well. 参数的相同问题也是其他类型不匹配错误的原因。
I suspect that you may have intended to use a pointer argument instead. 我怀疑您可能打算改用指针参数。
error: 'endl' was not declared in this scope
You have used the indentifier endl
in your program. 您已在程序中使用了标识符
endl
。 There is no declaration for endl
in your program. 您的程序中没有
endl
声明。 That is what the compiler is telling you. 那就是编译器告诉您的。
The compiler also has found a potential solution for you: 编译器还为您找到了潜在的解决方案:
note: suggested alternative: iostream:39:0 note: 'std::endl'
What the compiler is telling you, is that there is std::endl
declared in the header <iostream>
. 编译器告诉您的是,在头文件
<iostream>
声明了std::endl
。 <iostream>
is a standard library header. <iostream>
是标准库头。 If you indeed intended to use std::endl
(and I suspect that you did), then you can use it by typing std::endl
instead of endl
. 如果您确实打算使用
std::endl
(我怀疑您这样做过),则可以通过键入std::endl
而不是endl
来使用它。
PS you probably don't need to use std::endl
. PS,您可能不需要使用
std::endl
。 '\\n'
is probably sufficient for you and won't unnecessarily slow the program down by flushing the output stream. '\\n'
对您来说可能就足够了,不会因为刷新输出流而不必要地降低程序速度。
int counter, newarr[size];
size
is not compile time constant. size
不是编译时间常数。 The length of an automatic array must be compile time constant. 自动数组的长度必须是编译时间常数。 Hence, the program is ill-formed.
因此,该程序格式错误。
If you need an array whose length is determined at runtime, you can allocate the array dynamically. 如果需要长度在运行时确定的数组,则可以动态分配该数组。 The simplest way to allocate an array dynamically is to use
std::vector
. 动态分配数组的最简单方法是使用
std::vector
。
There are some changes needed to make your code work: 为了使代码正常工作,需要进行一些更改:
First, the function header for passing an array and size is something like this: 首先,用于传递数组和大小的函数标头如下所示:
int keepOdd(int* arr, int size);
note that we will return an int: this is the new size of the array. 请注意,我们将返回一个int:这是数组的新大小。
The logic of keepOdd can, then, be greatly simplified: 然后,可以大大简化keepOdd的逻辑:
int keepOdd(int* arr, int size) {
int counter;
counter = 0;
for (int i = 0; i < size; i++) {
if (arr[i] % 2 == 1) {
arr[counter++]= arr[i];
}
}
return counter;
}
Here you don't need to create a new array: you are rewriting even elements with the next odd element and returning the new array size. 在这里,您无需创建新的数组:您将使用下一个奇数元素重写偶数元素,并返回新的数组大小。
Then, just use the new size to print the elements: 然后,只需使用新的大小来打印元素:
int main () {
int arr1[] = {1, 4, 7, 1, 2, 10, 1};
int size = sizeof(arr1) / sizeof(arr1[0]);
std::cout << "size is " << size << "\n";
int newSize = keepOdd(arr1, size);
for (int i = 0; i < newSize; i++)
std::cout << arr1[i] << " ";
std::cout << std::endl << "Totally " << size << " values left.";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.