not sure how to procced


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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include<cmath>
using namespace 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

see this post for some examples how to read in: http://cplusplus.com/forum/beginner/19994/

also with reading input using cin you need to use the >> operator.
TBH, this task is way to hard for your current level of competency.

line 21 needs a ';' after the '}'
whatevs I'll post what you wrote in syntax correct.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<iostream>
#include<cmath>
using namespace 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.
Last edited on
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.
Last edited on
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
#include<string>
using namespace std;


int const size=15;

class RLN
{
  private:
        int num1 [15];
        int num2 [15];
        int sum [16];
        int sub [15];

  public:
        void get_input(int, int);
        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 ()
{
   return 0; // a float;
}
void  RLN::is_greater ()
{
   return 0; // a float;
}
void  RLN::do_sub ()
{
   return 0; // a float;
}
void  RLN::print_sum ()
{
   return 0; // an int;
}
void  RLN::print_sub ()
{
   return 0; // an int;
}




and the main program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
#include<string>
#include "RLN.h"

using namespace std;



void RLN::RLN()
{
        int i;
        for (i=0;i<size;i++)
        {
                num1[i]=0;
                num2[i]=0;
                sum[i]=0;
                sub[i]=0;
        }
        sum[i]=0;

}



int main()
{
        class RLN ob1;
        ob1.get_input();
        return 0;
}


but Im still getting a lot of error messages. I asked my professor what that out put should look like and i should be able to get something like this
1
2
3
4
5
6
7
5 6 7 8 9 1 2 4 5 6 0 0 9 1 2
4 4 0 0 0 8 9 7 6 1 2 3 5 6 8

Summation is:
1       0       0       7       9       0       0       2       2       1       7       2       4       4       8       0
Subtraction is:
1       2       7       8       8       2       2       6       9       4       7       7       3       4       4




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.

1
2
number1[ 15 ] = { 1, 9, 2, 8, 3, 7, 4, 6, 5, 0 5, 4, 3, 2, 1 };
number2[ 15 ] = { 1, 2, 3, 4, 5, 0, 5, 6, 4, 7, 3, 8, 2, 9, 1 }; // or something like that 


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.
Last edited on
I did some more work here is the class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<iostream>
#include<string>
#include<cmath>
using std::cin;
using std::cout;
using std::endl;




int const 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.
Topic archived. No new replies allowed.