-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctionex1.c
102 lines (82 loc) · 2.34 KB
/
functionex1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* Add name date and description here
* Example of a program using Library (stdlib, stdio) and Programmer defined functions
*/
#define _CRT_SECURE_NO_WARNINGS //for Visual Studio compiler
#pragma warning(disable:6031) //ignore scanf warnings
#include<stdio.h> //for input and output
#include<stdlib.h> //for absolute value
//calculates and returns the quotient of
//argument one divided by argument 2
//does not allow division by 0, the function will return 0.0 if the denominator has a 0 value
double FindQuo(double arg1, double arg2);
//calculates and returns the absolute value
//of the difference of 2 integers
int FindAbsoluteValue(int arg1, int arg2);
int main()
{
int num1, num2, absResMain;
double num3, num4, quotientMain;
//Greet the user by calling the Greeting function
Greeting();
//invoke or call the GetInteger function twice to get 2 integers
num1 = GetInteger();
num2 = GetInteger();
//invoke or call the function to do the absolute value calculation
absResMain = FindAbsoluteValue(num1, num2);
//invoke or call the GetDouble function twice to get 2 doubles
num3 = GetDouble();
num4 = GetDouble();
//invoke or call the function to do the quotient calculation
quotientMain = FindQuo(num3, num4);
//print the results
printf("\nThe absolute value of %d minus %d is %d\n\n", num1, num2, absResMain);
printf("\n%.2f divided by %.2f is %.2f\n\n", num3, num4, quotientMain);
//Say goodbye to the user
printf("\nGoodbye, Have a great day!\n");
return 0;
}
//greet the user
void Greeting()
{
printf("\nWelcome to the absolute value and quotient calculator\n");
}
//ask, get, and return an integer
int GetInteger()
{
int num;
printf("\nEnter an integer: ");
scanf("%d", &num);
return num;
}
//ask, get, and return a double
double GetDouble()
{
double num2;
printf("\nEnter a double: ");
scanf("%lf", &num2);
return num2;
}
//calculates and returns the quotient of
//argument one divided by argument 2
//does not allow division by 0, the function will return 0.0 if the denominator has a 0 value
double FindQuo(double arg1, double arg2)
{
if (arg2 == 0)
{
printf("\ncannot divide by zero\n");
return 0.0;
}
else
{
return arg1 / arg2;
}
}
//calculates and returns the absolute value
//of the difference of 2 integers
int FindAbsoluteValue(int arg1, int arg2)
{
int absResult;
absResult = abs(arg1 - arg2);
return absResult;
}