Question

Homework 4 – Create your own Function Create your own function in C that accepts one...

Homework 4 – Create your own Function Create your own function in C that accepts one input number and returns a double number. The themes for the functions should be one of the following:  Calculates 3.14 times of the square of input number. For example, if 2 is input then 12.56 should be returned. (e.g. 3.14*2*2 = 12.56)  Divides the number by 3 and returns the result. For example, if 6 was input then 2.0 should be returned.  Squares the number and returns the result. For example, if 12.1 was entered then 146.41 would be returned. You should provide both your C code and an example call to the C code function. Be sure to provide an overview of what your function is doing. Include header documentation in the code as well as internal code documentation. Provide a screen capture showing the results of testing your code in an online compiler. Be sure to test your code with several test cases and show your test case table.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program

#include<stdio.h>

//function prototypes
double getArea(double rad);
double divByThree(double n);
double square(double n);

//main function
int main()
{
//variable declaration
double rad, m, n;

/*** Testing ***/
printf("*** Testing ***\n");
rad = 2;
n = getArea(rad);
printf("Area of the circle is %.2f\n", n);

n = 6;
m = divByThree(n);
printf("%.2f divide by three is %.2f\n", n, m);

n = 12.1;
m = square(n);
printf("Square of %.2f is %.2f\n", n, m);

/**** user input ***/
printf("\n\n*** User input ***\n");

printf("Enter the radius: ");
scanf("%lf", &rad);
n = getArea(rad);
printf("Area of the circle is %.2f\n", n);

printf("Enter a number: ");
scanf("%lf", &n);
m = divByThree(n);
printf("%.2f divide by three is %.2f\n", n, m);

printf("Enter a number: ");
scanf("%lf", &n);
m = square(n);
printf("Square of %.2f is %.2f\n", n, m);

return 0;
}

//function to return the area of circle
double getArea(double rad)
{
double area = 3.14*rad*rad;
return area;
}

//function to divides the number by 3 and returns the result
double divByThree(double n)
{
double d = n/3;
return d;
}

//function to square the number and returns the result
double square(double n)
{
double d = n * n;
return d;
}

Output:

*** Testing ***
Area of the circle is 12.56
6.00 divide by three is 2.00
Square of 12.10 is 146.41


*** User input ***
Enter the radius: 10
Area of the circle is 314.00
Enter a number: 30
30.00 divide by three is 10.00
Enter a number: 20
Square of 20.00 is 400.00

Add a comment
Know the answer?
Add Answer to:
Homework 4 – Create your own Function Create your own function in C that accepts one...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create your own unique While-End or (For End) repetition C code. You decide the theme. Be...

    Create your own unique While-End or (For End) repetition C code. You decide the theme. Be sure to provide an overview of what your repetition structure is doing. Please keep the design simple for this exercise. Just a few lines of code is all that is needed for this response. This should be code you wrote for an application that is interesting to you. In other words, make it your own and have fun with it. Provide the C code...

  • Please write the code using matlab 1) i. Create an anonymous function named optimist, that accepts...

    Please write the code using matlab 1) i. Create an anonymous function named optimist, that accepts a single variable as input i Create a row vector Tthat represents the number of seconds in two minutes, starting ii. Call the function optimist on the vector T, store the result in variable R1 and returns the double of that variable from one and ending at a hundred and twenty v. Create an anonymous function named pessimist, that accepts a single variable as...

  • In c++ #include <iostream > 2 #include <set> #înclude <functional» 5 using namespace std; 7 //your...

    In c++ #include <iostream > 2 #include <set> #înclude <functional» 5 using namespace std; 7 //your code 9 int main) 4 6 8 1e set <double, greater<double>>valuesA --1.1, 2.9, -2.3, 2.71 J: set <double, greater<double>> valuesB -3.14, 2.71, -3.88, 2.19; double value; cin > value; 12 13 14 15 16 17 18 19 /your code return ; Scenario Write a program that creates two sets (the sets are given in the code below) and asks the user for a number....

  • For this question you need to create a function in Octave called MyFunction that accepts two...

    For this question you need to create a function in Octave called MyFunction that accepts two inputs, a list L and an integer K, and returns two values minsum and maxsum. A template function including the function prototype (first line) has already been created for you to use (do not change this). I will only mark the contents of the MyFunction.m file. Any code added to the main.m file is for your testing only and will not be marked. Your...

  • In basic C please with comments so I can redo It on my own, thank you!...

    In basic C please with comments so I can redo It on my own, thank you! For this lab, you only need to write a single user-defined function that analyzes a 2D array of chars. You do NOT need to write any of the code in main() that calls your function. Make sure to use the template and only add code to the user-defined function. Write a function win() with the following definition (i.e. prototype): int win(char board[7][5], char player)...

  • Problem 12.12 Pls show the m-file Develop your own M-file function for the Gauss-Seidel method without...

    Problem 12.12 Pls show the m-file Develop your own M-file function for the Gauss-Seidel method without relaxation based on Fig. 12.2, but change the first line so that it returns the approximate error and the number of iterations: function [x, ea, iter] = ... GaussSeidel (, b, es, maxit) Test it by duplicating Example 12.1 and then use it to solve Prob. 12.2a. Develop your own M-file function for Gauss-Seidel with relaxation. Here is the function's first line: function [x,...

  • Create a program that will determine the even number(s) from a list of numbers. Your program...

    Create a program that will determine the even number(s) from a list of numbers. Your program should create and call a function FindEven that accepts a list of numbers and returns a list of only the even numbers in sorted order. Note that the original list is given in the starter code. def body(): numberlist = [1, 1000, 5, -3, 2, 16 # Write your code here and notice level # Do NOT include: if __name__ == been provided for...

  • C++ visual studio program...Write your own version of a class template that will create a dynamic...

    C++ visual studio program...Write your own version of a class template that will create a dynamic stack of any data type. The pop function must return a bool; it should return a false if it was not able to pop an item off the stack. Otherwise it returns true. The parameter to the pop function is passed by reference and should be the item on the list if it was able to pop something. Create a driver program (main) that...

  • Write a C program that includes a function called moveEm() that accepts the ADDRESSES of three...

    Write a C program that includes a function called moveEm() that accepts the ADDRESSES of three double variables as input. The function should move the value of the smallest variable into the first variable, the middle value into the middle variable, and the largest value into the third variable. Call your function from within your program using the arguments 7.8, 3.5, and 1.1 in order to demonstrate that it works (make sure your logic works for any order of largest,...

  • python Write a function that takes as input a single integer parametern and computes the nth...

    python Write a function that takes as input a single integer parametern and computes the nth Fibonacci Sequence number The Fibonacci sequence first two terms are 1 and 1(so, if n - 2 your function should return 1). From there, the previous two values are summed to come up with the next result in the sequence 1.1,2,3,5,8,13, 21, 34, 55, etc.) For example, if the input is 7 then your function should return 13 26561.1615880 1 def Fibonacci(n): # your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT