Write a CC++ program (call it sum_of_digits) that takes an integer a.docx
1. Write a C/C++ program (call it sum_of_digits) that takes an
integer argument from the command line and outputs the sum of
all digits of the integer. For example, the result of the
invocation sum_of_digits 12345 should be 15. You have two
constraints: Constraint 1: Each process can do at most one
addition. If you want to calculate the sum of more than two
digits, you will have to fork off one or more processes in order
to do that, and each of the forked processes in turn can process
at most one more addition before forking off more processes to
help. Constraint 2: Each process can fork-off at most one other
process. After the call to program sum_of_digits with the
command line argument, the output should appear, and no more
processes should be running, in addition to the shell. Test your
program on any UNIX/LINUX machine, and turn in the source
code as part of the written assignment. (The source code should
be at most a few lines long.)
Solution
Ans; 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 <
2. 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; }
}