I’m trying to make a program that takes two big integers, puts them into a string array and then adds the values and reads prints it out in the correct order, but I’m having a fair amount of issues. For instance one of my tests 4321+765 is meant to return 5086 but says “Addition not possible” and I’m a bit confused as to how. It’s been awhile since I’ve done much algo stuff so I’m trying to get the hang of it better. Here is my code:
#include <string>
#include <iostream>
using std::string, std::cout, std::endl; //Include for debug outputs
#include "bigInt.h"
Any advice would be much appreciated! It seems I'm messing up somewhere with the logic for the carry but I can't figure out how...
char character(int x) {
// Converts integer 0-9 to corresponding character '0-9'
return '0' + x;
}
int number(char ch) {
// Converts character '0-9' to corresponding integer 0-9 by subtracting ASCII value of 0
if (ch >= '0' && ch <= '9') {
return ch - '0';
}
return -1;
}
void reverseArray(int arr[], int n) {
// Swap elements up to the middle of the array to reverse the order
for (int i = 0; i < n / 2; ++i) {
int temp = arr[i];
arr[i] = arr[n - i - 1];
arr[n - i - 1] = temp;
}
}
bool add(const int A[], int nA, const int B[], int nB, int C[], int& nC) {
// Check if there is enough space in the result array
int maxLen = nA > nB ? nA : nB;
if (maxLen + 5 > nC) {
return false; // Insufficient space
}
// Initialize carry and index variables
int carry = 0;
int k = 0;
for(int i=0;i<maxLen;i++){
}
// Perform addition digit by digit
for (int i = 0; i < maxLen || carry > 0; ++i) {
int digitA = i < nA ? A[i] : 0;
int digitB = i < nB ? B[i] : 0;
int sum = digitA + digitB + carry;
carry = sum / 10; // Determine the carry for the next iteration
C[k] = sum % 10; // Store the result digit
k++;
}
// Update the length of the result array
nC = k;
reverseArray(C,nC);
return true; // Addition successful
}
bool fromStringToArray(const string& str, int A[], int size, int& n) {
n = str.length();
cout<<"n: "<<endl;
if (n > size || n == 0) {
return false;
}
// Check for non-digit characters and store in reverse order
for (int i = 0; i < n; i++) {
if (!isdigit(str[i])) {
return false;
}
A[n - 1 - i] = number(str[i]);
cout<<"Testing index "<<n-1-i<<"has value "<<A[n-1-i]<<endl; // Store digits in reverse order
}
// Handle the case where the number is zero
if (n == 1 && A[0] == 0) {
n = 1; // Correct representation of zero
}
return true;
}
string addBigInt(const string& A, const string& B) {
// Check if valid big integer and make placeholder arrays of size 100 to store the values
int nA, nB, nC;
int A_arr[100], B_arr[100];
if (!fromStringToArray(A, A_arr, 100, nA) || !fromStringToArray(B, B_arr, 100, nB)) {
// Call stringToArray and check for errors
return "Error: Invalid big integer";
}
int C[100];
if (!add(A_arr, nA, B_arr, nB, C, nC)) {
// Call add function and check for errors
return "Error: Addition overflow";
}
// Reverse the result array
reverseArray(C, nC);
string result;
for (int i = 0; i < nC; ++i) {
// Convert array back to string for formatting
cout<<"Test C "<<character(C[i])<<" "<<endl;
result += character(C[i]);
}
return result;
}
and my test :
#include <iostream>
using std::cout, std::cin, std::endl;
#include <string>
using std::string;
#include "bigInt.h"
int main() {
const int size = 100;
int A_arr[size];
int B_arr[size];
int C_arr[size];
int nA, nB, nC;
while (true) {
string strA, strB;
cout << "Enter the first big integer: ";
cin >> strA;
cout << "Enter the second big integer: ";
cin >> strB;
if (!fromStringToArray(strA, A_arr, size, nA) || !fromStringToArray(strB, B_arr, size, nB)) {
// Calls fromStringtoArray and checks for valid input
cout << "Invalid input. Please enter positive integers or zero." << endl;
continue; // Ask for input again
}
if (!add(A_arr, nA, B_arr, nB, C_arr, nC)) {
// Call sum function and return error if addition not possible
cout << "Error, addition not possible." << endl;
continue; // Ask for input again
}
// Output the formatted result
cout << strA << endl;
cout << "+" << endl;
cout << strB << endl;
cout << "---------" << endl;
for (int i = 0; i < nC; ++i) {
cout << C_arr[i];
}
cout << endl;
// Ask user if they want to continue
cout << "Do you want to add more numbers? (y/n): ";
char cont;
cin >> cont;
if (cont != 'y' && cont != 'Y') {
break;
}
}
return 0;
}
There seems to be something I messed up with the logic because I get errors that addition isn’t possible for many number values that need a carry. This is odd to me since int C is 100 length so I’m not sure why it’s getting this error. The tests show it to have failed conversions in fromStringToArray with leading 0’s, multiple digits, and exact size limit. What do I need to change? We’re instructed not to use or or any other libraries for this one as well. Additionally, if I wanted this to be able to take bases from 10-16 and do conversions based on that, what would be the right path to start on?
Zarina L is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1