NEW VECTOR. HELP

Who can help me? I shoud create a new vector from the first one: the new vector has to contain all the values between -15 and 45. Shouldn't be any empty cells.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>
#include<stdlib.h>
#define l_max 6
void carico (float[]);
void new_vector (float[]);
using namespace std;
int main()
{
    float vett[l_max];
    carico(vett);
    new_vector(vett);
    system ("PAUSE");
    return 0;
}

void carico(float vett[])
{
    cout<<"Insert value.\n";
    for(int i=0; i<l_max; i++)
    {
        cin>>vett[i];
    }
}

void new_vector(float vett[])
{
    int vett_new[l_max], j=0;
    for(int i=0; i<l_max; i++)
    {
        if(vett[i]>-15 && vett[i]<45)
        {
            vett_new[j]=vett[i];
        }
    }
    cout<<"The vector with values between -15 and 45 is: \n";
    for(int i=0; i<l_max; i++)
    {
        cout<<vett_new[i]<<endl;
    }
}
There are no vectors anywhere in the code you've posted.

Just to be clear - do you really mean "vector"?

Or are you supposed to be doing this using C arrays?
I’m sorry... I meant array. Can you help me please?
you have 2 choices with C arrays.
1) make the array bigger than it needs to be.
2) use a pointer.

since you specify no empty cells, that means 2.
and, unfortunately, you need to know the size.
so you get the happy task of iterating the whole array just to count how many you have, then allocating your pointer to that size, then iterating it again to move the values into the new array.

This is a perfect example of why c++ vector is better than arrays for most tasks.

some compilers support nonstandard variable length arrays, you can do that instead of the pointer, but you still have to count them first to allocate the array.
Thank you very much, you are very clear. I tried to count the numbers but it still doesn't work. Can you tell me why please?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

#include<iostream>
#include<stdlib.h>
#define l_max 6
void carico (float[]);
void new_vector (float[]);
using namespace std;
int main()
{
    float vett[l_max];
    carico(vett);
    new_vector (vett);
    system ("PAUSE");
    return 0;
}

void carico(float vett[])
{
    cout<<"Insert value.\n";
    for(int i=0; i<l_max; i++)
    {
        cin>>vett[i];
    }
}

void new_vector(float vett[])
{
    int cont_p=0;
    for(int i=0; i<l_max; i++)
    {
        if(vett[i]>-15 && vett[i]<45)
        {
            cont_p+=1;
        }
    }
    int vett_new[cont_p], j=0;
    for(int i=0; i<l_max; i++)
    {
        if(vett[i]>-15 && vett[i]<45)
        {
            vett_new[j]=vett[i];
        }
    }
    cout<<"The vector with values between -15 and 45 is: \n";
    for(int i=0; i<cont_p; i++)
    {
        cout<<vett_new[i]<<endl;
    }
}
> vett_new[j]=vett[i];
j is your output index, which you need to increment each time you find a value to add to your array.

https://en.wikipedia.org/wiki/Debugger
For example, you would perhaps start with a breakpoint at line 36.
You run the code, type in what you want in your carico function.

When the program gets to the instruction corresponding to line 36, the debugger will take over (that's what breakpoints do).
The first thing you look at is the cont_p variable to see if it's a reasonable value.

Having decided it is, you could then single-step through the code to detect when what happens differs from what you expect.


> I tried to count the numbers but it still doesn't work.
But did you notice anything odd in what was wrong?
On visual inspection, it would seem that the first number printed at line 47 would be the last valid number in your array. This would be a big clue.
Topic archived. No new replies allowed.