C++ Programming Code This

Hello can anyone help me figure out this C++ programs I don't understand how to do it can anyone program it for me and use comments?

Static arrays or static matrices must be used for this assignment; that is
memory for the matrix must be allocated at compile time; solutions that do not use static
allocation are unacceptable.

Write a program to add two large integers with up to 300 digits. One approach is to treat
each number as a list, each of whose elements is a block of digits of that number. For
example, the integer 179,534,672,198 might be stored with block[O] =198, block[l] =672,
block[2] = 534, block[3] = 179. Then add two integers (lists), element by element, carrying
from one element to the next when necessary
Last edited on
We won't do your homework for you. Show us what you've got so far.
plz i ask for help i am beginner I don't know alot of c++ i am learning i will fail my class it is due tonight :(
plz i ask for help i am beginner I don't know alot of c++ i am learning i will fail my class it is due tonight :(


Sounds like you deserve to fail.
The 'easy' way is to use the approach of having 2 arrays of 300 elements - with each digit stored in an element. Then a simple addition iteration over the arrays.

You first need a function that accepts an integer of up to 300 digits, check that it's valid and convert this into an array with each digit per element.

You need a carry variable - init to 0. Starting from the least significant digit of the input arrays, add the two digits and the carry. The result digit is this sum mod 10. The carry is this sum div 10 (integer in both cases). Store result in the result array. Then do the next digit et al. When finished, display the carry if not 0 and then display the result array starting from the most significant digit - ignoring 0 until a non-0 is displayed.

Easy-peasy!
Something like this:

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
#include <iostream>
#include <string>
#include <cctype>

constexpr size_t MaxDig {300};

bool getNum(unsigned char numb[MaxDig]) {
	std::string inp;
	size_t el {MaxDig};

	std::getline(std::cin, inp);

	for (auto d {inp.crbegin()}; el && d != inp.crend(); ++d)
		if (std::isdigit(static_cast<unsigned char>(*d)))
			numb[--el] = *d - '0';
		else
			return false;

	return el != 0;
}

void show(unsigned char numb[MaxDig]) {
	bool gotnz {};

	for (size_t e = 0; e < MaxDig; ++e)
		if (numb[e] != 0) {
			gotnz = true;
			std::cout << (int)numb[e];
		} else
			if (gotnz)
				std::cout << (int)numb[e];
}

int main()
{
	unsigned char n1[MaxDig] {};
	unsigned char n2[MaxDig] {};

	if (!getNum(n1))
		return (std::cout << "Bad number\n"), 1;

	if (!getNum(n2))
		return (std::cout << "Bad number\n"), 1;

	unsigned char res[MaxDig] {};
	unsigned char car {};

	for (size_t e = MaxDig; e > 0; --e) {
		const auto rest {n1[e - 1] + n2[e - 1] + car};

		car = static_cast<unsigned char>(rest / 10);
		res[e - 1] = rest % 10;
	}

	if (car)
		std::cout << (int)car;

	show(res);
}



 999999999999999999999999999999
7777777777777777777777777777777
8777777777777777777777777777776

Topic archived. No new replies allowed.