Skip to content

Instantly share code, notes, and snippets.

@dvtate
Created May 28, 2016 21:56
Show Gist options
  • Save dvtate/ab7eba3d9217e0c4dffd33f9223f2fd8 to your computer and use it in GitHub Desktop.
Save dvtate/ab7eba3d9217e0c4dffd33f9223f2fd8 to your computer and use it in GitHub Desktop.
Plots a linear function in the terminal. Kevin Kim and I worked on this.
#include<iostream>
void linearGraph(double m, double x, double b);//graphing function
int main(){
//linear:
float m, b;//f(x) = mx+b
int x_min, x_max;//min_x <= x <= max_x
//vars:
std::cout <<"\nSlope: ";
std::cin >>m;
std::cout <<"Y-intercept: ";
std::cin >>b;
//domain:
std::cout <<"X-min: ";
std::cin >>x_min;
std::cout <<"X-max: ";
std::cin >>x_max;
if (x_min > x_max)//Doesn't get graph
std::cout <<"\nWARNING:\n Your x-min is greater than your x-max.\n";
for(int i = x_min; i <= x_max; i++)//iterate through given x-values
linearGraph(m, i, b);//plot y-value for each x-value
}
void linearGraph(double m, double x, double b){
//plot y-value at given x-value
for(int i = 0; i < (m * x + b); i++)//run once for each whole number
std::cout <<"█";
std::cout <<" : f(" <<x <<")=" <<(float) m * x + b <<" \n" ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment