C++ Simple Math Problems

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2009
Posts: 19
Reputation: dylank is an unknown quantity at this point 
Solved Threads: 0
dylank dylank is offline Offline
Newbie Poster

C++ Simple Math Problems

 
0
  #1
31 Days Ago
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) :
  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training
 
0
  #2
31 Days Ago
  1. int xMin = -10;
  2. int xMax = 10;
  3.  
  4. double xData[xMax - xMin];
xData contains 20 elements starting from 0 to 19.

  1. int counter = xMin;
  2. for (counter; counter <=xMax; counter++) {
While counter runs from -10 to 10
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 19
Reputation: dylank is an unknown quantity at this point 
Solved Threads: 0
dylank dylank is offline Offline
Newbie Poster
 
0
  #3
31 Days Ago
Originally Posted by invisal View Post
  1. int xMin = -10;
  2. int xMax = 10;
  3.  
  4. double xData[xMax - xMin];
xData contains 20 elements starting from 0 to 19.

  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 19
Reputation: dylank is an unknown quantity at this point 
Solved Threads: 0
dylank dylank is offline Offline
Newbie Poster
 
0
  #4
31 Days Ago
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..
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training
 
0
  #5
31 Days Ago
Originally Posted by dylank View Post
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.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Reply

Tags
array, c++, graph, variable

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC