Join 149,907 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,207 people online right now. Registration is fast and FREE... Join Now!
christopher, a high school computer whiz kid, has been challenged by his cousin Thea, a math genius, to compute for integer using the four basic arithmetic operations using a computer program. however, thea, wanting his dear cousin to fail, deliberately increased the complexity on how it will be done. from her explanation, integers entered in the console by users should be temporarily stored into a variable and later copied to an array, digit by digit.
she then added that the arithmetic operation, except division, must be done digit by digit in the array, starting from its last element progressing to the first. just like the normal way.
It looks like you haven't attempted to finish this code skeleton. We aren't going to just write your functions for you - do you have a question on this?
It looks like you haven't attempted to finish this code skeleton. We aren't going to just write your functions for you - do you have a question on this?
How will i going to store the inputed numbers in the array?
i mean like this:
CODE
standard addition process addition process using using variables using arrays
Well, you can try using the modulus operator and division by 10.
As you work through an integer, the next least significant digit can be found by taking the remainder after division by 10 i.e. modulus 10. Store this remainder as the current digit. Then divide the original number by 10 and repeat.
To get the remainder after division with integers in C/C++, you use the % operator. So using the number 123456789 as an example:
CODE
int num=123456789; int array[10]; //empty array for storing digits
array[9]=num%10; //last element of array now contains the digit 9
num=num/10; //num is now equal to 12345678
Following the last division by ten step, you would then repeat until the variable num is equal to zero.
This post has been edited by jjhaag: 5 Jan, 2008 - 01:39 AM
Those functions don't accept integers, they accept arrays - if that's the format for the functiosn that you're supposed to use, you need to perform the conversion to arrays first, and then pass the arrays, not num1 and num2, to the functions when you make the calls from main().
As for actually performing the operations, you do it just like you would perform addition, subtraction, and multiplication by hand. Digit by digit, carrying to or borrowing from the next most significant digit when necessary.
Those functions don't accept integers, they accept arrays - if that's the format for the functiosn that you're supposed to use, you need to perform the conversion to arrays first, and then pass the arrays, not num1 and num2, to the functions when you make the calls from main().
As for actually performing the operations, you do it just like you would perform addition, subtraction, and multiplication by hand. Digit by digit, carrying to or borrowing from the next most significant digit when necessary.
Read back over the second post that I made in this thread. It shows you the first steps in doing so - and to get the entire number stored as an array, you just need to perform those same steps repeatedly until all the digits have been converted.
Well, you can try using the modulus operator and division by 10.
As you work through an integer, the next least significant digit can be found by taking the remainder after division by 10 i.e. modulus 10. Store this remainder as the current digit. Then divide the original number by 10 and repeat.
To get the remainder after division with integers in C/C++, you use the % operator. So using the number 123456789 as an example:
CODE
int num=123456789; int array[10]; //empty array for storing digits
array[9]=num%10; //last element of array now contains the digit 9
num=num/10; //num is now equal to 12345678
Following the last division by ten step, you would then repeat until the variable num is equal to zero.