...

View Full Version : [C++] Need help with command line graphing



thekevin07
09-13-2008, 04:56 AM
I need to graph 3 points based on the linear or quadratic equation and I can't get my head around how to graph the points. It will work fine if you put points in that are in sequence like 2 3 4 but not 2 4 5 any help is greatly appreciated below is the source from microsoft visual studio 2008


// program002.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <cstdio>
#include <cmath>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

int iNumOfDataPoints, iEquation;
double mx, my;


//linear or quadratic if the user enters 0 or any other number the program assums quadratic
cout << "Choose an equation: Enter 1 for Linear and 0 for quadratic (default is quadratic)" << endl;
cin >> iEquation;


//ask user how many data points
cout<<"Please enter your number from lowest to highest."<<endl;
cout << "How many data points would you like to plot (max 3)?" << endl;
cin >> iNumOfDataPoints;

double iDataX[4], iDataY[4];

for(int c=1;c<=iNumOfDataPoints;c++)
{
cout << "Enter data point(x) ";
cout << c << endl;
cin >> iDataX[c];

if(iDataX[c]<=0)
{
cout << "Data point must be positive number. " << endl;
return 0;
}

//now lets get the y values
//linear
if(iEquation==1)
{
iDataY[c]= 2*iDataX[c] + 4;
}
else //quadratic
{
iDataY[c]= pow(iDataX[c],2) + 4*iDataX[c]+2;
}
}

cout << endl;
for(int c=1;c<=iNumOfDataPoints;c++)
{
cout << iDataY[c] << endl;
}

cout << endl;
cout << endl;
//find largest value

mx=std::max(iDataX[1],iDataX[2]);
mx=std::max(mx,iDataX[3]);
double mx2=mx;



my=std::max(iDataY[1],iDataY[2]);
my=std::max(my,iDataY[3]);



//graph
int z=iNumOfDataPoints;

for(my;my!=0;my--)
{
cout<<"|";
if(my==iDataY[z])
{
if(mx==iDataX[z])
{
int n=1;
while(n<mx)
{
cout<<" ";
n++;
}
cout<<"*"<<endl;
z--;
}
else
{
cout<<endl;
}
mx--;
}
else
{
cout<<endl;
}
}


for(int f=0;f<=mx2;f++)
{
cout << "-";
}
cout << endl;



return 0;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum