Re: Vector help, subscript out of range, and basic tutoring
On 16 mar, 03:31, "Daniel T." <danie...@earthlink.net> wrote:
On Mar 15, 9:50 pm, yogi_bear_79 <yogi_bear...@yahoo.com> wrote:
Distant learning student. My lab is to write a function to perform
the addition of large integers, with no limit to the number of digits.
(Also have to do a subtraction, division, and multiplication lab). It
is suggested to treat each number as a sequence. This is what I have
so far, this is rough code just to get it working:
1. I can't get the syntax correct on the 'for' statement in the
longAdditon function. No matter what I try I get a run-time error
subscript out of range.
2. Being very much a rookie, I am sure this isn't the prettiest code;
I am open to ideas, hints, etc. Bear in mind that I am basically self
taught here, and more of a systems admin than a programmer.
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
int longAdditon(vector<int> &vOne, vector<int> &vTwo);
string toStr(int &i);
int main ()
{
vector<int> vOne, vTwo;
string one, two;
int x = 0;
size_t r;
cin>>one;
r = one.length();
for (int x = 0; x < r; x++){
vOne.push_back(int(one[x] - '0'));
}
cin>>two;
r = two.length();
for (int x = 0; x < r; x++){
vTwo.push_back(int(two[x] - '0'));
}
longAdditon(vOne, vTwo);
}
int longAdditon(vector<int> &vOne, vector<int> &vTwo)
{
int sum, carryOver = 0;
string results;
for(size_t x = vOne.size()-1; x >= 0; x--){
Note, because 'x' is of type size_t, it will *never* be less
than 0. As such, x >= 0 will always be true.
The "standard" syntax for such a loop would be:
size_t x = vOne.size() ;
while ( x > 0 ) {
--x ;
// ...
}
Still, better solutions are possible here.
I suggest you look into vector::rbegin() and vector::rend().
Or use a little endian representation, with vector::begin() and
vector::end(). For a number of reasons, that's the way I'd go.
Even better would be to use std::transform to add each element
of the two vectors together, then go through the result and
take care of the carryover as a separate operation.
I'd be interested in seeing how you do that. Determining after
the fact that a carry should have taken place isn't that
obvious. I know how I'd do it, but if for some reason I had to
use such obscure tricks, I'd certainly explain it carefully in
comments. Much better to just be clean and correct (especially
as in this case, the clean solution will also be the fastest).
Lastly, you really should wrap the whole thing into a class...
class BigNum {
public:
explicit BigNum( const std::string& s );
std::string print() const;
};
BigNum add( const BigNum& left, const BigNum& right );
Later, when you learn about operator overloading, you can make
it so that someone can use the '+' to add two bigNums, like
this:
BigNum x, y;
// put values in them
BigNum z = x + y;
a BigNumber class is a great way to learn the language.
I'm not sure; it requires mastering a lot of semi-mathematical
issues that aren't necessarily everyone's cup of tea. (Most of
the problems in getting division correct and sufficently fast
are independent of the language.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34