Vector Function

This program isn't done, I know I'm just making a simple mistake. As you see in the void function from Lines 13-26, essentially I want the loop to be able print the values of my sector as (ex.) - (24,36,14), but when i run the program, it'll print (24,36, 321651327). Basically the last number will be what i presume to be the printed address. Is it perhaps because I'm not initializing a variable somewhere? I thought that by giving the value from my int(main), the function would know that the *n/*m pointer refers to what I inputted within the program, then print the nth value from the array.

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
50
51
52
53
54
55
56
57
58
59
60
  #include <iostream>

using namespace std;

bool vect(int n, int m)
{
    if(n==m)
        return 1;
    else
        return 0;
}

void printvect (int *vec1, int *vec2,int*n,int *m)
{

    for(int i=0;i<*n-1;i++)
    {
        cout<<vec1[i]<<" , ";
    }
    cout<<vec1[*n]<<endl;
    for(int i=0;i<*m-1;i++)
    {
        cout<<vec2[i]<<" , ";
    }
    cout<<vec2[*m]<<endl;
}

int main()
{
    int n=0;
    int m=0;
    cout<<"What are the dimensions for Vector 1?"<<endl;
    cin>>n;
    cout<<"What are the dimensions for Vector 2?"<<endl;
    cin>>m;
    if (vect(n,m))
    {
        int vec1[n];
        int vec2[m];

        cout<<"Enter the values for Vector 1."<<endl;
        for(int i=0;i<n;i++)
           {cin>>vec1[i];
           }
        cout<<"Enter the values for Vector 2."<<endl;
        for(int j=0;j<m;j++)
        {
            cin>>vec2[j];
        }
        printvect(vec1,vec2,&n,&m);


    }


    else if (!vect(n,m))
        cout<<"The dimensions of your vector do not match."<<endl;

        return 0;
}
Lines 20 & 25 should be
cout<<vec1[*n-1]<<endl;
and
cout<<vec2[*m-1]<<endl;
respectively.

BTW lines 38 & 39 are non-standard C++. Array sizes must be constant.
@dhayden

OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO, I just forgot, because essentially the 3 values that are representative of the array are vec1[0], vec1[1], vec1[2], it's only when you initialize is where you define the total values into the array, int vec1[3]. Then to print those values, it follows vec1[0], vec1[1], vec1[2]. Thank you Thank you hahah
Topic archived. No new replies allowed.