Hello all,
I am writing a program for an assignment that reads in two positive integers that are 15 digits in length and then outputs the addition and subtraction of the two numbers. Ive just started the assignment but I'm kind of confused on how I should even begin with this part...
The result of addition is stored in an integer array,say sum, of size 16(addition of two n digit numbers will be at most n+1 digits) and the result of subtraction is stored in an integer array, say sub, of size 15. Then print the result on screen. Don't print any leading zero in the result. But if the result is zero (i.e. all the elements in sum or sub array is zero) then print a single zero.
#include<iostream>
#include<cmath>
usingnamespace std;
class RLN
{
private:
int num1 [15]
int num2 [15]
int sum [16]
int sub [15]
public:
void get_input(int, int)
float do_add ()
float is_greater ()
float do_sub ()
int print_sum ()
intprint_sub ()
}
void RLN::get_input( int num1, int num2)
{
cout<< "Enter First Number:"<<endl;
cin<<num1<<endl;
cout<< "Enter Second Number" <<endl;
cin<< num2 <<endl;
}
If anyone could give me pointers in the right direction that would be most appreciated.
If you only dealing with 2 numbers it doesn't make much sense to use arrays, however the numbers you are reading in WILL be bigger than an int, and the way you are reading in it won't work. The only way to do what you are trying to achieve is to parse them as char[]. and then write methods that deal with simulating math, OR use int[] but your input will need to be broken up somewhat. I suggest perhaps reading in as char[] and then convert that to int[].
INT_MAX on 32bit systems is somewhere around 2147483647
when you +1 to this you get wrap around, which means it starts back around at it's lowest value(negative number) which happens to be around -2147483648
#include<iostream>
#include<cmath>
usingnamespace std;
// take note of all the added semi-colons.
class RLN
{
private:
int num1 [15];
int num2 [15];
int sum [16];
int sub [15];
public:
void get_input(int, int);
float do_add ();
float is_greater ();
float do_sub ();
int print_sum ();
int print_sub (); // you had intprint_sub (); space is needed between type and name.
};
// the above is the header declarations
// the below is the class implementations
void RLN::get_input( int num1, int num2)
{
cout << "Enter First Number:" << endl;
cin >> num1 >> endl;
cout << "Enter Second Number" << endl;
cin >> num2 >> endl;
}
float RLN::do_add ()
{
return 0; // a float;
}
float RLN::is_greater ()
{
return 0; // a float;
}
float RLN::do_sub ()
{
return 0; // a float;
}
int RLN::print_sum ()
{
return 0; // an int;
}
int RLN::print_sub ()
{
return 0; // an int;
}
the "header" file is declarations of functions, variables etc. and need a colon to close.
the "implementation" file (.cpp file) contains all the implementations of the previous declarations in the header.
I will give a demo on addition:
say we have these 2 array numbers, for example sake I will keep it small however you will need to process up to 15-16 elements, we can pretend the remaining 11 element are filled with zeros:
arr1 = { 1,0,5,9 };
arr2 = { 4,0,5,6 };
basically for the addition you will want to add each element starting at the end:
( 9 + 6 ) = 15, we only want to store single digits in the array so basically each addition can have a check is number if greater than 10, if true -10, and then add 1 to the previous element.
so the last element becomes 5. the second last element(previous) becomes n+n+1; +1 for the tenths we just subtracted.
( 5 + 5 ) + 1 = 11, again this element becomes 1, and we add 1 to the previous;
( 0 + 0 ) + 1 = 1, 1 is smaller than 10 so we do nothing;
( 1 + 4 ) = 5, 5 is smaller than 10 so we do nothing.
our addition of 1059 + 4056 becomes our temporary array of 5115.
I completely agree with you that is over my head. I have never had any experience with computer programming until I started this class. I've been trying really but I'm having a hard time understanding and thinking in "the language". But I need to complete this class to graduate so I must keep trying. Ive been working more on the program, thank you so much for you help I think that i was able to get things going in the right directions. This is what I have now:
Ok I will try and find some sample code for you to read in the array. But as for all the math, I'm going to leave that up to you because most of it is trivial and simply requires high school math, with a little programming logic thrown in. However as for reading the array in, There's no clear cut way to do it in C++ and what people suggestions I have read in other threads usually doesn't work as we want it to so I have to find a silly workaround. (oh snap java wins again).
so you may as well pretend that you have finished that part yourself.
In you code simply, try and finish off all the other methods, hard code 2 15element arrays so you have something to work with and test your methods. eg.
You can also ask your teacher for help describing how you can read in the array. Perhaps he will suggest the number won't be read in like a standard number and perhaps each digit will have a whitespace seperator, which will make our job a lot easier. What I mean by that is instead of reading in a number from keyboard like: 1193847471234612
we read in a number like 1 1 9 3 8 4 7 4 7 1 2 3 4 6 1 2
which cin breaks up very nicely and conveniently.
#include<iostream>
#include<string>
#include<cmath>
using std::cin;
using std::cout;
using std::endl;
intconst size=15;
class RLN
{
private:
int num1 [15];
int num2 [15];
int sum [16];
int sub [15];
int x [15];
int y [15];
public:
void get_input();
void do_add ();
void is_greater ();
void do_sub ();
void print_sum ();
void print_sub ();
};
void RLN::get_input()
{
int i;
for(i=0;i<size;i++)
cin>>num1[i];
for(i=0;i<size;i++)
cin>>num2[i];
}
void RLN::do_add ()
{
sum= num2+num1
return 0;
}
void RLN::is_greater ()
{
if (num1>num2)
{
x=num1
y=num2
}
else
{ x=num2
y=num1
}
return 0;
}
void RLN::do_sub ()
{
sub=x-y
return 0;
}
void RLN::print_sum ()
{
cout<<"The summation is:"<<endl;
cout<<sum<<endl:
return 0 ;
}
void RLN::print_sub ()
{
cout<<"The subtraction is:"<<endl;
cout<<sub<<endl;
return 0;
but when Im trying to determine how to use the if else statements because when I compile I get a message saying that you cannot assign arrays. When we're reading the array it has to be from a txt input file. i think we can read the array anyway we want, as long as were able to solve the problem...
The problem is your dealing with arrays, not numbers. you need to look at this from that perspective, that's the reason you can have up to 15 digit numbers. really if you write this class properly you should be able to have even bigger numbers like 124 digit numbers :P
look at my last post where I described how to the addition.