0% found this document useful (0 votes)
10 views5 pages

Assignment-5 Int Array Functions

The document outlines an assignment involving four functions for manipulating integer arrays in C. These functions include calculating the sum of elements, summing only even numbers, counting occurrences of a specific element, and reversing an array. Each function is accompanied by sample code demonstrating its implementation and usage within a main function.

Uploaded by

Aketi Tanuja
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)
10 views5 pages

Assignment-5 Int Array Functions

The document outlines an assignment involving four functions for manipulating integer arrays in C. These functions include calculating the sum of elements, summing only even numbers, counting occurrences of a specific element, and reversing an array. Each function is accompanied by sample code demonstrating its implementation and usage within a main function.

Uploaded by

Aketi Tanuja
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/ 5

ASSIGNMENT – 5 Int Array Functions

Name: Aketi Thanuja Batch: Sept 8


Have an array of integers.
Write functions
1) calc_sum : input args are "array" and "num". Return is sum
#include <stdio.h>
int cal_sum(int a[],int n)
{
int sum=0;
for(int i = 0; i < n; i++)
sum = sum + a[i];
return sum;
}

int main()
{

int n,sum = 0;
printf("Enter array size\n");
scanf("%d",&n);
int a[n];
printf("Enter array elements\n");
for(int i = 0; i < n; i++)
scanf("%d",&a[i]);
sum=cal_sum(a,n);
printf("Sum of the array = %d\n",sum);
return 0;
}
2) calc_sum_even : sum of even numbers (discard odd) [guideline : you must
"continue"]
#include <stdio.h>
int calc_sum_even(int a[],int n)
{
int sum=0;
for(int i = 0; i < n; i++)
{
if(a[i]%2!=0)
{
continue;
}
else
{
sum = sum + a[i];
}
}
return sum;
}
int main()
{
int n,evensum = 0;
printf("Enter array size\n");
scanf("%d",&n);
int a[n];
printf("Enter array elements\n");
for(int i = 0; i < n; i++)
scanf("%d",&a[i]);
evensum=calc_sum_even(a,n);
printf("Sum of even numbers = %d\n",evensum);
return 0;
}
3) count_elem : args are array/num/elem : number of times elem occurs in
the array
#include <stdio.h>
int count_elem(int a[],int n, int e)
{
int count=0;
for(int i = 0; i < n; i++)
{
if(a[i]==e)
{
count++;
}
}
return count;
}
int main()
{
int n,countelement = 0,e;
printf("Enter array size\n");
scanf("%d",&n);
int a[n];
printf("Enter array elements\n");
for(int i = 0; i < n; i++)
scanf("%d",&a[i]);
printf("Enter element to be counted\n");
scanf("%d",&e);
countelement=count_elem(a,n,e);
printf("Count of the element = %d\n",countelement);
return 0;
}
4) reverse_array(array1, num1, array2) : input is array1, num1,
reversed sequence is stored in array2.
#include <stdio.h>
void reverse_array(int a1[],int n, int a2[])
{
int j = 0;
for(int i = n- 1; i >= 0; i--)
{
a2[j] = a1[i];
j++;
}
for(int i = 0; i < n; i++)
printf("%d\n",a2[i]);
}
int main()
{

int n,countelement = 0;
int*res;
int a2[n];
printf("Enter array size\n");
scanf("%d",&n);
int a1[n];
printf("Enter array elements\n");
for(int i = 0; i < n; i++)
scanf("%d",&a1[i]);
reverse_array(a1,n,a2);
return 0;
}

You might also like