Hi, there are several threads on this issue, but none seem to give me a good answer. My current code is this (This program plots points on a graph) :

//Put line equasion into arrays.
#include <iostream>
using namespace std;

double getData(int counter, double m, double b) {
       int yValue = counter * m;
       yValue = yValue + b;
       return yValue;
       }

int main() {
    cout << "Putting Y = MX+B into arrays xData[] and yData[]" << endl;
    
    int xMin = -10;
    int xMax = 10;
    double b = 0;
    double m = 2;
    
    //int yMin = xMin*m+b;
    //int yMax = xMax*m+b;
    
    
    int counter = xMin;
    double xData[xMax - xMin];
    double yData[xMax - xMin];
    double yTemp;
    
    for (counter; counter <=xMax; counter++) {
        yTemp = getData(counter, m, b);
        xData[counter] = counter;
        yData[counter] = yTemp;
        cout << "(" << xData[counter] << "," << yData[counter] << ")   " << endl;
        }
        cout << yData[-5];
    system("pause");
    return 0;
}

The code returns the following output:

Putting Y = MX+B into arrays xData[] and yData[]
(-10,2.24756e-307)
(-9,-18)
(-8,-16)
(-7,3.47668e-310)
(-6,0)
(-5,1.78923e-307)
(-4,2.22516e-307)
(-3,-6)
(-2,-4)
(-1,-2)
(0,0)
(1,2)
(2,4)
(3,6)
(4,8)
(5,10)
(6,12)
(7,14)
(8,16)
(9,18)
(10,20)
1.78932e-307Press any key to continue . . .

What is the deal? I did this source code on paper (Like what the values would hold step by step) and even had the program skip yData[] entirly and give a correct answer. It seems having the data in the array is screwing it up. If anyone can help, that would be great!
--Dylan

PS, no matter what M is, the weird outputs stay the same.

Recommended Answers

All 4 Replies

int xMin = -10;
int xMax = 10;

double xData[xMax - xMin];

xData contains 20 elements starting from 0 to 19.

int counter = xMin;
for (counter; counter <=xMax; counter++) {

While counter runs from -10 to 10

int xMin = -10;
int xMax = 10;

double xData[xMax - xMin];

xData contains 20 elements starting from 0 to 19.

int counter = xMin;
for (counter; counter <=xMax; counter++) {

While counter runs from -10 to 10

How does that help me? My problem is the program putting values like (-5,1.78923e-307) out. I can tell you now, -5 * 2 + 0 is not 1.78923e-307.

Thanks people, I got the answer from another fourm I was posting on. The problem was no using negative values in the array identifier ers. So i had to make a new var..

Thanks people, I got the answer from another fourm I was posting on. The problem was no using negative values in the array identifier ers. So i had to make a new var..

Well, maybe you didn't get my message from my post at all. First index of an array is 0. xData[xMax - xMin] equals to xData[20] because xMax - xMin = 20 . So allowed index should be in range of 0 to 19. Your counter runs from -10 to 10 and you use counter as index of your array which is not in range for the first 10 negative numbers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.