Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLease follow the tips. I can\'t use anything I haven\'t learned yet. An array c

ID: 3536746 • Letter: P

Question

PLease follow the tips. I can't use anything I haven't learned yet.


An array can be used to store large integers one digit at a time. FOr example, the integer 1234 could be stored in the array a by setting a[0] to 1, a[1] to 2, a[2] to 3, and a[3] to 4. However, for this exercise you might find it mroe useful to store the digits backward.


In this exercise you will wrte a program that reads in two positive integers that are 20 or fewer digits in length and then outputs the sums of the two numbers. Your program witll read the digits as values of type char so that the number 1234 is read as the four characters '1', '2', '3', and '4'. After they are read into the program, the characters are changed to type int. THe digits will be read into a partically filled array, and you might find it useful to reverse the order of the elements in the array after the array is filled with data from the keyboard. (Whether or not you reverse the order of the elements in the array is up to you. It can be done either way, and each way has its advantages and disadvantages. ) Your program will perform the addition by implementing the usual paper and pencil addition algorithm. The result of the addition is stored in an array of size 20 and the result is then written to the screen. If the result of the additon is an integer with more than the maximum number of digits, that is more than 20, then your program should issue a message saying that it has encountered an integer overflow. You should be able to change the maximum length of the integers by changing only one globally defined constant. Include a loop that allows the user to continue to do more additions until the user says the program should end.



Hint: use 3 character arrays to store the first, second numbers and the sum.

Each number is entered one character at a time using cin.get(c); so a loop is necessary for obtaining a number. You should write an input function to do the job which in addition to filling the character array with digits, should also validate each character to be a digit. None digit character should terminate the input function and proper value be returned to the caller.

Use an addition function to carry out the addition of the two 'numbers' stored in character arrays. Just like manually additions, it must be carried out starting from the right-most digit. When the 2 single digit numbers adds up to exceed 10, a 1 is carried over to the digit on the left.

In the process of performing the addition, you may need to shift the elements in an array, so a separate shift function is recommended as it simplifies the code.
==========================================================
Algorithm from discussions in class:

/*
chapter 7.7
a program uses character arrays to carry out additions for
integer numbers (usefull for large numbers)

data structure:
     - 3 char arrays. 2 for the 2 numbers, and 1 for
     the sum.
     char n1[SIZE], n2[SIZE], sum[SIZE+1];
     - store digits in reverse order in arrays
     - initialize all 3 arrays to 0
     
Algorithm:
          1. input function that obtains 2 numbers from
          user.  
          -Stored in arrays n1 & n2 in reverse order (the
          1's digit in index 0, 10's digit in index 1, and
          so forth)
          - perform input validations (all chars are digits,
          and the max number of chars do not exceed the array
          size)
          - convert each char to an int digit by subtracting
          48.
          
          2. addition function adds the 2 numbers in n1 & n2,
          and stores the result in the sum array.
          Add each element in n1 and n2 starting from index 0.
          Use an int var carry_over (initialized to 0).
          e.g. sum[i] = n1[i] + n2[i] + carry_over;
               carry_over = sum[i] / 10;
               sum[i] = sum[i] % 10;
          
          3. output function
          display as follows:        
          index:         ........3210
          number 1: 57843897598734598
          number 2:+      87476457747
          ============================
          sum:      57843985075192355

          Use a for loop starting from the end of each
          array and loop to the beginning of the array
*/
  

Explanation / Answer

 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
#include <iostream>  using namespace std;  int main() {     cout << "Please enter the length of your number: ";     int y;     cin >> y;          cout << "Please enter your first number: ";     int x;     cin >> x;          cout << "Please enter your second number: ";     int q;     cin >> q;          int *p;     p = new int[y];          for (int i = 0; i < y; i++) {         p[i] = x % 10;         x = x / 10;     }          int *j;     j = new int[y];          for (int i = 0; i < y; i++) {         j[i] = q % 10;         q = q / 10;     }               for (int i = y - 1; i != -1; i--) {         cout << (j[i] + p[i]) % 10;         if ((j[i] + p[i]) % 10 != j[i] + p[i])             j[i - 1] = j[i - 1] + 1;     }     }


  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
#include <iostream>    using namespace std;    int main()  {      cout << "Please enter the length of your number: ";      int y;      cin >> y;            cout << "Please enter your first number: ";      int x;      cin >> x;            cout << "Please enter your second number: ";      int q;      cin >> q;            int *p;      p = new int[y];            for (int i = 0; i < y; i++) {          p[i] = x % 10;          x = x / 10;      }            int *j;      j = new int[y];            for (int i = 0; i < y; i++) {          j[i] = q % 10;          q = q / 10;      }                  for (int i = y - 1; i != -1; i--) {          cout << (j[i] + p[i]) % 10;          if ((j[i] + p[i]) % 10 != j[i] + p[i])              j[i - 1] = j[i - 1] + 1;      }       }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote