0

I need to complete the task, meeting the following requirements: Algorithms for input and output of array elements, as well as for performing a given operation on an array, should be formatted as functions. When accessing array elements, use a pointer.

In principle, my program should work, but it works only for 1 number, for 2 it already shows some kind of weird, and for 3 or more - nothing at all.

#include <iostream>
#include <stdlib.h>

using namespace std;

void input(int x[], int n);
int spisok(int x[], int n);

int main() {
    int n, x[n];
    cout << "Vvedite kolichestvo chisel v massive" << endl;
    cin >> n;
    input(x, n);
    spisok(x, n);
    for (int i = 0; i < n; i++) {
        if (x[i] != 0) {
            cout << "Prostiye chisla " << x[i] << ' ';
        }
    }
    return 0;
}
void input(int x[], int n) {
    int * p = x;
    for (int i = 1; i <= n; i++) {
        cout << "Vvedite " << i << "-oe chislo" << endl;
        cin >> * p;
        p++;
    }
}
int spisok(int x[], int n) {
    int i = 0;
    for (int j = 2; j <= x[i]; j++) {
        if (i > n) {
            break;
        }
        if (x[i] % j == 0 && x[i] != j) {
            x[i] = 0;
            break;
        }
        i++;
    }
    return *x;
}
Parham
  • 3,157
  • 4
  • 31
  • 44
  • 1
    `int n,x[n];` is undefined behavior. Not to mention, that VLAs are non-standard, but allowed by some compilers as an extension. You are, here, creating a VLA of undetermined size. Ask yourself: what is the value of `n`, at the point of this declaration? – Algirdas Preidžius Nov 16 '20 at 20:46
  • Vvedite kolichestvo chisel v massive - insert the number of numeras Vvedite "< – Андрей Кострюков Nov 16 '20 at 20:48
  • @АндрейКострюков I, am sorry, but what does that have to do with my comment? The fact that you read `n` at some later point in time is irrelevant. The variables are created at the point of declaration. The array size doesn't change after the fact, once the variable, that was used to create it, changes its value. C++ doesn't work that way. If you are learning C++, consider learning from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Nov 16 '20 at 20:54

1 Answers1

0

As you actually did not ask a question I take the liberty to interpret it as: How to find the mistake?

Further I am assuming that you use gcc. The answer for different compilers is similar (check their manual).

It might be a bit of a surprise, but as a matter of fact, gcc does not compile standard C++ with its default settings.

Unless you know what you are doing you should at least use the flags:

  • -pedantic to make it compile the code as standard C++
  • -Wall to make it report most common warnings
  • -Werror to report warnings as errors

With that flags your code produces following error message:

<source>: In function 'int main()':
<source>:10:12: error: ISO C++ forbids variable length array 'x' [-Werror=vla]
   10 |     int n, x[n];
      |            ^
<source>:10:15: error: 'n' is used uninitialized in this function [-Werror=uninitialized]
   10 |     int n, x[n];
      |               ^
cc1plus: all warnings being treated as errors

The first is because array sizes must be compile time constants. See Why aren't variable-length arrays part of the C++ standard? for details and use a std::vector for dynamically sized arrays instead.

The second is because you use n before it is initialized. You read n from user input, but that is after you declare the array in this line int n, x[n];. Code is executed from top to bottom. The value of n doesn't magically travel back in time after read from user input to use the right size for the array.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185