[C++] Need help with win32 program

Discussion in 'C++' started by thekevin07, Sep 13, 2008.

  1. thekevin07

    thekevin07 New Member

    Joined:
    Sep 13, 2008
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    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

    Code:
    // 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;
    }
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Just change
    if(mx==iDataX[z])
    to
    mx=iDataX[z];
    and lose the else clause.

    You don't need an if at this point because if(my==iDataY[z]) already removes all cases where there isn't a * to plot. So all you need to do within this if is to look up the value of iDataX[z] and draw the * in the appropriate place.
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    You also don't need the mx--. It was this that was making it "work" when the entered values were consecutive. Consequently you also don't need mx2 as mx doesn't change over the output loop.
     
  4. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    Just a small correction to xpi0t0s's analysis of your code. mx does change during the output loop because you are assigning to it (in the fixed version). So you do still need mx2.

    By the way, you should get used to using zero-based array subscripting, since that is the C/C++ way.
     
  5. thekevin07

    thekevin07 New Member

    Joined:
    Sep 13, 2008
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    Thank you works perfectly now
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice