Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 13th, 2008, 11:57 AM   #1
ShawnStovall
Call me Chuck
 
ShawnStovall's Avatar
 
Join Date: Aug 2007
Location: USA, MI
Posts: 37
Rep Power: 0 ShawnStovall is on a distinguished road
Send a message via AIM to ShawnStovall
Multi-dimensional vectors

I'm creating a program to help me solve polynomial multiplication so I won't have to do all that multiplying.(I'm lazy. )And I have run into a problem. I found that in order for the multiplication to turn out right I have to create dynamic two-dimensional arrays. I messed with new for a bit until I just figured it would be more efficient to just use the vector class. But the problem is I keep on getting "Vector subscript out of range" when I get to a certain point in my program(commented bellow).

C++ Syntax (Toggle Plain Text)
  1. // main.cpp -- This program will solve any number of binomials multiplied together
  2. #include <iostream>
  3. #include <vector>
  4. using std::vector;
  5. using std::cout;
  6. using std::cin;
  7. using std::endl;
  8.  
  9. int main()
  10. {
  11. char again = 'y';
  12.  
  13. cout << "Programed by Shawn Stovall(With assistance from Carl Bondy)\n04-10-08\nPROTOTYPE\n\n";
  14.  
  15. while(again != 'n')
  16. {
  17. vector<double> coeff;
  18. vector<double> lone;
  19. vector<vector<double> > coeff_sqr;
  20. vector<vector<double> > coeff_ans;
  21. vector<vector<double> > coeff_ans2;
  22. vector<vector<double> > lone_ans;
  23. int num_poly;
  24.  
  25. cout << "Please enter the number of binomials in your problem: ";
  26. cin >> num_poly;
  27.  
  28. coeff.resize(num_poly);
  29. lone.resize(num_poly);
  30. coeff_sqr.resize(num_poly);
  31. coeff_ans.resize(num_poly);
  32. coeff_ans2.resize(num_poly);
  33. lone_ans.resize(num_poly);
  34.  
  35. for(int i = 0; i < num_poly; i++)
  36. {
  37. coeff_sqr[i].resize(num_poly);
  38. coeff_ans[i].resize(num_poly);
  39. coeff_ans2[i].resize(num_poly);
  40. lone_ans[i].resize(num_poly);
  41. }
  42.  
  43. cout << "Each binomial will be in the form: \n\n([coefficient]x + [number])"
  44. "\nExample: (x + 7)(5x + -3)\n\n"
  45. "Please enter the binomials:\n";
  46.  
  47. for(int i = 0; i < num_poly; i++)
  48. {
  49. cout << "\nBinomial #" << i + 1 << endl;
  50. cout << "Coefficient: ";
  51. cin >> coeff[i];
  52. cout << "Number: ";
  53. cin >> lone[i];
  54. cout << endl;
  55. }
  56.  
  57. for(int i = 0; i < (num_poly - 1); i++) // This loop
  58. for(int j = 1; j >= (num_poly - 1); j += 2)
  59. {
  60. int t = 1;
  61. coeff_sqr[i].at(j - t) = coeff[i] * coeff[j];
  62. coeff_ans[i].at(j - t) = coeff[i] * lone[j];
  63. coeff_ans2[i].at(j - t) = lone[i] * coeff[j];
  64. lone_ans[i].at(j - t) = lone[i] * lone[j];
  65. t++;
  66. }
  67.  
  68. double fin_coeff_sqr = 0;
  69.  
  70. for(int i = 0; i < (num_poly - 1); i++)
  71. {
  72. double temp = coeff_sqr[i][0];
  73.  
  74. for(int j = 1; j < (num_poly - 1); j++)
  75. temp += coeff_sqr[i][j];
  76.  
  77. fin_coeff_sqr += temp;
  78. }
  79.  
  80. double fin_coeff_ans = 0;
  81.  
  82. for(int i = 0; i < (num_poly - 1); i++)
  83. {
  84. double temp = coeff_ans[i][0] + coeff_ans[i][0];
  85.  
  86. for(int j = 1; j < (num_poly - 1); j++)
  87. temp += coeff_ans[i][j] + coeff_ans2[i][j];
  88.  
  89. fin_coeff_ans += temp;
  90. }
  91.  
  92. double fin_lone = 0;
  93.  
  94. for(int i = 0; i < (num_poly - 1); i += 2)
  95. {
  96. double temp = lone_ans[i][0];
  97.  
  98. for(int j = 1; j < (num_poly - 1); j++)
  99. temp += lone_ans[i][j];
  100.  
  101. fin_lone += temp;
  102. }
  103.  
  104. cout << "\nThe answer is " << fin_coeff_sqr << "x^2+" << fin_coeff_ans << "x+" << fin_lone << endl;
  105. cout << "\nWould you like to go again? (y/n) ";
  106. cin >> again;
  107. }
  108.  
  109. cout << "\nGoodbye!\n\n";
  110. return 0;
  111. }

Any pointers or help would be highly appreciated! Thanks.
ShawnStovall is offline   Reply With Quote
Old Apr 13th, 2008, 2:28 PM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3 Jimbo is on a distinguished road
Re: Multi-dimensional vectors

The problem is with t in the inner loop. Because it's inside the loop, each time through, you'll reset it to 1, do your work, then increment it. The next time, you set it to 1 (note that j went up by 2), do your work, and increment... repeat as necessary. As it's coded, t will always be 1 when you do the calculations.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Apr 13th, 2008, 2:52 PM   #3
ShawnStovall
Call me Chuck
 
ShawnStovall's Avatar
 
Join Date: Aug 2007
Location: USA, MI
Posts: 37
Rep Power: 0 ShawnStovall is on a distinguished road
Send a message via AIM to ShawnStovall
Re: Multi-dimensional vectors

I was so distracted by the large computations that I overlooked the little ones! Thanks.

EDIT It fixed that silly little slip-up but I'm still getting the error. EDIT
ShawnStovall is offline   Reply With Quote
Old Apr 13th, 2008, 2:57 PM   #4
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3 Jimbo is on a distinguished road
Re: Multi-dimensional vectors

Can you post the new code? or even just the snippet
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Apr 13th, 2008, 2:58 PM   #5
ShawnStovall
Call me Chuck
 
ShawnStovall's Avatar
 
Join Date: Aug 2007
Location: USA, MI
Posts: 37
Rep Power: 0 ShawnStovall is on a distinguished road
Send a message via AIM to ShawnStovall
Re: Multi-dimensional vectors

Quote:
Originally Posted by Jimbo View Post
Can you post the new code?
Exactly the same as the old except that the declaration of t is now out of the loop.


Here:
C++ Syntax (Toggle Plain Text)
  1. int t = 1;
  2.  
  3. for(int i = 0; i < (num_poly - 1); i++)
  4. for(int j = 1; j >= (num_poly - 1); j += 2)
  5. {
  6. coeff_sqr[i].at(j - t) = coeff[i] * coeff[j];
  7. coeff_ans[i].at(j - t) = coeff[i] * lone[j];
  8. coeff_ans2[i].at(j - t) = lone[i] * coeff[j];
  9. lone_ans[i].at(j - t) = lone[i] * lone[j];
  10. t++;
  11. }
ShawnStovall is offline   Reply With Quote
Old Apr 13th, 2008, 3:30 PM   #6
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3 Jimbo is on a distinguished road
Re: Multi-dimensional vectors

I ran it again, with 2 binomials. The inner loop condition is true (num_poly - 1) == 1, so j >= num_poly - 1 will always be true. Then when you're indexing into coeff[j] you end up with an index greater than the size of the vector. Just thinking about it, if you ever had more than 2 binomials, the condition would be false and the loop would never run. You'll have to rework the logic on that one
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Apr 13th, 2008, 3:39 PM   #7
ShawnStovall
Call me Chuck
 
ShawnStovall's Avatar
 
Join Date: Aug 2007
Location: USA, MI
Posts: 37
Rep Power: 0 ShawnStovall is on a distinguished road
Send a message via AIM to ShawnStovall
Re: Multi-dimensional vectors

lol I'm missing a lot lately! It's been a few months since I last programed, but I didn't expect to be this rusty... off to try it again I guess!
ShawnStovall is offline   Reply With Quote
Old Apr 13th, 2008, 4:41 PM   #8
ShawnStovall
Call me Chuck
 
ShawnStovall's Avatar
 
Join Date: Aug 2007
Location: USA, MI
Posts: 37
Rep Power: 0 ShawnStovall is on a distinguished road
Send a message via AIM to ShawnStovall
Re: Multi-dimensional vectors

All my work, foiled. My friend told me a wrong way to multiply binomials, and now I have to start the initial algorithm from scratch again. Oh well, just more practice right?
ShawnStovall is offline   Reply With Quote
Old Apr 13th, 2008, 4:43 PM   #9
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3 Jimbo is on a distinguished road
Re: Multi-dimensional vectors

haha, nice perspective
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Apr 13th, 2008, 4:50 PM   #10
ShawnStovall
Call me Chuck
 
ShawnStovall's Avatar
 
Join Date: Aug 2007
Location: USA, MI
Posts: 37
Rep Power: 0 ShawnStovall is on a distinguished road
Send a message via AIM to ShawnStovall
Re: Multi-dimensional vectors

Well, I think I figured it out this time, now just a few hours of work... I hope.
ShawnStovall is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
reading from file, and vectors question jasonfrost C++ 3 Oct 3rd, 2007 10:02 AM
How do I read .mbm (Multi BitMap) files? moondog Other Programming Languages 19 Aug 16th, 2007 9:59 PM
multi dimensional array error veiga2 C++ 2 Nov 16th, 2006 4:05 AM
Arrays or Vectors? can342man C++ 2 Apr 20th, 2006 4:57 PM
Passing vectors as arguments Soulstorm C++ 6 Mar 18th, 2006 5:49 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:34 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC