0% found this document useful (0 votes)
2 views2 pages

array

The document contains three C programming examples demonstrating how to pass individual and multi-dimensional array elements to functions. The first example shows passing specific elements of a one-dimensional array, the second calculates the sum of a one-dimensional array, and the third displays elements of a two-dimensional array. Each example includes the necessary code and function definitions.

Uploaded by

Shyam Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

array

The document contains three C programming examples demonstrating how to pass individual and multi-dimensional array elements to functions. The first example shows passing specific elements of a one-dimensional array, the second calculates the sum of a one-dimensional array, and the third displays elements of a two-dimensional array. Each example includes the necessary code and function definitions.

Uploaded by

Shyam Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Q1.

Passing Individual Array Elements

#include <stdio.h>
#include<conio.h>
void display(int age1, int age2) {
printf("%d\n", age1);
printf("%d\n", age2);
}
void main()
{
int ageArray[] = {2, 8, 4, 12};
// pass second and third elements to display()
display(ageArray[1], ageArray[2]);
getch();
}

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

#include <stdio.h>
#include<conio.h>
float calculateSum(float num[]);
void main() {
float result, num[] = {23.4, 55, 22.6, 3, 40.5, 18};
// num array is passed to calculateSum()
result = calculateSum(num);
printf("Result = %.2f", result);
getch();
}
float calculateSum(float num[]) {
float sum = 0.0;
for (int i = 0; i < 6; ++i) {
sum += num[i];
}
return sum;
}
Q3. Passing two-dimensional array
#include <stdio.h>
void displayNumbers(int num[2][2]);
void 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]);
}
}
// pass multi-dimensional array to a function
displayNumbers(num);
getch();
}
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]);
}
}
}

You might also like