-4
#include <iostream>
using namespace std;

class vector{
    private:
        int *arr;
        int n;

    public:
        vector(int a, int n){
            for(int i = 1; i <= n; i ++)
                arr[i] = a;
        }

        vector(){
            for(int i = 1; i <= n; i ++)
                arr[i] = 0;
        }

        void reactualizare_vector(int t, int m){
            n = t;
            for(int i = 1; i <= n; i ++)
                arr[i] = m;
        }

        void afis_vector(){
            for(int i = 1; i <= n; i ++)
                cout << arr[i] << " ";
        }

        void citire_vector(int n){
            for(int i = 1; i <= n; i ++)
                cin >> arr[i];
        }
};

int a, n, t, m;

int main()
{
    cin >> a >> n;

    vector p(a, n);
    p.citire_vector(n);
    p.afis_vector();

    return 0;
}

Input: 2 4 2 1 4 3

Output: 2 1 4 3 0 0 0 0 1952810132 2543616 1952810096 -594764666 6422492 2002010568 2543616 -1294838125 0 0 2543616 0 0 0 0 -1294838125 6422432 0 6422500 2002070528 -974236977 0 6422508 2002010520 -1 2002121555 0 0 4199136 2543616 0

How can I size the vector to not show what is after 2 1 4 3 ?

Mat
  • 202,337
  • 40
  • 393
  • 406
Sasuke1312
  • 23
  • 4
  • 2
    You have a pointer `arr`, but *where does it point?* You never make it point anywhere, which means `arr` will be uninitialized and have an indeterminate value. When you dereference it you will have *undefined behavior*. Perhaps it's time to invest in [a couple of good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), or go through your tutorials or class notes about *dynamic allocation*. – Some programmer dude Mar 01 '20 at 06:40
  • 1
    Read a good book on [C++ programming](http://stroustrup.com/Programming/). Read some [C++ tutorial](http://www.cplusplus.com/doc/tutorial/). See some [C++ reference site](https://en.cppreference.com/w/cpp). Use [standard containers](https://en.cppreference.com/w/cpp/container) – Basile Starynkevitch Mar 01 '20 at 06:40
  • 2
    And the member `n` is never initialized. – 273K Mar 01 '20 at 06:42
  • If using [GCC](https://gcc.gnu.org/) compile with all warnings and debug info, so with `g++ -Wall -Wextra -g` – Basile Starynkevitch Mar 01 '20 at 06:42
  • A few considerations : do not use `using namespace std;` and learn to name your variables/methods/functions in English : e.g. `show_vector` is way better than `afis_vector` , and so on. – schaiba Mar 01 '20 at 06:44

1 Answers1

0

Coding style (Variable naming, etc) is very important. I strongly recommend to learn these (mentioned in above comments), but I think other people know better on this issue. I myself don't think I have a good coding style to give someone useful advice of... I will just jump to the stuff that may make your code work at least.

1) n in the class vector is not assigned properly. You took n input as a global variable, but it isn't actually getting in the class. You need to assign value like p.n = n; but since n in the vector class is private, you may either try writing a function to do this. One way might be modifying the initializer to something like

vector(int a, int sz){
    n = sz;
    for(int i = 1; i <= n; i ++)
        arr[i] = a;
}

However, I think writing a resize function is generally a lot better idea.

In some compilers and settings, applying this can make your code work.

2) As above comment already mentioned, you should actually allocate the pointer arr. I have no idea why you are ignoring index 0 in arr, but if you want to, you may allocate (n+1) * sizeof(int) bytes of memory for arr in the initializing step. Something like this might work.

vector(int a, int sz){
    n = sz;
    arr = (int *)(malloc((n+1)*sizeof(int)));
    for(int i = 1; i <= n; i ++)
        arr[i] = a;
}

3) If you have to write a vector class, this is irrelevant. But if you are not specifically asked to write this class, and you have more important part of task to do, use std::vector. Even after applying my fixes to your code, std::vector offers a lot better performance, a whole bunch of useful functions, and you will be very likely to make errors and spend hours of debugging if you have to write the whole vector class. Avoid reinventing the wheel.


Apparently everything about memory allocation written here seems to be wrong now. I had no idea about modern C++ memory allocation. Please ignore these, and since I do not know how to erase accepted answer, delete this if someone can. Sorry for outdated information.

Gratus
  • 29
  • 4
  • 4
    `malloc`? Are you kidding? – 273K Mar 01 '20 at 07:23
  • 1
    Please consider to delete this answer. In 2020 we do not use raw pointers for owned memory in C++. And also **never** ````malloc````, and even not ````new````. The missing destructor, resulting in a memory leak, is typical. So, don't use such language constructs in C++. – A M Mar 01 '20 at 09:12