Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Multi-dimensional vectors (http://www.programmingforums.org/showthread.php?t=15606)

ShawnStovall Apr 13th, 2008 10:57 AM

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. :P)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).

:

  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.

Jimbo Apr 13th, 2008 1:28 PM

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.

ShawnStovall Apr 13th, 2008 1:52 PM

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

Jimbo Apr 13th, 2008 1:57 PM

Re: Multi-dimensional vectors
 
Can you post the new code? or even just the snippet

ShawnStovall Apr 13th, 2008 1:58 PM

Re: Multi-dimensional vectors
 
Quote:

Originally Posted by Jimbo (Post 143838)
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:
:

  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.         }


Jimbo Apr 13th, 2008 2:30 PM

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 :icon_wink:

ShawnStovall Apr 13th, 2008 2:39 PM

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 Apr 13th, 2008 3:41 PM

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?

Jimbo Apr 13th, 2008 3:43 PM

Re: Multi-dimensional vectors
 
haha, nice perspective :D

ShawnStovall Apr 13th, 2008 3:50 PM

Re: Multi-dimensional vectors
 
Well, I think I figured it out this time, now just a few hours of work... I hope. :(


All times are GMT -5. The time now is 5:16 AM.

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