Question

In: Computer Science

The program shall take two integer arrays as input. Each array represents a non-negative number. Each...

The program shall take two integer arrays as input. Each array represents a non-negative number. Each element in the array is one digit however when all digits in the array are combined a number is formed. See example below:

int Array1 = {4,5,6,7} represents number 7654
int Array2 = {2,3,4,5} represents number 5432
You will add the two numbers i.e., 7654 + 5432 = 13086 and store it in a new array similar to how the numbers were stored earlier i.e., {6,8,0,3,1}. The output of your program will be this array for given input arrays.

Your program should work with any size arrays and both arrays may not be the same size. If the result is larger than an integer (4 bytes) your program will return an empty array. Code must be written C++.

Solutions

Expert Solution

CODE:

#include <iostream>

using namespace std;

int main(){
int array1[] = {4,5,6,7};
int array2[] = {2,3,4,5};
int a=0,b=0,c=0,length1=0,length2;
int j=0;
//getting the lengths of the two arrays
length1 = *(&array1+1) - array1;
length2 = *(&array2+1) - array2;
//getting the first number
for(int i=length1-1;i>=0;i--){
a = 10*a + array1[i];
}
//getting the second number
for(int i = length2 - 1;i>=0;i--){
b = 10*b + array2[i];
}
//the addition of the two numbers is c
c = a+b;
int p = c;
int length = 0;
//getting the length of c
while(p!=0){
length += 1;
p = p/10;
}
p = c;
//storing the numbers in the array with the units digit as the first element in the array
int *array3 = new int[length];
for(int i = 0;i<length;i++){
array3[i] = p%10;
p = p/10;
}
//displaying the output array if the size of the array
if(sizeof(array3)<=4&&sizeof(c)<=4){
cout<<"The addition of the two array numbers "<<a<<" and "<<b<<" give the array: "<<endl;
for(int i = 0;i<length;i++){
cout<<array3[i]<<" ";
}
cout<<endl;
}else{
cout<<"The addition of the two array numbers "<<a<<" and "<<b<<" gives a result which is larger than an integer"<<endl;
}
return 0;
}
______________________________________

CODE IMAGES:

__________________________________________________

OUTPUT:

________________________________________________

Feel free ti ask any questions in the comments section
Thank You!


Related Solutions

Input: An array of non-negative integers, where each element in the array represents your maximum jump...
Input: An array of non-negative integers, where each element in the array represents your maximum jump length at that position. Output: A boolean value if you are able to reach the last index starting if you start at the first spot in the array. [Please write a recursion function!] Example 1: Input: [2,4,1,2,4,1] Output: True (Ex. 0 to 1 to 5 or 0 to 2 to 3 to 5) Example 2: Input: [3,2,1,0,4] Output: false (You will always arrive at,...
Input: An array of non-negative integers, where each element in the array represents your maximum jump...
Input: An array of non-negative integers, where each element in the array represents your maximum jump length at that position. Output: A boolean value if you are able to reach the last index starting if you start at the first spot in the array. Example 1: Input: [2,4,1,2,4,1] Output: True (Ex. 0 to 1 to 5 or 0 to 2 to 3 to 5) Example 2: Input: [3,2,1,0,4] Output: false (You will always arrive at, and get stuck at, index...
. As input you are given two arrays: an array of numbers ? and an array...
. As input you are given two arrays: an array of numbers ? and an array ? of queries where each query is a target number. The array ? is unsorted and may contain duplicates. Your goal is, for each query ? in the array ?, count the number of pairs in the array ? that sums up to ?; that is, the number of distinct pairs of indices [?, ?], with ? < ?, such that ?[?] + ?[?]...
Write a program factor that prints the prime factors of a specified non-negative integer. The integer...
Write a program factor that prints the prime factors of a specified non-negative integer. The integer will be provided as a command-line argument. You may assume that the integer will be greater than 1 and less than or equal to the largest signed integer on your platform (that is, you may use an int).
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
ite a C program that prompts for and reads in a non-negative integer from the keyboard....
ite a C program that prompts for and reads in a non-negative integer from the keyboard. Read it into an int variable x. Then display the 32 bits in x from the lsb to the msb (so the display will show the value in x in binary with the bits in reverse order). For example, if you input 6, then your program displays 00000000000000000000000000000110 Use the algorithm given in class (repeatedly divide by 2). Use the / and % operators....
Consider the following program specification: Input: An integer n > 0 and an array A[0..(n –...
Consider the following program specification: Input: An integer n > 0 and an array A[0..(n – 1)] of n integers. Output: The largest index b such that A[b] is the largest value in A[0..(n – 1)]. For example, if n = 10 and A = [ 4, 8, 1, 3, 8, 5, 4, 7, 1, 2 ] (so A[0] = 4, A[1] = 8, etc.), then the program would return 4, since the largest value in A is 8 and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT