473,549 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Mar 16 '08 #1
5 3058
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++"

Mar 16 '08 #2
yogi_bear_79 <yo**********@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.
Mar 16 '08 #3
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??
Mar 16 '08 #4
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:ja******* **@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
Mar 16 '08 #5
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:ja******* **@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
Mar 16 '08 #6

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

Similar topics

20
5617
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 -------------------------------------------------------- 2Sports 'trouble shooting illustrated 'trouble shooting Newsstand 'trouble...
17
3335
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 this uovec at the moment (for Unit-Offset VECtor). I want the class to respond correctly to all usage of STL containers and algorithms so that it is...
8
8491
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 don't know why. Briefly.... I have an array of Variants dimmed as this: Global gMyConnection() As Variant I redim it later like this:
51
23650
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 10018-clc.c: In function `main': 10018-clc.c:22: warning: array subscript has type `char' I don't like warnings ... or casts.
6
2721
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 based on the const type. Infact if I use the Array on the left side i.e like a1 = 111 then it uses the first while if I use cout << a1 it uses the...
6
11069
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 to a Windows PC and build it in MS VC++. Now I got a lot of "vector subscript out of range" assertion errors and I don't know why because on my Mac...
2
3003
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 run-time error specifying that "Vector Subscript out of range". what should I do in order to get out of this run time error.
4
7080
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 Expression:vector subscript out of range.
1
3098
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 Private Sub MFG_Item_dbclick() with mfg_item .Row=.rowsel txt_Name.text=mfg_Item.textmatrix(row, 1) txt_Id.text=mfg_Item.textmatrix(row, 2)
0
7979
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7497
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7826
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6065
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5385
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5107
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3493
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1074
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
781
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.