68.8k views
2 votes
PLEASE HELP... JAVA CODE

Assume you are coding a Decimal arithmetic machine.

So input a 22 digit numbr ( up to 22 digits) that represents someone's bank balance.

Assume that another data itesm is 22 digits and it is the amount aof a depost.

Add the two items together and replace the balance wit hthe result.

As an example assume the balance is 456.32

and the deposit is 123.62

then we add them together.

You do not need to deal with the decimal point

I EXPECT:
1) there is clear user ergonomics. tell the user to input the balance with the dollars and cents. But do not enter a decimal point.

2) I expect you to store each digit in a char. So the balance might look like

char balance[22]; // an array of characters.

3) cin >> balance ; // this should input the value.

4) Same stuff for the Deposit.

5) you might need to write a function to right justify the values.

6) Remember that you are dealing with ASCI character set here.

.

.

.

If you don't understand the assignment prompt, here's more explanation...

I envisioned a char balance [20];

and char deposit [20];

Input the two decimal values ( Both positive).

You may need to shift the values to the right to get them to line up.

Assume the right most two digits are the cents .

then add the deposit to the balance.

and show the result.

REMEMBER That the characters in the char array are ASCII Characters. So a zero is real a value of 48 and a 4 is really '0' + 4.

Please use const int num_Digits = 20; or something like that. I expect to see reasonable C++ Code.

DO NOT CALL SOIME DECIMAL ROUTINE YOU find in a library.

You do the work via for looks and artihmetic.

User Volk
by
7.6k points

1 Answer

3 votes

Below is a simple C++ program that fulfills the requirements of the assignment.

#include <iostream>

const int numDigits = 20;

// Function to right-justify a char array by shifting to the right

void rightJustify(char arr[], int length) {

for (int i = length - 1; i > 0; --i) {

arr[i] = arr[i - 1];

}

arr[0] = '0'; // Fill the leftmost digit with '0'

}

// Function to add two char arrays representing decimal numbers

void addDecimal(char balance[], char deposit[]) {

int carry = 0;

for (int i = numDigits - 1; i >= 0; --i) {

int digitSum = (balance[i] - '0') + (deposit[i] - '0') + carry;

balance[i] = (digitSum % 10) + '0';

carry = digitSum / 10

// Display the result

std::cout << "New Balance: " << balance << std::endl;

return 0;

User Alicia Tang
by
8.5k points