C language dynamic array use implementation code

  • 2020-05-10 18:35:50
  • OfStack

C language dynamic array

Read in n integers from the keyboard, use a dynamic array to store the integers read in, and calculate their sum and average output respectively. It is required to implement program code using functions whenever possible. If the average is a decimal, only the integer part is retained.

Sample input:

5

3 4 0 0 2

Sample output:

9   1

Sample input:

7

3 2 7 5 2 9 1

Sample output:

29   4

The code is as follows:  


#include<stdio.h>
int addAll(int a[],int N);
int aveFun(int sum,int n);
int main(){
int N;
int sum=0,ave=0;
scanf("%d",&N);
int a[N];
for(int i=0;i<N;i++){
scanf("%d",&a[i]);
}
sum = addAll(a,N);
ave = aveFun(sum,N);
printf("%d %d",sum,ave);
}
int addAll(int a[],int N){
int sum=0;
for(int i=0;i<N;i++){
sum += a[i];
}
return sum;
}
int aveFun(int sum,int N){
return sum/N;
}

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: