-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
78 lines (61 loc) · 2.22 KB
/
main.cpp
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
#include <iostream>
int Add(int* a, int* b); //Add two numbers and return the sum
void AddVal(int* a, int* b, int* result); //Add two numbers and return the sum through the third pointer argument
void Swap(int* a, int* b); //Swap the value of two integers
void Factorial(int* a, int* result); //Generate the factorial of a number and return that through the second pointer argument
void AddVal(int* a, int* b, int* result) {
*result = *a + *b;
return;
}
int Add(int* a, int* b) {
return *a + *b;
}
void Swap(int* a, int* b) {
int helper;
helper = *a;
*a = *b;
*b = helper;
return;
}
void Factorial(int* a, int* result) {
int i;
int lim{ *a };
*result = *a;
for (i = 0; i + 1 < *a; ++i) {
*result = (*result) * (lim - (i + 1));
}
return;
}
/*Main notes -------------------------------------------------------------------------
--> Input not protected
--> Variables can be avoided by storing information on double pointers, but that would be pointless...
thereby defeating this whole excersise, because the point of the excercise is to get the pointer.
-----------------------*/
int main() {
int a{ 5 }, b{ 0 }, c{ 0 }; // for the factorial testing
int d{ 0 }, e{ 0 }; // for swap testing
using namespace std;
/*Addition testing ----------------------------------------------
-----------------------*/
cout << "Enter two numbers to calculate the addition: " << endl;
cin >> a;
cout << "Enter anoether one" << endl;
cin >> b;
AddVal(&a, &b, &c);
cout << "your result is " << c << endl;
/*Factorial testing ----------------------------------------------
-----------------------*/
cout << "Enter a number to calculate the factorial : " << endl;
cin >> a;
Factorial(&a, &b);
cout << "The result of the factorial of " << a << " is: " << b << endl;
/*swap testing ----------------------------------------------------
-----------------------*/
cout << "enter a two digit number to swap: " << endl;
cin >> a;
d = a / 10;
e = a - d * 10;
Swap(&d, &e);
cout << "The result is: " << (d * 10 + e) << endl;
return 0;
}