Function definition

Write a function that returns an integer and takes three character array parameters: left, right, and total. Left and right must be initialized by your phase 2 function. The function you define for this phase will fill total with the result. The integer that is returned is the “carry” result, NOT the sum! Since left and right do not change, you must make them constant; however, total does change, so it must NOT be declared constant.


Function Definition
Please read “Converting Characters to Integers”, as you will need to correctly convert characters to integers and vice-versa for this function definition.
Your function will need to add the arrays left and right element-by-element and store the result into your total array. You MUST use a for loop to accomplish the following:


1. Initialize a “carry” variable to integer 0
2. Start your for loop at element 0 and go up to (but not including) ARRAY_SIZE.
3. Add carry + righti + lefti (remember to convert to integers from characters)
4. If the result is >= 10, store just the ones digit to totali (remember to convert back and forth
between integers and characters) and set carry to 1
5. If the result is < 10, store the entire result (converted into a character) into totali and set
carry to 0
6. When your loop is finished, return the value of carry
Since your function returns the value of carry, we can determine if, through addition, you overflowed the total array. For example, 99999999999999999999 + 1 will give 21 numbers (1 with 20 zeroes), but your array only stores 20, so this overflows.
Please try writing the code yourself first. When you get stuck, post what you have, along with your questions.

When posting code, put it code tags: highlight the code in the edit window and then click the "<>" button on the right.

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
#include <iostream>
using namespace std;


int const ARRAY_SIZE = 20;
void print_array (char (&array)[ARRAY_SIZE]);
void storing_array (char (&array)[ARRAY_SIZE]);
int main()
{
	int i;
	char array [ARRAY_SIZE];
	cout << "Enter the number of digits: ";



	return 0;
}

//--------------------------end of main//

void print_array(char (&array)[ARRAY_SIZE])
{

	int i; //initializing i to be the first index without the character 0
	for (i=ARRAY_SIZE -1; i >=0; i--) {

		if (array[i] != '0') break;
	}

	for (; i >0; i--) { //printing the array in reverse
		cout << array[i];
	}

}
void storing_array(char (&array)[ARRAY_SIZE])
{
	int i;
	//promting the user to imput

	do {
		cout << "Enter the number of digits: ";
		for (i= ARRAY_SIZE-1; i >=0; i --) {
			cin >> array[i];
		}

	} while( );

}



This is what I have thus far. Do I add another function for this section?
I also have to write these error conditions. Would these go after my do while loop?

Error Condition 1: If the user types a number of digits less than or equal to 0 or a size that would exceed the size of your array, prompt the user again.
Error Condition 2: If the user types a character that is not a digit while typing the number, input the rest of the digits, and then prompt the user to enter the number again.
Prompts
Enter the number of digits:
Enter XX digits in reverse:
Replace XX with the actual number of digits the user specified.
Function Definition
As the user types in each digit as a character, you’ll need to put the first digit they type into the 0th element of the array, the second digit into the 1st element, and so forth. If the user is done typing digits and they have not filled the entire array, you must put ‘0’s into the remaining array elements.
Caution: Do NOT initialize your entire array to ‘0’ and then start changing digits. This is very inefficient and there are better ways. You must implement the “better way”.
Sample Interaction
Enter the number of digits: -6
Enter the number of digits: 6
Enter 6 digits in reverse: 6*4321
Enter 6 digits in reverse: 654321


@dhayden
Topic archived. No new replies allowed.