Homework # 5: CS 109 (Programming II) More practices with Programming using fanc
ID: 3607170 • Letter: H
Question
Homework # 5: CS 109 (Programming II) More practices with Programming using fanctions DUE DATE: November 1, 2017 1. Define a function for each of the following: a) Define a function F which takes four inputs: a, b, c. and x-all double and computes and retums the value of ax2 +bx+c b) Define a fiunction that will take an integer N as input and computes and returns the value of 1+2+3+4+5+6N (Use a loop inside the finction to do this) Check both the fiunctions by writing a main and calling both the fiunction with appropriate inputs. 2. Define a function that can be used to find if a particular year is leap year or not. In order to check if a year is leap year or not, follow the steps given below Check if year is divisible by 4, if so, Then check if year is not divisible by 100 or year is divisible by 400 then return true else retum false Else Return false Call this function from main by sending an input for year. 3. Write a program that will use an array called homework to store five different homework scores, tests which will store three different test scores, and midterm and final in variables. Then compute the averageHW score by taking summing the five homeworks and dividing by 5. Compute averageTest score by summing three test scores and dividing by 3. Now compute the overallscore by using the following formula: Overallscore 0.2 averageHW +0.3 averageTest+0.2 midterm+0.3 final Display the OverallScore Now call the function Grade which will return the letterGrade by taking overallScore as input The function returns a character value. Display the letter grade. up ProDisplay P222vaExplanation / Answer
1.(a) and (b)
Sol:
#include<stdio.h>
#include<math.h>
double F(double a,double b,double c,double x) // create function F is return double value;
{
return a*x*x+b*x+c; // calculate and return value
}
int sum(int n) // create a function sum of n numbers
{
int i,a,s=0;
for(i=1;i<=n;i++) // take a loop until n value
{
printf(" Enter a integer no:");
scanf("%d",&a); // read the each digits in th n values
s+=a; // calculate sum of n values
}
return s; // return sum
}
int main() {
double t1=F(3.2,2.2,1.0,2.0); // call the F() and return double value stored into t1
int t=sum(5); // call the sum() function and return sum is stored into t
printf(" F=Value: %.2f",t1); // print F() value
printf(" Sum=%d",t); // print sum() value
return 0;
}
2.
Sol:
#include<stdio.h>
int leapYear(int y)
{
if(y%4==0&&y%100!=0||y%400==0) // Check the condition whether the year is leap or not. if this condition is satisfied then return 1 else return 0;
return 1;
else
return 0;
}
int main()
{
int year;
int t;
printf("Enter year: ");
scanf("%d",&year); // read the year
t=leapYear(year); // call the leapYear()
if(t==1) printf("Leap Year"); // if return leapYear() either 1 or 0 that is stored into t varible
else printf("Not Leap Year");
return 0;
}
Output:
Enter year : 1900
Not Leap Year
Enter Year: 2012
Leap Year
3.
Sol:
#include<stdio.h>
#include<math.h>
char grade(float score) // Finding grade based on score
{
if(score>=4) return 'A'; // check condition the score is more than 4 it return A
else
if(score>=3&&score<=3.9) return 'B'; // check condition the score is in between 3 to 3.9 its return B
else
if(score>=2&&score<=2.9) return 'C'; // check condition the score is in between 2 to 2.9 its return C
else
if(score>=1.5&&score<=1.9) return 'D'; // check condition the score is in between 1.5 to 1.9 its return D
else
return 'F'; // otherwise its return F
}
int main()
{
int hm[3];int mid;int final; // take array variable hm[] and mid and final variable
int i;
float sum=0.0,sum1=0.0,avgHW,avgTest,total;//declare variable sum,avg and total
char ch;
printf(" Enter 3 Home Work Scores:");
for(i=0;i<3;i++) // take loop read the 3 home work scores.
scanf("%d",&hm[i]);
printf(" Enter Midterm Score: "); // take midterm score
scanf("%d",&mid);
printf(" Enter Final Score: "); // take final score
scanf("%d",&final);
for(i=0;i<3;i++)
{
sum+=hm[i]; // calculate sum of 3 home work score
sum1+=hm[i]+mid+final; //calculae sum of 3 home work and mid and final score
}
avgTest=sum/3.0; // calculate average Test Score
avgHW=sum1/5.0; // calculate average home work score
//printf(" Average Test: %.2f",avgTest);
//printf(" Average HomeWork: %.2f",avgHW);
total=0.2*avgHW+0.3*avgTest+0.2*mid+0.3*final; // calculate overall Score
printf(" OverallScore: %.2f",total); // print the Overall Score
ch=grade(total); // call grade function and return specific grade and print
printf(" Grade=%c",ch); // print the grade
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.