- 1
Function Output C++
void calcsum(int arr[], int size){ int sum = 0; for (int x = 0; x < size; x++){ sum += arr[x];} std::cout << sum;} what should i code to the main() if i want to output this function?
10 Respuestas
+ 5
DarkEye here's a sample code for you:
#include <iostream>
void calcsum(int arr[], int size)
{
int sum = 0;
for (int x = 0; x < size; x++)
sum += arr[x];
std::cout << sum;
}
int main()
{
// Here's an array with 5 elements
int arr[] {10,20,30,40,50};
// And this is how we pass the array
// note that we pass the array size (5)
// as the 2nd argument in calling calcsum
calcsum(arr, 5);
}
Hth, cmiiw
+ 4
Call the function, passing it an array of int, and the array size for first and second argument, respectively. Needless to say the array is expected to have values : )
+ 3
Well, of course it doesn't work, you passed an int value as the 1st argument, while the function expected an array of int, you need to pass an array as 1st argument. Do you have an array already or not?
+ 1
thanks that is what i looked for. have a nice day!
+ 1
answer is for and x. God Jesus Bless.
0
i know but how do i output this?
for example: calcsum(3, 5);
this doesn't work. i want to know what to write it correctly to output my function
0
I'll ask that way, if I have an arr[3], what should I write in calcsum
-> calcsum(? , 5);
0
DarkEye please open my code again and let me know if your problem is solved.https://code.sololearn.com/cCXiKO2MmfI4/?ref=app
0
my problem is solved prabhjot
- 3
DarkEye Hope this code will help. If have any suggestions please tell me. I thankful for that
https://code.sololearn.com/cCXiKO2MmfI4/?ref=app