Answers for "how to pass an array to a function"

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
6

how to accept an array as function parameter in c

#include <stdio.h>
void displayNumbers(int num[2][2]);
int main()
{
    int num[2][2];
    printf("Enter 4 numbers:\n");
    for (int i = 0; i < 2; ++i)
        for (int j = 0; j < 2; ++j)
            scanf("%d", &num[i][j]);

    // passing multi-dimensional array to a function
    displayNumbers(num);
    return 0;
}

void displayNumbers(int num[2][2])
{
    printf("Displaying:\n");
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 2; ++j) {
           printf("%d\n", num[i][j]);
        }
    }
}
Posted by: Guest on September-22-2020
0

passing an array to a function

/**
* myFunction - receives an array param of 10 int variables
*/

void myFunction(int param[10]) {
   .
   .
   .
}
Posted by: Guest on August-04-2021

Code answers related to "how to pass an array to a function"

Code answers related to "Javascript"

Browse Popular Code Answers by Language