944,155 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2188
  • C++ RSS
Nov 3rd, 2009
0

C++ Simple Math Problems

Expand Post »
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) :
C++ Syntax (Toggle Plain Text)
  1. //Put line equasion into arrays.
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. double getData(int counter, double m, double b) {
  6. int yValue = counter * m;
  7. yValue = yValue + b;
  8. return yValue;
  9. }
  10.  
  11. int main() {
  12. cout << "Putting Y = MX+B into arrays xData[] and yData[]" << endl;
  13.  
  14. int xMin = -10;
  15. int xMax = 10;
  16. double b = 0;
  17. double m = 2;
  18.  
  19. //int yMin = xMin*m+b;
  20. //int yMax = xMax*m+b;
  21.  
  22.  
  23. int counter = xMin;
  24. double xData[xMax - xMin];
  25. double yData[xMax - xMin];
  26. double yTemp;
  27.  
  28. for (counter; counter <=xMax; counter++) {
  29. yTemp = getData(counter, m, b);
  30. xData[counter] = counter;
  31. yData[counter] = yTemp;
  32. cout << "(" << xData[counter] << "," << yData[counter] << ") " << endl;
  33. }
  34. cout << yData[-5];
  35. system("pause");
  36. return 0;
  37. }
The code returns the following output:
C++ Syntax (Toggle Plain Text)
  1. Putting Y = MX+B into arrays xData[] and yData[]
  2. (-10,2.24756e-307)
  3. (-9,-18)
  4. (-8,-16)
  5. (-7,3.47668e-310)
  6. (-6,0)
  7. (-5,1.78923e-307)
  8. (-4,2.22516e-307)
  9. (-3,-6)
  10. (-2,-4)
  11. (-1,-2)
  12. (0,0)
  13. (1,2)
  14. (2,4)
  15. (3,6)
  16. (4,8)
  17. (5,10)
  18. (6,12)
  19. (7,14)
  20. (8,16)
  21. (9,18)
  22. (10,20)
  23. 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.
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
dylank is offline Offline
66 posts
since Oct 2009
Nov 4th, 2009
0
Re: C++ Simple Math Problems
C++ Syntax (Toggle Plain Text)
  1. int xMin = -10;
  2. int xMax = 10;
  3.  
  4. double xData[xMax - xMin];
xData contains 20 elements starting from 0 to 19.

C++ Syntax (Toggle Plain Text)
  1. int counter = xMin;
  2. for (counter; counter <=xMax; counter++) {
While counter runs from -10 to 10
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Nov 4th, 2009
0
Re: C++ Simple Math Problems
Click to Expand / Collapse  Quote originally posted by invisal ...
C++ Syntax (Toggle Plain Text)
  1. int xMin = -10;
  2. int xMax = 10;
  3.  
  4. double xData[xMax - xMin];
xData contains 20 elements starting from 0 to 19.

C++ Syntax (Toggle Plain Text)
  1. int counter = xMin;
  2. 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.
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
dylank is offline Offline
66 posts
since Oct 2009
Nov 4th, 2009
0
Re: C++ Simple Math Problems
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..
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
dylank is offline Offline
66 posts
since Oct 2009
Nov 4th, 2009
0
Re: C++ Simple Math Problems
Click to Expand / Collapse  Quote originally posted by dylank ...
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.
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How do I set shared ptr to NULL
Next Thread in C++ Forum Timeline: Need help....





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC