Help with writing Fuctions and driver program

I have a problem with creating a .h function program and a driver program to test the functions.
this is the header file I created:
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#ifndef MYFUNCTIONS_H_INCLUDED
#define MYFUNCTIONS_H_INCLUDED
#include<cassert>
using namespace std;

int max(int m,int n)
{
    int maxNum;
    if(m>n)
    {
        maxNum=m;
    }
    else
    {
        maxNum=n;
    }
    return maxNum;

}
double max(double m, double n)
{
    int maxNum;
    if(m>n)
    {
        maxNum=m;
    }
    else
    {
        maxNum=n;
    }
    return maxNum;

}

int max(int m[ ],int n)
{
    int maxArr = m[10];
    for(int i = 1; i < n; i++)
    if(m[i] > maxArr)
    maxArr = m[i];
    return maxArr;
}

double max (double m[ ], int n)
{
    

}

double max (double m[ ], double n)
{

}

int min ( int m, int n )
{
    int minNum;

    if(m<n)
    {
        m=minNum;
    }
    else
    {
        n=minNum;
    }

    return minNum;

}

double min (double m, double n)
{
    int minNum;

    if(m<n)
    {
        minNum=m;
    }
    else
    {
        minNum=n;
    }

    return minNum;
}

int min (int m[ ], int n)
{

}
double min (double m[ ], double n)
{

}

int absoluteValue ( int m)
{
    int absVal;

    if(m>0)
    {
        absVal = m;
    }
    else
    {
        absVal = -(m);
    }
    return absVal;

}

double absoluteValue ( double m )
{

}

double factorial( int n)
{
    double val=1;

    assert(n>=0);

    while(n>0)
    {
        val *= n;
        n--;
    }
    return val;

}

double combination( int m, int n)
{



}

double permutation( int m, int n)
{

}


#endif // MYFUNCTIONS_H_INCLUDED


this is the driver program i a making
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
#include <iostream>
#include <iomanip>
#include <cassert>
#include "myFunctions.h"

using namespace std;


int main()
{
    cout<<fixed;
    double m,n;
    cin>>m;
    cin>>n;

    cout<<"Max = "<<max(m,n)<<endl;
    cout<<"Min = "<<min(m,n)<<endl;
    cout<<"Absolute Value = "<<absoluteValue(m)<<endl;


    cout<<fixed;
    for(int i=0; i<10; i++)
    {
        cout<<i<<" "<<factorial(i)<<endl;
    }
    return 0;

}


Any help would be appriciated. Am i going about it the right way? PS I cant use the cmath lib.
It's a poor practice to implement functions in a .h (or .hpp) file. This can cause multiply defined symbols at link time if your header is included in more than one .cpp file.

Reduce your header file to just function declarations and move the definitions to their own .cpp file.

It is also a poor practice to include using namespace std; in a .h file This can cause naming conflicts.

your min and max functions can be shortened considerably by use of the ternary operator.
Example:
1
2
3
    int max(int m,int n)
    { return (m<n) ? m : n; 
    }



@DeezyM89 - you do know that C++ has std::min and std::max functions? You don't need to provide them!

See the min/max items in
https://en.cppreference.com/w/cpp/algorithm
http://www.cplusplus.com/reference/algorithm/

Also std:abs()
https://en.cppreference.com/w/cpp/numeric/math/abs
http://www.cplusplus.com/reference/cmath/
yeah, but for this project, we have to create our own. the problem states that we need to know how to create our own functions.
what problem are you having?

some general comments that don't matter:
useless variables / cleanup. Several functions can use this sort of cleanup.
1
2
3
4
5
6
7
8
9
int max(int a, int b)
{
     if(a>b) return a;  //the return ends the function. 
     return b; //else is implied here. equality doesn't care which comes out. 
    //no need for a temporary variable that you then return, no need to test both sides
//of the equality...  and not that much more than the ternary, but easier to read for 
// non c++ gurus
}

double and int:
a double has 64 bits, a big chunk of which are eaten up for the exponent and such. A 64 bit integer then, can represent exact factorials to a bigger number than a double. But, after that number is reached, a double can represent approximate factorials for a long time beyond what will fit. You need to know if you want them exact or approximate. If you end up using a 64 bit int for them, it should be unsigned, and you should then just use a lookup table rather than compute them. If you stick with doubles, what you have is great.

fun way: double absolute value can be hacked via the sign bit in a single logic operation.
We are in 2022 and you continue to use Style-C arrays which is strongly discouraged if you do not have a sharp mastery of C++, because Style-C arrays are sources of bugs in C++
Possibly:

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
#include <iostream>
#include <iomanip>

template<typename T>
T max(T m, T n) {
    return m > n ? m : n;
}

template<typename T>
T min(T m, T n) {
    return m < n ? m : n;
}

template<typename T, size_t N>
T max(T (&m)[N]) {
    auto maxArr {m[0]};

    for (size_t i = 1; i < N; ++i)
        if (m[i] > maxArr)
            maxArr = m[i];

    return maxArr;
}

template<typename T, size_t N>
T min(T(&m)[N]) {
    auto minArr {m[0]};

    for (size_t i = 1; i < N; ++i)
        if (m[i] < minArr)
            minArr = m[i];

    return minArr;
}

template<typename T>
T abs(T m) {
    return m < 0 ? -m : m;
}

uint64_t factorial(unsigned n) {
    uint64_t val {1};

    for (; n > 0; val *= n--);
    return val;
}

int main() {
    double m {}, n {};

    std::cin >> m;
    std::cin >> n;

    std::cout << std::fixed;
    std::cout << "Max = " << max(m, n) << '\n';
    std::cout << "Min = " << min(m, n) << '\n';
    std::cout << "Absolute Value = " << abs(m) << '\n';

    for (unsigned i {}; i < 10; ++i)
        std::cout << i << " " << factorial(i) << '\n';
}

Topic archived. No new replies allowed.