473,785 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding large integers

52 New Member
hello everyone,

Well I tried to solve this one on my own but it seems i need your help again :\

I'm trying to write a program to add or subtract two large integers... easy enough..

Where i run into problems is working with pointers. Here's where i'm at right now.

I have a function that takes in the integers as strings and puts it into a struct

Expand|Select|Wrap|Line Numbers
  1. //struct declaration
  2. struct integer {
  3.     int* digits;
  4.     int size;
  5. };
  6. //function declaration
  7. struct integer* read_integer(char* stringInt);
  8.  
so far this works fairly well... except when i try to print the digits as integers it gives me the ascii equivilent number (like 54 for 1 or something)

then i have a function to add the two large integers, and this is where i'm currently stuck

Expand|Select|Wrap|Line Numbers
  1. // function code
  2. struct integer* add(struct integer *p, struct integer *q){
  3.        int *first, *second, carry, i, length;
  4.        struct integer* final;
  5.        carry = 0;
  6.        if(p[0].size > q[0].size)
  7.           length = p[0].size;
  8.        else
  9.           length = q[0].size;
  10.        for(i=0;i<length;i++){
  11.           if(p[0].digits[i] == "\0"){
  12.              //[WARNING] comparison between pointer and integer
  13.              first = 0;
  14.           else
  15.              *first = p[0].digits[i];
  16.           if(q[0].digits[i] == "\0")
  17.              //[WARNING] comparison between pointer and integer
  18.              second = 0;
  19.           else
  20.              *second = q[0].digits[i];
  21.           final[0].digits[i] = *first + *second + carry;
  22.           if(final[0].digits[i] >= 10){
  23.              final[0].digits[i] = final[0].digits[i] - 10;
  24.              carry = 1;
  25.           }
  26.           else
  27.              carry = 0;
  28.           }
  29.  
  30. }
  31.  
compiling this gives me 2 warning on the lines above comments in code above.
running it will give an access violation and crash the program.

I'm not sure where i'm going wrong but i'm sure it has something to do with my use of pointers.

Any help will be greatly appreciated!
May 27 '08 #1
4 5970
Laharl
849 Recognized Expert Contributor
Using double quotes there means you're comparing it to a string literal. Use single quotes for the character literal you want.
May 27 '08 #2
Shisou
52 New Member
ahhhh, that got rid of those pesky warnings :) thank you...

however i still get an access violation, and i believe it is at the following line

Expand|Select|Wrap|Line Numbers
  1. *first = p[0].digits[i];
  2.  
atleast when i tried to step through this line in my debugger the error popped up :\
May 27 '08 #3
oler1s
671 Recognized Expert Contributor
Expand|Select|Wrap|Line Numbers
  1. int *first
Why is first a pointer to an int? If you have described a valid reason for it to be a pointer, where does it point?
May 28 '08 #4
Shisou
52 New Member
I didn't really have a reason for first and second to be pointers... except that it was the only way to get some kind of functionality out of the program... but now that i have the strings converted to integers I think i should be able to use just a regular int.

I'm going to keep working this part alone and see where i can get, if I need anything else I'll be sure to post here again :)

Thanks again!
May 28 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2204
by: DJTB | last post by:
Hi, I'm trying to manually parse a dataset stored in a file. The data should be converted into Python objects. Here is an example of a single line of a (small) dataset: 3 13 17 19 -626177023 -1688330994 -834622062 -409108332 297174549 955187488 589884464 -1547848504 857311165 585616830 -749910209 194940864 -1102778558 -1282985276 -1220931512 792256075 -340699912 1496177106 1760327384
10
2691
by: sp0 | last post by:
Is there a reason why to make mix numbers improper when adding? It seems when subtracting and adding, adding a subtracting the whole numbers and fraction parts should be sufficient? what'ch think
6
2875
by: Michael Zielinski | last post by:
I was wondering if anybody knows of a package that does arbitrarily large integers that I can get preferably for free.
5
3261
by: Edith Gross | last post by:
I'd like to add to unsigned integers and find out whether a carry occurs. Can I do that in a simple and fast way? A similar problem is subtracting with borrow and the multiplication of two unsigned integers where the result may not fit? Can anybody give me a hint of how to wolve these problems? TIA,
4
1656
by: KC | last post by:
How can I represent a 22-digit integer in C? float is not large enough. Thanks!
17
12794
by: Sri | last post by:
How do you add an n-bit number in C? Regards, Sri
42
3377
by: yong | last post by:
Hi all I have an large integer in this format x1*256^5 + x2*256^4 + x3*256^3 + x4*256^2 + x5*256 + x6 now I must convert it to this format y1*900^4 + y2*900^3 + y3*900^2 + y4*900 + y5 x1-x5 is given.I must get y1-y4 from it. How can I do this on my 32bit PC?
22
4503
by: Frinton | last post by:
Hi, I am trying to do some calculations on large numbers (ie 7,768,489,957,892,578,474,792,094 / 12,280) and no matter what I do it doesn't get it quite right. Its always somewhere between 10 and and 5000 out :( I have a suspition is could be down to one of the number functions I am using along the way but im not sure.
18
5836
by: johnathan | last post by:
I need help in writing the addition , subtraction etc. method. eample the add method must add large intergers, say 2500 + 69587562. each number is entered in the subscript individually. this is what i have done so far.I created the class,constructors public class Example
0
9481
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10336
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8978
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.