I look at it like this:
Write a main function that:
1. Declares three VLI: vli1, vli2 and vli3
2. Enters values for two VLIs : vli1 and vli2 (using inputVLI function)
3. Outputs vli1 and vli2 (using outputVLI function)
4. etc. etc. |
Now it seems to me that you are trying to start with step 2.
That looks like the reason for some of the confusion.
To begin, you are supposed to declare three VLIs. Now since the array and size values are tied together in their meaning and purpose, I'd be tempted to use a structure to hold them.
On the other hand, the function prototypes refer to the two parts separately, so it doesn't matter very much which way you do it.
At any rate, you will start with something like this (but you don't have to follow the names I use).
At the global scope (outside of any function)
const int MAXDIGITS = 100;
In function main():
1 2
|
int VLIoneArray[MAXDIGITS];
int VLIoneSize = 0;
|
The reason for placing the constant at the global level is that the other functions may need to use its value.
Next call the function
inputVLI(VLIoneArray, VLIoneSize);
The code for that function is the thing which now needs to be written. Now the purpose of the function is to:
a) ask the user to input the number
b) get the input as a string of characters
c) check that the string is no longer than MAXDIGITS. If it's longer you could simply truncate the string, but it might be better to issue a warning message and ask the user to try again.
d) use a loop to step through the input string one character at a time. Convert the character to the corresponding (integer) decimal digit. Store that digit in the VLI array at the corresponding index position. (each char in the string corresponds to one element in the array).
e) when you get to the end of the input string, store the count of the number of digits which were stored, into the VLI size value.
Since the
array is passed to the function as an address, and the
size is passed by reference, the values will directly affect the input parameters. Thus the function can now simply end.
If you follow this approach, of breaking down each task into smaller subtasks, you should be able to write the code.
When you are ready to write the code for the next function, before writing any code, write down the list of steps that the function will need to follow, before you start.
Coding is actually the relatively easy part, it's just translating human words into a computer-speak version. The important part is the design stage. When doing this, you will be thinking in terms of what the computer will need to do, and how, but you will express this in ordinary English, not in code.
One thing which I've not addressed is whether to process the input string from left to right or from right to left. Normally when we read, we proceed from left to right. However, in the case of a number, it may be better to proceed from the right-hand end. The reason for that is to start with the units (ones), then the tens, then hundreds, and so on.