1

How do I print the values of sin, cos, and tan from 0 degree to 360 degrees in the C++ programming language?

#include <iostream>
#include <math.h>
using namespace std;
#define PI 3.14159265
int main()
{
    for (int i = 0; i < 360; i++) {
        cout << i << " degrees:" << endl;
        cout << "Sin => " << sin(PI*i/180) << endl;
        cout << "Cos => " << cos(PI*i/180) << endl;
        cout << "Tan => " << tan(PI*i/180) << endl;
    }
}

I have tried above the above code but it gives only the values from 311 degree to 360 degree. How can I print the values of sin, cos, and tan of degree 0 to degree 360 in the console?

L. F.
  • 19,445
  • 8
  • 48
  • 82
Michel JS
  • 13
  • 4
  • mention the exact code to print the values all values in one console output! – Michel JS Mar 24 '20 at 11:32
  • 9
    Are you sure it's not just too much information for your window scrollback buffer? That's about 200 lines of output so, if your buffer is that big, that would explain it. Get rid of all but the last `endl` and see if you get four times more. – paxdiablo Mar 24 '20 at 11:35
  • Welcome to Stack Overflow. Questions asking for the exact code to perform a certain task is off-topic for Stack Overflow. We can, however, help you find problems in your code, since you showed your attempt to solve the problem. By the way, please avoid `using namespace std;` because it is considered bad practice. See [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721) – L. F. Mar 24 '20 at 11:37
  • 1
    You try to print 1440 lines. As paxdiablo already mentioned this may be too much for your terminal. As an alternative try to write to a file. – Lukas-T Mar 24 '20 at 11:49
  • You should be able to extend the # of lines of scrollback that your terminal supports. If this is windows you can do this by right clicking on the title bar then click Properties and Layout and change the number of lines in the height of the Screen Buffer Size. I have mine set to 9000 here. – drescherjm Mar 24 '20 at 11:59

3 Answers3

2

As @paxdiablo mentioned, there's just too many lines of information, so a way to fix that is by removing endl and adding \t for degree, sin and cos as follows, the following code displays all the required information as I checked. The code is almost same but I removed using namespace std and replace your for loop body with the following code:

 for(int i = 0; i < 360; i++) {
        std::cout << i << " degrees:" << "\t";
        std::cout << "Sin => " << sin(PI*i/180) << "\t";
        std::cout << "Cos => " << cos(PI*i/180) << "\t";
        std::cout << "Tan => " << tan(PI*i/180) << std::endl;
    }
  • I checked the output and it showed all the values, in case it doesn't you might have to replace that endl with \t but that might not look as good –  Mar 24 '20 at 11:54
  • the console prints only from degree 287 to degree 360 – Michel JS Mar 24 '20 at 12:36
  • the console prints only from degree 287 to degree 360 – Michel JS Mar 24 '20 at 12:36
  • @MichelJS Did you increase the scrollback buffer yet? It printed every line for me on Windows 10 on a Visual Studio 2019 project. Or are you using some other OS / terminal. I know most linux terminals will allow you to also increase the scrollback buffer as that is one of the first settings I usually change.. – drescherjm Mar 24 '20 at 12:52
  • @Aneesh M Bhat , i used codeblocks , it shows the less values – Michel JS Mar 24 '20 at 13:02
  • @Aneesh M Bhat ,can you please explain the "vector function" method to print the values – Michel JS Mar 24 '20 at 13:03
  • 1
    Is that Code::Blocks on windows or some other OS? If windows this should help you with increasing the scroll back buffer: [https://superuser.com/questions/378313/windows-command-prompt-how-do-i-increase-my-buffer](https://superuser.com/questions/378313/windows-command-prompt-how-do-i-increase-my-buffer) – drescherjm Mar 24 '20 at 13:10
  • Note that after you increase the buffer, you have to run the program again. Then you should have a scroll bar on the cmd.exe window. If this is windows 10 the setting should be saved across runs. – drescherjm Mar 24 '20 at 13:17
  • @Michel JS ill try it but I dunno much about vectors –  Mar 24 '20 at 13:19
0

Here is, how you can get this

#include <iostream>
#include <cmath>
using namespace std;
#define PI 3.14159265
int main()
{
    for(int i = 0; i <= 360; i++) {
        cout << i << " degrees:" << endl;
        cout << "Sin("<< i <<") => " << sin(PI*i/180) << endl;
        cout << "Cos("<< i <<") => " << cos(PI*i/180) << endl;
        cout << "Tan("<< i <<") => " << tan(PI*i/180) << endl;
    }
    return 0;
}

use cmath instead of math.h

Sourav
  • 393
  • 2
  • 18
0

@Michel JS this one uses vectors as u asked, took some time since I didn't know much about it but here u go, I'm hoping this is what you wanted.

#include <iostream>
#include <cmath>
#include <vector>
#define PI 3.14159265

int main()
{
    std::vector<float> sine,cosine,tangent;

    for(int i = 0; i < 360; i++) {
        sine.push_back(sin(PI*i/180));
        cosine.push_back(cos(PI*i/180));
        tangent.push_back(tan(PI*i/180));
    }
    for ( int i = 0 ; i < 360 ; i++)
    { 
        std::cout << i << " degrees:" << "\t";
        std::cout << "Sin => " << sine.at(i) << "\t";
        std::cout << "Cos => " << cosine.at(i) << "\t";
        std::cout << "Tan => " << tangent.at(i) << std::endl;
    }

}