Write a small program that fills an array of doubles with user input, prints the
ID: 3758439 • Letter: W
Question
Write a small program that fills an array of doubles with user input, prints the doubles on the screen in a column, and adds up all the doubles in the array and prints the sum onto the screen. You must use at least 3 functions
- Declare an array of doubles of size 20.
- Ask the user how many doubles to enter <= 20.
- Use a loop to read the doubles into the array from the user.
- Use a loop to print the array onto the screen in a column.
- Use a loop to add up all the items in the array and store the sum
- print the sum onto the screen
Explanation / Answer
#include <stdio.h>
void read(double [],int);
double sum(double [],int);
void print(double [],int);
int main()
{
double a[20];
int n;
printf(" How many double to enter <=20.");
scanf("%d",&n);
if(n>20)
printf(" Enter <=20 only");
else {
read(a,n);
print(a,n);
printf(" Sum=%lf ",sum(a,n));
}
return 0;
}
void read(double a[],int n){
int i;
printf(" Enter %d Values",n);
for(i=0;i<n;i++){
scanf("%lf",&a[i]);
}
}
double sum(double a[],int n){
double total=0.0;
int i;
for(i=0;i<n;i++){
total=total + a[i];
}
return total;
}
void print(double a[],int n){
int i;
for(i=0;i<n;i++){
printf(" %lf",a[i]);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.