Need help with functions!

So I am trying to print the clark's tables for sin, cos tan etc and here is the code for sin :
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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
#include <iomanip>
using namespace std;


constexpr double pi = 3.14159;

double deg_to_rad(double deg){
     return deg * ( pi / 180 );
}

int main () {

     

     for (int i = 0; i < 90; ++i){

          cout << i << "  "; // to show which angle the row is

          for (double j = 0; j <= 0.9; j+= 0.1)
          {
               cout << setprecision(4) << sin( deg_to_rad(i + j) ) << " ";
          }

          cout << "\n";
     }

     cout << "\n";
     return 0;
}

The thing is, to print the tables of cos and tan, all I have to do is replace sin in line 26 with cos or tan. How do I change the above code (lines 20 to 30) to the function named clarketable that, if I call clarktable(cos) from the main function, it prints that table? That is, I want sin, cos or tan to be a parameter.
Last edited on
You can use an enumeration [1] and pass SIN, COS or TAN to your function. In your function you can have a 'switch' to print the correct table on the basis of what was passed as a parameter.

[1] http://en.cppreference.com/w/cpp/language/enum
Last edited on
In your function you can have a 'switch' to print the correct table on the basis of what was passed as a parameter.

Will not the function become thrice as large if I use the switch? All I want is to change one thing in line 26. Is there a way to search and replace just that in the function? Something like a 'define'? I am new so forgive me for asking stupid questions.
No, the function will become only slightly larger. What I was thinking of is something like this:

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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
#include <iomanip>
using namespace std;


const double pi = 3.14159;

double deg_to_rad(double deg){
     return deg * ( pi / 180 );
}

enum FUNCTION {SIN, COS, TAN};

void table(FUNCTION f) {
     for (int i = 0; i < 90; ++i){

          cout << i << "  "; // to show which angle the row is

          for (double j = 0; j <= 0.9; j+= 0.1)
          {
               switch(f) {
                    case SIN:
                    cout << setprecision(4) << sin( deg_to_rad(i + j) ) << " ";
                    break;

                    case COS:
                    cout << setprecision(4) << cos( deg_to_rad(i + j) ) << " ";
                    break;

                    case TAN:
                    cout << setprecision(4) << tan( deg_to_rad(i + j) ) << " ";
                    break;
               }
          }

          cout << "\n";
     }
}

int main () {

     table(COS);     

     cout << "\n";
     return 0;
}
there a way to search and replace just that in the function?
You can pass function as a function parameter. And you can use switch to pass correct function as parameter.

Example:
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
#include <iostream>
#include <iomanip>
#include <functional>
#include <cmath>

constexpr double pi = 3.14159265358;

double deg_to_rad(double deg)
{
    return deg * ( pi / 180 );
}

void print_table_impl(std::function<double(double)> op)
{
    for (int i = 0; i < 90; ++i) {
        std::cout << std::setw(2) << i;
        for (int j = 0; j < 10; ++j)
            std::cout << ' ' << std::setw(7) << op( deg_to_rad(i + j/10.) );
        std::cout << '\n';
    }
}

enum func { SIN, COS, TAN };

void print_table(func op)
{
    switch(op) {
        case SIN: print_table_impl(sin); break;
        case COS: print_table_impl(cos); break;
        case TAN: print_table_impl(tan); break;
    }
}

int main ()
{
    std::cout.flags(std::ios::fixed);
    std::cout.precision(4);
    std::cout << "Which table do you need?\n\t1: Sin\n\t2: Cos\n\t3: Tan\n";
    int choice;
    std::cin >> choice;
    switch(choice) {
        case 1: print_table(SIN); break;
        case 2: print_table(COS); break;
        case 3: print_table(TAN); break;
    }
}
Of cource you can ditch enum and pass integers directly to print_table.
Edit: fixed missing breaks
Edit 2: Beautified output.
Last edited on
minomic, Thank you so much :)
MiiNiPa, Thank you! That cleared it up! Never knew there was something like that!
For underlying mechanism you can read information on function pointers (they are slightly more complex to use, but it is what std::function uses internally):
http://www.learncpp.com/cpp-tutorial/78-function-pointers/
Topic archived. No new replies allowed.