简体   繁体   中英

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".

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. 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. There is no declaration for endl in your program. 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> is a standard library header. 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 .

PS you probably don't need to use std::endl . '\\n' is probably sufficient for you and won't unnecessarily slow the program down by flushing the output stream.


 int counter, newarr[size]; 

size is not compile time constant. 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 .

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.

The logic of keepOdd can, then, be greatly simplified:

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.";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM