473,396 Members | 2,147 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,396 software developers and data experts.

add subtract large numbers C++

I'm looking to find the sums and differences of large numbers inputed by the user. all numbers must by positive and whole and the first number must be larger than the second. I'm having a couple problems:

1) I'm currently using arrays and saving the input into a string. I think that, when copying the data from the string to the array, I need to have the numbers at the very end of the array. I think I found a good way but its not working correctly.

2) This might be linked to #1 but I think my programming is wrong when I'm calculating the sum and differences for the two numbers. I get really long outputs that don't make sense.

Here is all the code:

Expand|Select|Wrap|Line Numbers
  1. #include<string>
  2. #include<iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     //Declare Variables
  8.     int fnum[512] = {0};
  9.     int snum[512] = {0};
  10.     int sum[512] = {0};
  11.     int diff[512] = {0};
  12.     string first;
  13.     string second;
  14.  
  15.  
  16.     //Prompt user for first integer
  17.     cout << "Please enter a positive whole number no larger than 512 digits long." << endl;
  18.     cin >> first;
  19.  
  20.     if (first[0] == '-') {
  21.         cout << "Your number must be positive.";
  22.         return -1;
  23.     }
  24.  
  25.     for (int i = 0; i < sizeof(first); i++) {
  26.         fnum[sizeof(first) - i] = first[i];
  27.     }
  28.  
  29.  
  30.     //Prompt user for second integer
  31.     cout << endl << "Please enter a positive whole number no larger than 512 digits long" << endl;
  32.     cout << "AND is smaller than the first number." << endl;
  33.     cin >> second;
  34.     if (second[0] == '-') {
  35.         cout << "Your number must be positive.";
  36.         return -1;
  37.     }
  38.  
  39.     for (int j = 0; j < sizeof(second); j++) {
  40.         snum[sizeof(second) - j] = second[j];
  41.     }
  42.  
  43.  
  44.     //If second integer is larger than first return error
  45.     if (first < second) {
  46.         cout << "The second integer must be smaller than the first integer.";
  47.         return -1;
  48.     }
  49.  
  50.     //TEST: make sure input copied into string correctly
  51.     cout << endl;
  52.     cout << "Your first number is " << first << "." << endl;
  53.     cout << "Your second number is " << second << "." << endl;
  54.     cout << endl;
  55.  
  56.     //TEST: make sure string copied into array correctly
  57.     cout << "first number is: ";
  58.     for(int n = 0; n < sizeof(first); n++) {
  59.         cout << fnum[n];
  60.     }
  61.  
  62.     cout << endl << "second number is: ";
  63.     for(int m = 0; m < sizeof(second); m++) {
  64.         cout << snum[m];
  65.     }
  66.  
  67.  
  68.     //Display sum of two integers
  69.     cout << endl;
  70.     /*for(int s = 0; s < 513; s++) {
  71.        if ((512 - s) == 0) {
  72.           sum[512 - s] = fnum[512 - s] + snum[512 - s];
  73.        }       
  74.        sum[512 - s] = fnum[512 - s] + snum[512 - s];
  75.        if (sum[512 - s] >= 10) {
  76.           sum[512 - s] = sum[512 - s] - 10;
  77.           sum[512 - s - 1] = sum[512 - s - 1] + 1;
  78.        }
  79.     }
  80.     cout << first << " + " << second << " = ";
  81.     for (int a = 0; a < 512; a++) {
  82.         cout << sum[a];
  83.     }*/
  84.  
  85.  
  86.     //Display difference of two integers
  87.     cout << endl;
  88.  
  89.     /*for(int d = 0; d < 513; d++) {
  90.        diff[512 - d] = fnum[512 - d] - snum[512 - d];
  91.        if (fnum[512 - d] > snum[512 - d]) {
  92.           fnum[512 - d - 1] = fnum[512 - d - 1] - 1;
  93.           fnum[512 - d] = fnum[512 - d] + 10;
  94.        }
  95.     }
  96.     cout << first << " - " << second << " = ";
  97.     for (int b = 0; b < 512; b++) {
  98.         cout << diff[b];
  99.     }
  100.     cout << endl;*/
  101.  
  102.  
  103.     //Pause and exit
  104.     system("PAUSE");
  105.     return 0;
  106. }
  107.  
Dec 17 '11 #1
3 8591
weaknessforcats
9,208 Expert Mod 8TB
Is this a homework problem?

If not, then you should use one of the big number libraries already written for C++. Large number operaions have already been coded up
Dec 18 '11 #2
alas, it is a homework assignment. However, it is extra credit.

I'm only allowed to use iostream, strings and cmath libraries.
Dec 19 '11 #3
weaknessforcats
9,208 Expert Mod 8TB
In that case I would use std::strings for your numbers instead of int arrays.

Then to add two numbers I would write a function that takes two strings by reference. Create two iterators and point them to string::rbegin so you can iterte from the right to he left.

You should be able to add chars until you get to string::rend.

The sum is returned as a string.
Dec 19 '11 #4

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

Similar topics

4
by: koray | last post by:
hi, i need to show large numbers seperated by commas. since i'm using variables from speedscript, i cannot know their values, since the user should enter them. how should i code to show these...
8
by: Sam Fisher | last post by:
Hi, I have a situation where I have to support a large number(not decimals), something of the order of 20 to 30 digits in the database(sql server 2000) I am not able to use LONG data type as it...
8
by: simpleman | last post by:
Hi, I have an assignment that requires me to subtract two very large unsigned integers using linked list.AFAIK I have to manipulate data as character. But I dont know how to get input into the...
3
by: Alex Vinokur | last post by:
Dann Corbit has implemented function int ilog2 (unsigned long) at http://groups.google.com/groups?selm=lkPa4.2165%24I41.1498%40client Is exist a similar C++-function for very large numbers,...
10
by: Tuvas | last post by:
I've been thinking about writing a program to generate the world's largest prime numbers, just for the fun of it. This would require being able to hold an 8000000 digit number into memory (25...
1
by: Jonathan | last post by:
My app is trying to multiply and add some very large numbers. Here is the line that I am am having problems with: Seconds = CDbl(PartB) * CDbl(16777216) Seconds is a double and PartB is a...
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...
3
by: CFonville | last post by:
I was wondering if there is any way to store large numbers in a variable? With this simple script: var bigpi = 1234567890123456789012345678901234567890123456789012345678901234567890;...
24
by: Frank Swarbrick | last post by:
We have a batch process that inserts large numbers (100,000 - 1,000,000) of records into a database each day. (DL/I database.) We're considering converting it to a DB2 table. Currently we have...
2
by: Todd.Branchflower | last post by:
Hey everyone, I am new to c++ and the group and would appreciate your help. I was doing a problem on projecteuler.net in which you had to find the largest factor of an extremely large number. ...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.