Graph on Console


Thanks.
Last edited on
I think I would have three functions here:
1
2
3
4
5
6
7
8
9
10
11
12
// Converts degrees to radians
double radians(int degrees);

// Maps a value to a range
// For changing your results of -1 - 1 to 0 - 80 (the console width)
//   (Figuring out the position of the *)
// Called like: int result = map(0 , 80,-1.0, 1.0, sin( radians(60) ));  
int map(int viewLow, int viewHigh, double dataMin, double dataMax, double data);

// Print a line
// given the position of the astericks, print the line with a '|' in the middle
void printLine(int astericksPos,  int linePos = 40);


Main would be:
1
2
3
4
5
6
7
8
9
10
11
int main(void)
{
  for (int i = 0; i < 360; i++)
  {
    double rads = radians(i);
    double value = sin(rads);
    int mapped = map(0, 80, -1.0, 1.0, value);
    printline(mapped);
  }
  return 0;
}


Last edited on
Thanks.
Last edited on
Do a google search of modular programming. I'm not too familiar with the term, so I figure it has to do with OOP.

Function example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

//Function declaration
void hello();

int main(void)
{
  hello();
  return 0;
}

// Function definition
void hello()
{
  cout << "Hello World\n";
}


The function declaration only states that there is something called "hello", and that it takes no arguments. The Definition is where the actual code of the function goes.

For more about functions:
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/

As you can see from my first post, I left the real work for you :)


Topic archived. No new replies allowed.