Answers for "pass array to function c++"

C++
3

pass array to function c++

#include <iostream>
using namespace std;
 
// function declaration:
double getAverage(int arr[], int size);

int main () {
   // an int array with 5 elements.
   int balance[5] = {1000, 2, 3, 17, 50};
   double avg;

   // pass pointer to the array as an argument.
   avg = getAverage( balance, 5 ) ;
 
   // output the returned value 
   cout << "Average value is: " << avg << endl; 
    
   return 0;
}
Posted by: Guest on July-16-2021
12

array as parameter c++

void myFunction(int param[]) {
   .
   .
   .
}
Posted by: Guest on May-08-2020
5

how to pass an array to a function

// Program to calculate the sum of array elements by passing to a function 

#include <stdio.h>
float calculateSum(float age[]);

int main() {
    float result, age[] = {23.4, 55, 22.6, 3, 40.5, 18};

    // age array is passed to calculateSum()
    result = calculateSum(age); 
    printf("Result = %.2f", result);
    return 0;
}

float calculateSum(float age[]) {

  float sum = 0.0;

  for (int i = 0; i < 6; ++i) {
		sum += age[i];
  }

  return sum;
}
Posted by: Guest on December-22-2020

Code answers related to "pass array to function c++"

Browse Popular Code Answers by Language