Greetings everyone, i have this assignment in \" C\" LANGUAGE and im trying to u
ID: 3863845 • Letter: G
Question
Greetings everyone, i have this assignment in "C" LANGUAGE and im trying to use three loops in a array but im missing one loop. here are the instructions.
Write a small program that:
fills an array of doubles with user input,
prints the doubles on the screen in a column,
adds up all the doubles in the array
prints the sum onto the screen.
- You must use at least 3 functions
- Declare an array of doubles of size 12.
- Prompt the user for how many doubles they want to enter <= 12.
- Use a loop to read the doubles into the array from the keyboard.
- 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
Please help me and it is in program C not C++.
Thank you
Explanation / Answer
// C code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
double calculateSum(double doubleArray[],int n)
{
int i;
double sum=0;
// Use a loop to add up all the items in the array and store the sum
for(i=0;i<n;i++)
{
sum=sum+doubleArray[i];
}
return sum;
}
// Use a loop to print the array onto the screen in a column.
void displayArray(double doubleArray[],int n)
{
int i;
printf(" Double Array: ");
// Use a loop to print the array onto the screen in a column.
for(i=0;i<n;i++)
{
printf("%lf ",doubleArray[i]);
}
}
int checkSize(int n)
{
if(n >= 1 && n <= 12)
return 1;
else
return 0;
}
int main()
{
// Declare an array of doubles of size 12.
double doubleArray[12];
// Prompt the user for how many doubles they want to enter <= 12.
int n,i;
while(1)
{
printf("How many doubles you want to enter.(less than equal to 12): ");
scanf("%d",&n);
if(checkSize(n) == 1)
break;
else
printf("Invalid Input ");
}
// Use a loop to read the doubles into the array from the keyboard.
printf("Enter the doubles ");
for(i=0;i<n;i++)
{
scanf("%lf",&doubleArray[i]);
}
displayArray(doubleArray,n);
double sum=calculateSum(doubleArray,n);
printf(" Array sum:%lf ",sum);
return 0;
}
/*
output:
How many doubles you want to enter.(less than equal to 12): 20
Invalid Input
How many doubles you want to enter.(less than equal to 12): 5
Enter the doubles
1
2
3
4
2
Double Array:
1.000000
2.000000
3.000000
4.000000
2.000000
Array sum:12.000000
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.