Vector help, subscript out of range, and basic tutoring

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yogi_bear_79

    Vector help, subscript out of range, and basic tutoring

    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(vec tor<int&vOne, vector<int&vTwo );
    string toStr(int &i);

    int main ()
    {
    vector<intvOne, 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(vOn e, vTwo);
    }

    int longAdditon(vec tor<int&vOne, vector<int&vTwo )
    {
    int sum, carryOver = 0;
    string results;

    for(size_t x = vOne.size()-1; x >= 0; x--){
    sum = vOne[x] + vTwo[x] + carryOver;
    if(sum >= 10){
    carryOver = sum - 9;
    sum = 0;
    }
    else{
    carryOver = 0;
    }
    results = results + toStr(sum);
    }
    string::reverse _iterator rit;
    for ( rit=results.rbe gin() ; rit < results.rend(); rit++ )
    cout << *rit;

    return 0;
    }

    string toStr(int &i)
    {
    //convert output double to char
    std::string s;
    std::stringstre am out;
    out << i;
    s = out.str();
    return s;
    }

  • Barry

    #2
    Re: Vector help, subscript out of range, and basic tutoring

    On 3ÔÂ16ÈÕ, ÉÏÎç9ʱ50·Ö, yogi_bear_79 <yogi_bear...@y ahoo.comwrote:
    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.
    size_t is "unsigned", so it's never negative, then "i >= 0" is always
    true.
    you can try

    <code>
    size_t n = 0;
    std::cout << (n -1) << std::endl;
    </code>

    to see what's going on.
    >
    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.
    >
    You can google "BigInterge r C++"



    Comment

    • Daniel T.

      #3
      Re: Vector help, subscript out of range, and basic tutoring

      yogi_bear_79 <yogi_bear_79@y ahoo.comwrote:
      Then I thought I would add (pad) amount of zeros to the begining of
      the vector that was short. Currently I am not sure how to add the
      zeros to the front of the vector.
      Switch to a deque and use push_front.

      Comment

      • yogi_bear_79

        #4
        Re: Vector help, subscript out of range, and basic tutoring

        On Mar 16, 12:42 pm, "Daniel T." <danie...@earth link.netwrote:
        yogi_bear_79 <yogi_bear...@y ahoo.comwrote:
        Then I thought I would add (pad) amount of zeros to the begining of
        the vector that was short. Currently I am not sure how to add the
        zeros to the front of the vector.
        >
        Switch to a deque and use push_front.
        In only the true dignity of rookie code I wrote this funciton to
        handle the diff between vector sizes

        vector<intpad(v ector<int&vSml, vector<int&vLrg )
        {
        size_t pad;

        pad = vLrg.size() - vSml.size();
        vector<intvTmp( pad,0);
        for (int i = 0; i < vSml.size(); i++)
        vTmp.push_back( vSml[i]);

        return vTmp;
        }

        OK, Additon & Subtraction are done.....Any hints on the algorithim for
        Multiplication & Subtraction??

        Comment

        • James Kanze

          #5
          Re: Vector help, subscript out of range, and basic tutoring

          On 16 mar, 17:42, "Daniel T." <danie...@earth link.netwrote:
          yogi_bear_79 <yogi_bear...@y ahoo.comwrote:
          Then I thought I would add (pad) amount of zeros to the
          begining of the vector that was short. Currently I am not
          sure how to add the zeros to the front of the vector.
          Switch to a deque and use push_front.
          Use a little endian representation and stick with push_back.

          In the end, he'll doubtlessly want to change the base from 10;
          10 makes very inefficient use of the memory. When he gets
          there, he'll have to use the standard conversion routines for
          input and output. Until then, nothing prevents him from
          inputting as he currently does, then using std::reverse to end
          up with a little endian representation.

          Not, of course, that using push_back or push_front is a
          particularly good idea. I'd go with insert( v.back(), sizeDiff,
          0 ). (Or with v.front() as first argument if you stick with big
          endian.)

          --
          James Kanze (GABI Software) email:james.kan ze@gmail.com
          Conseils en informatique orientée objet/
          Beratung in objektorientier ter Datenverarbeitu ng
          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

          Comment

          • James Kanze

            #6
            Re: Vector help, subscript out of range, and basic tutoring

            On 16 mar, 16:19, yogi_bear_79 <yogi_bear...@y ahoo.comwrote:
            I have the Addition/Subtraction pretty much figured out, just some
            cleaning yet. I have no idea how to implement the multiplication &
            divison in this style, I can't even imagine how to do it on paper
            this way!
            Knuth, vol.2 has a very good explination of this. That's where
            I'd start.

            --
            James Kanze (GABI Software) email:james.kan ze@gmail.com
            Conseils en informatique orientée objet/
            Beratung in objektorientier ter Datenverarbeitu ng
            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

            Comment

            Working...