Hi, I am a begginner programmer that is attempting to create a program that will spit out the (x,y) plots for a graph. Currently, I only need it to give me the x and y data in (x,y) form, so here is my current code. By the way, I am experiencing errors in it so if anyone could proofread it that would be greatly appriciated.

--Dylan

//Grapher

#include <iostream>;
#include <fstream>;
#include <string.h>;
#include <cctype>;
using namespace std;

void grapher(float, float, int, int, int, int);

int main() {
    float yInt;
    float xInt;
    float m;
    int yMin = -10;
    int yMax = 10;
    int xMin = -10;
    int xMax = 10;
    
    cout << "Welcome to teh GRAPHER" << endl;
    cout << "Key = Y=M*X+B" << endl;
    cout << "Enter M: ";
    cin >> m;
    cout << "Enter B: ";
    cin >> yInt;
    grapher(m, yInt, yMin, yMax, xMin, xMax);
    
    system("pause");
    return 0;
}

grapher(float m, float yInt,int yMin,int yMax, int xMin, int xMax) {
    int counter = xMin;
    cout << "Calculating graph..." << endl;
    float xData[];
    float yData[];
    
    while(counter <= yMax || counter >= yMin || counter =< xMax || counter => xMin){
        cout << "In teh Graph Calculating Prog..." << endl;
        xData[counter] = counter;
        yData[counter] = xData[counter] * m + yInt;
        cout << "(" << xData[counter] << "," << yData[counter] << ")" << endl;
        }
    return;
}

You don't need ; after your #includes

You need a return type for your function definition (and you can omit the return)

>= and <= are the only valid ways of writing those operators

you need to give an array size for xData and yData

just follow the errors that you get and start from the top of the list and correct them...

you need to move the counter along by one and save "dumping" your x,y value arrays until the end. you can transform your while loop into a for.

Edited 7 Years Ago by jonsca: n/a

How to declare an array in c++ :

int Array[4] = {1,2,3,4};
const int SIZE = 6;
float Array[SIZE] = {0}; //set all elements to 0
char Array[5] = {'1','2','3','4','\0'}; // "\0" is the null character and an array of char should end with it.

int Array[]; // invalid form
int[] Array; //invalid form

Alright, Well, I think I have the bulk of the code worked out. The plotting seems to work out fine, here is my revised code:

//Grapher

#include <iostream>
#include <fstream>
#include <string.h>
#include <cctype>
using namespace std;

//void grapher(float, float, int, int, int, int);
void grapher (float m, float yInt,int yMin,int yMax, int xMin, int xMax) {
    int counter = yMin;
    cout << "Calculating graph..." << endl;
    float xData[xMax-xMin];
    float yData[yMax-xMin];
    int yDifference = xMax-yMin;
    
    for(counter; counter <=yDifference; counter++) {
        yData[counter] = counter;
        xData[counter] = counter;
        xData[counter] = xData[counter] * m + yInt;
        cout << "(" << xData[counter] << "," << yData[counter] << ")" << endl;
        }
}


int main() {
    float yInt;
    float xInt;
    float m;
    int yMin = -10;
    int yMax = 10;
    int xMin = -10;
    int xMax = 10;
    
    cout << "Welcome to teh GRAPHER" << endl;
    cout << "Key = Y=M*X+B" << endl;
    cout << "Enter M: ";
    cin >> m;
    cout << "Enter B: ";
    cin >> yInt;
    grapher(m, yInt, yMin, yMax, xMin, xMax);
    
    system("pause");
    return 0;
}

This does mostly what I want it to. One problem is with this code (In the grapher(); function:

if(xData[counter] <= xMax || xData[counter] >= xMin || yData[counter] <= yMax || yData >= yMin) {
            cout << "Finished." << endl;
            system("pause");
            system("cls");
            main();
            }

I get an error :/?

Also with the equation y=1x+0 (so y=x), the program spits out

Welcome to teh GRAPHER
Key = Y=M*X+B
Enter M: 1
Enter B: 0
Calculating graph...
(-10,-2.5625)
(-9,2.29589e-041)
(-8,0)
(-7,0)
(-6,3.76489e-039)
(-5,5.88488e-039)
(-4,6.26338e-039)
(-3,6.24485e-039)
(-2,-2)
(-1,-1)
(0,0)
(1,1)
(2,2)
(3,3)
(4,4)
(5,5)
(6,6)
(7,7)
(8,8)
(9,9)
(10,10)
(11,11)
(12,12)
(13,13)
(14,14)
(15,15)
(16,16)
(17,17)
(18,18)
(19,19)
(20,20)
Press any key to continue . . .

Any help with the numbers before (-2,-2)?

Edited 7 Years Ago by dylank: n/a

You're calling main at the end of your function and you shouldn't be.

The problem with your arrays is that you are trying to go from yData[xMin] which may not (and in fact does not in this case) contain the proper values. I might think about deriving yMin and yMax from y = xMin*m+b and y = xMax*m+b respectively.

There may be an issues with all the floats and ints around. Instead of declaring some of them ints to fit in with your loop schema, you may be better off casting from float to int (beware rounding) when you have to.

You're most of the way there... test your example for more cases, e.g. decimals.

This article has been dead for over six months. Start a new discussion instead.