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.
@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).
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.
int main() {
string s1 = "12345678901234567890";
string s2 = "98765432198765432100";
vector<int> v1;
vector<int> v2;
for(unsignedint i=0; i<s1.length(); i++) {
// put in the vector the char
// converted to an int
v1.push_back(s1[i]-'0');
}
for(unsignedint i=0; i<s2.length(); i++) {
v2.push_back(s2[i]-'0');
}
// do your calculations...
}
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.