473,394 Members | 1,715 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,394 software developers and data experts.

Adding large integers

52
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 5953
Laharl
849 Expert 512MB
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
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 Expert 512MB
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
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
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...
10
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
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
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...
4
by: KC | last post by:
How can I represent a 22-digit integer in C? float is not large enough. Thanks!
17
by: Sri | last post by:
How do you add an n-bit number in C? Regards, Sri
42
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...
22
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...
18
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. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
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...

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.