Very Long Integers Problem

Hi Everyone.. Im here again to ask help.. Im task to make a code/program.. here is the problem below...


Very Long Integers
Input File: longint.txt
Adding numbers in computer is quite an easy task. With the use of variables and
simple mathematical operations, addition may be performed at great speed. However
this action is not applicable in the case of very long integers. Very long integers
cannot be accommodated by data types such as INT or Long INT’s because of storage
limits. However this action may be done by typical math operations known to
elementary pupils… This operation is done by arranging these numbers one over the
other and adding individual digits from right to left. If the total per column is 2 digits,
the left digit is carried over to the next column.
Create a code in C++ that will read a file for a series of very long integers and display
the sum of these integers.
INPUT
The input will consist of at most 10 lines of text, each of which contains a single
VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length,
and will only contain digits (no VeryLongInteger will be negative).
The first integer in the input file indicates the number of long integers to add.
OUTPUT
Your program should output the sum of the VeryLongIntegers given in the input.
Sample Input
3
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
Sample Output
370370367037037036703703703670



Additional help.. can you put comments in each line of the codes. big thanks :)
Hi,
I am quite new to this forum, so I don't know if they have a particular policy for questions like this one... What I'm asking is: is it ok to do other people's homework? In other forums where I usually write, there are rules against this (bad) practice and an attempt from the OP is mandatory before anyone can give a "complete" solution.

Just asking...
Thanks in advance for any reply.
@minomic I don't think that doing the work for them will help them learn much. The best way to help them is by asking to see what the student has written so far and then going from there (like giving them tips on whether they're going in the right direction).
according to the admin
Don't post homework questions
Programmers are good at spotting homework questions; most of us have done them ourselves. Those questions are for you to work out, so that you will learn from the experience. It is OK to ask for hints, but not for entire solutions.
Ok, I agree. So, while we wait to see what he has done, I can give these tips:

- read the input as strings or, better, as vectors of int
- process the input from the end to the beginning
- write the result from the last number to the first (if you use a vector you have the insert method that you can use to insert an element in the first position of the vector). You can also write the result from the first number to the last and then reverse it.
Last edited on
@fifthSage, you should try that yourself.

you can start this way,
use vector as minomic said, (if you dont know vector yet, use dynamic array)

string s1, s2;
cin >> s1 >> s2; // the two numbers to add

int *p1 = new int[s1.length];
int *p2 = new int[s2.length];

fill the arrays by digits from the string.

int *result = new int[bigger of s1 and s2 +1];

start adding from last digit of both array. i.e. p1[last] + p2[last]
........

Last edited on
everyone im very sorry..

I'd be careful nxt time .. anyway tnx everyone :)
@fifthSage

Ok, but did you solve your problem?
not yet sir T.T
@anup30

sir, what do you mean by *pi??

im sorry sir, i'm a new student in programming

:(
That's a pointer to an integer and anup30 used it to declare an array of integer which is dynamically allocated (with the new).
if you didn't learn pointers yet, the program is too Advanced for you.
try learn basics C++ features first

here is a complete code example. but its lot fun to do yourself :)
http://ideamonk.blogspot.com/2008/10/adding-large-numbers-in-c.html

good luck.
This can be an idea if you want to avoid pointers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main() {

	string s1 = "12345678901234567890";
	string s2 = "98765432198765432100";

	vector<int> v1;
	vector<int> v2;

	for(unsigned int i=0; i<s1.length(); i++) {
		// put in the vector the char
		// converted to an int
		v1.push_back(s1[i]-'0');
	}
	for(unsigned int i=0; i<s2.length(); i++) {
		v2.push_back(s2[i]-'0');
	}
	
	// do your calculations...

}
@minomic, students learn pointer before vectors!
Yes, so it should be. But you never know... :)
Besides, I'm not using anything "advanced" (iterators or similar stuff). Just a vector seen as a resizable array. Once he has done the push_back thing, he can access any element with the [] operator, just like a normal array.

Otherwise I'm afraid OP will have to skip this exercise.
Otherwise I'm afraid OP will have to skip this exercise

"advanced" is a subjective term. to you iterators is advanced.
the OP don't know pointers. so you can definitely assume he don't know classes - so he don't know vectors.

the the right task for OP would be to learn the features which are involved in the solution. he may review the problem when learn the topics.

you know, i am trying to tell - right now OP can't but skip this.
Topic archived. No new replies allowed.