Hello there!
This is the first time actually posting here, forgive me if I do something wrong. I'm a beginner C++ student, we just started with arrays and strings. :)
Anyhow, the problem I'm having is with a problem I was assigned the other day, involving strings and very long integers (numbers of up to 100 digits).
The problem is like this:
Problem [Very Long Integer]
Definition: A very long integer (VLI) is a nonnegative integer that has no more than 100 decimal digits and it is represented as a pair of two variables <size, arr>, where:
size – the current number of decimal digits of the VLI;
arr - an array of decimal digits (each decimal digit is stored in one array element).
Example:
const int MAXDIGITS = 100;
int arr[MAXDIGITS] = {1,2,3,4,5,6,7,8,9,0,9,8,7,6,5,4,3,2,1,0,9};
// The curent content of VLN is: 123456789098765432109
// There are 21 decimal digits in this VLI.
// The current number of digits is size = 21
A. List of Subproblems
Write a C++ function named inputVLI that inputs a VLI as a string of characters and transforms the string into the corresponding vli (Every character is converted into a decimal digit)
Prototype: void inputVLI(int vli[], int& size);
Write a C++ function named outputVLI that outputs a VLI.
Prototype: void outputVLI(int vli[], int size);
Write a C++ function named addVLI that calculates and “returns” the sum of two VLIs (vli3 = vli1 + vli2)
Prototype: void addVLI(int vli1[], int vli2[] int vli3[],
int size1, int size2, int& size3);
Write a C++ function named compVLI that compares two VLIs.
Prototype: int compVLI (int vli1[], int vli2[],
int size1, int size2);
The value returned by the addVLI function is:
-1, if vli1 < vli2
0, if vli1 == vli2
1, if vli1 > vli2
Write a main function that:
Declares three VLI: vli1, vli2 and vli3
Enters values for two VLIs : vli1 and vli2 (using inputVLI function)
Outputs vli1 and vli2 (using outputVLI function)
Calculates vli3 (vli3 = vli1 + vli2, using addVLI function)
Compares vli1 and vli3 (using compVLI function)
(Sorry for the length, but I thought the info might help)
I have the main skeleton of the program already made, but I'm hitting a major roadblock with this problem, specifically at the first part, the input. I'm really new to strings in general, so I'm unsure of how they work, when I input a string of numbers into an array, are they automatically split into decimal digits, or do I have to do so myself? And if so, ideas on how?
I don't need the problem solved, but some pointers on good ways to go about this would be nice! I've included the skeleton code if it helps understand. :)
I've bolded the part that I'm currently stuck on.
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 49 50 51 52 53 54 55 56 57 58
|
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
const int MAX_DIGITS = 100;
//prototypes
void inputVLI(int vli[], int& size);
void outputVLI(int vli[], int size);
void addVLI(int vli1[], int vli2[], int vli3[],
int size1, int size2, int& size3);
int compVLI (int vli1[], int vli2[],
int size1, int size2);
/*
Write a main function that:
Declares three VLI: vli1, vli2 and vli3
Enters values for two VLIs : vli1 and vli2 (using inputVLI function)
Outputs vli1 and vli2 (using outputVLI function)
Calculates vli3 (vli3 = vli1 + vli2, using addVLI function)
Compares vli1 and vli3 (using compVLI function)
*/
//main function
void main()
{
}
//Function definitions
void inputVLI(int vli[], int& size)
{
}
void outputVLI(int vli[], int size)
{
}
void addVLI(int vli1[], int vli2[], int vli3[],
int size1, int size2, int& size3)
{
}
int compVLI (int vli1[], int vli2[],
int size1, int size2)
{
}
|
Many Thanks!
-Drew