Need Help with Array+Subroutines - Simple Stuff

Hello. I need help with something. I need to create a program that reads some numbers, and calculate them on a separated subroutine, and the return of this subroutine must be the sum of all the numbers.
I'm getting an error but I can't figure out why =/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int calc(int val, int qtd){
    int sum=0;
    for (int cont=0; cont<qtd; cont++)
        sum=sum+val;
    return sum;
}

int main()
{
    cout << "Inform how many numbers will be used: ";
    int qtd;
    cin >> qtd;
    int val[qtd];
    for (int cont=0; cont<qtd; cont++){
        cout << "Inform the " << cont+1 << " number: ";
        cin >> val[cont];
    }
    cout << "The sum of all numbers is :" << calc(val, qtd);
    return 0;
}


The error that I'm getting is on the line 22.
The error message: "invalid conversion from 'int*' to 'int' [fpermissive]

Can someone help me out to fix this? Thanks.
Last edited on
Look at the definition of calc() on line 5. What type have you defined the first parameter to be?

Look at the definition of the local variable val on line 17. What type have you declared that to be?

Can you see the difference?

// line 5
int calc(int val, int qtd){


This does not do what you want.

val is an int. That means it is a single integer. An array has multiple integers.

You cannot pass an array to a function in C++ ... at least not in the traditional sense. Instead, you pass a pointer to the first element of the array:

 
int calc(int* vals, int qtd)


(Note the added * here... that makes it a pointer)


One you make that change you'll have another problem here:

1
2
// line 8
        sum=sum+val;


This keeps adding a single value to the sum over and over... rather than adding each element in the array once. So basically this is a goofy way to do multiplication (sum will be set to val*qtd because you're just adding val qtd times).

Since we've changed val to vals and made it a pointer instead of a single array... we can now access elements through that pointer via the usual [bracket] operator:

 
        sum += vals[cont];



After that the program should work.
The error you're seeing is on this line:

 
cout << "The sum of all numbers is :" << calc(val, qtd);


The problem with this is that 'val' was an array... and you were passing it to a function which accepted a single integer. Now that we've changed calc so that it accepts a pointer instead of a single integer, this will have fixed itself (array names can be automatically cast to pointers to their first element).




=========================
On a side note.... this:
1
2
// line 17
    int val[qtd];

is nonstandard and may not work depending on whether or not your compiler supports it. You can't use non-const variables as array sizes in C++. Though many compilers support it as an extension because it's allowed in C.

But whatever
=========================
@Mikeboy
I did saw the difference, but I can't use int calc (int val[qtd], int qtd) on line 5.

@Disch
This is my problem, I don't know yet how to properly pass an array to a function, I tried to put the val[qtd] as a parameter, but it didn't worked.

I understood what you did, but I did some changes, here's the result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int calc(int val[], int qtd){
    int sum=0;
    for (int cont=0; cont<qtd; cont++)
        sum=sum+val[cont];
    return sum;
}

int main()
{
    cout << "Inform how many numbers will be used: ";
    int qtd;
    cin >> qtd;
    int val[qtd];
    for (int cont=0; cont<qtd; cont++){
        cout << "Inform the " << cont+1 << " number: ";
        cin >> val[cont];
    }
    cout << "The sum of all numbers is :" << calc(val, qtd);
    return 0;
}


On line 5, instead of adding that int* (I dont know what it means, we didn't learned this kind of stuff yet) I used the [], I guess it works the same way?

And on line 8 I could now change the sum to get all the right values.
Thanks a lot for your help :D
I used the [], I guess it works the same way?


In this case... yes it is identical.

I just personally hate that syntax because it is extremely misleading (especially for beginners), and is specific to this particular context (pretty much anywhere else, empty brackets are either illegal or mean something else entirely).

But if it works for you then it works. =) Go for it.
Last edited on
Topic archived. No new replies allowed.