473,395 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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(vector<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(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--){
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.rbegin() ; rit < results.rend(); rit++ )
cout << *rit;

return 0;
}

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

Mar 16 '08 #1
5 3052
On 3ÔÂ16ÈÕ, ÉÏÎç9ʱ50·Ö, yogi_bear_79 <yogi_bear...@yahoo.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 "BigInterger C++"

Mar 16 '08 #2
yogi_bear_79 <yo**********@yahoo.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.
Mar 16 '08 #3
On Mar 16, 12:42*pm, "Daniel T." <danie...@earthlink.netwrote:
yogi_bear_79 <yogi_bear...@yahoo.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(vector<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??
Mar 16 '08 #4
On 16 mar, 17:42, "Daniel T." <danie...@earthlink.netwrote:
yogi_bear_79 <yogi_bear...@yahoo.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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Mar 16 '08 #5
On 16 mar, 16:19, yogi_bear_79 <yogi_bear...@yahoo.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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Mar 16 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: andy.rich | last post by:
I am getting the following error and I do not know why. Can anyone help? -------------------------------------------------------- this is what appears on the screen...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
8
by: VB Programmer | last post by:
I'm acutally using VB6, not VB.NET, but I couldn't find the newsgroup for version 6.... I need help for something that should be simple. I keep getting a "subscript out of range" error and...
51
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc...
6
by: josh | last post by:
Hi I've a dubt! when we have overloaded functions the compiler chooses the right being based on the argument lists...but when we have two subscript overloaded functions it resolves them being...
6
by: Andy | last post by:
Hi all, I started developing a little app on my Mac using XCode some month ago. The app is running fine on my mac like a sharm. Now I am nearly ready and yesterday I moved the whole source code...
2
by: flavourofbru | last post by:
Hi all, I am declaring 2 vectors as vector<int> abc; vector<int> xyz; i am passing the vector "xyz" as an argument to a function and the return value is stored in vector "abc". But it shows a...
4
by: Han | last post by:
when I exe my project in vs.net2005,I got the error following: Debug Assertion Failed! Program:........ File:c:\program files\microsoft visual studio 8\vc\include\vector Line:756 ...
1
by: godhulirbalaka | last post by:
Dear all, I have a problem with MSFlexGrid. I have few field and one MSFlexGrid So i want if i doubleclick any row then the datas of that row distribute to those field I already wrote the command...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.