1

My programming assignment is to perform addition and subtraction operations with large integers up to 20 digits in length by using arrays. The instructions tell us to perform the arithmetic operations when digits are stored starting from the end of the array. For example:

string value1 = "";
cin >> value1; //input 1234
test[19] = 4;
test[18] = 3;
test[17] = 2;
test[16] = 1;

so it'll be easier to perform sum and difference operations. Any unused digits should be initialized to 0.

I started off by writing a for loop to read the last index of the test[] array to the first index. The variable, numDigits, keeps track of all the nonzero values in the array.

include<iostream>
include<string>
using namespace std;

int main()
{
        string value1 = "";
        int numDigits = 0;
        const int Max_Digits = 20;
        int test[Max_Digits] = {0}; 
        test[19] = 10;
        //cin >> value1;

    for (int i = Max_Digits - 1; i >= 0; i--)
    {
        if (test[i] != 0)
            numDigits++;
    }
    cout << "There are " << numDigits << " nonzero values."; //numDigits == 1
       /*cout << "Your number is: " << test[];*/
       return 0;
}

So if the user enters "1234" into the string variable, value1, I would want the program to convert the string into an array of digits and output it like 1234 (no commas or spaces) before I continue the assignment.

NotNameless
  • 41
  • 3
  • 9
  • 2
    Do you know how to iterate through a string? Do you know how to convert the character '4' into the number `4`? Do you know how to put an element into an array? – Beta Apr 02 '19 at 21:14
  • So what happens if the user goes crazy and enters more than 20 numbers? Shouldn't you be using a vector instead? – Constantinos Glynos Apr 02 '19 at 21:52
  • @ConstantinosGlynos The validation comes later. For now, I want to start with converting a string to an int array of digits. Also, my professor also hasn't lectured us on vectors yet and there was no mention of them as an option on the instructions. – NotNameless Apr 02 '19 at 22:18
  • @UchennaOnuigbo: Fair enough... Do you need the numbers to be backwards inserted in the array or forward? So, if the user inserts "1234", do you want the first element in the array to be 1, or 4? – Constantinos Glynos Apr 02 '19 at 22:43
  • @ConstantinosGlynos If the user inserts "1234", then the 19th index would be 4, then 3 for [18], 2 for [17] and lastly, 1 for [16]. The rest of the elements get initialize to 0 – NotNameless Apr 02 '19 at 22:52

1 Answers1

2

I wasn't sure whether you needed backwards insertion or forward, so the following demo does both. Pick your choice.

The idea is simple. For backwards insertion you need to create an iterator i which is initialized to Max_Digits-1 and decrements as the iterator that goes through the string increments. For forward insertion, you need to get the length of the string using std::string::length() and assign the value with Max_Digits-(strLen-i). The std::string::length() function will recalculate the length of the string every time which is called. It unnecessary to pay for that cost, so might as well store it in a variable.

#include <iostream>
#include <string>

int main()
{
    std::string value1 = "";

    std::cout<< "Enter a number up to 20 ints long >> ";
    std::cin >> value1;
    std::cout<<std::endl<< "Entered string: " << value1 <<std::endl;

    constexpr int Max_Digits = 20;

    int backwards[Max_Digits] = {0}; 
    int bck_itr = Max_Digits-1;
    for(int i=0; value1[i]!='\0'; ++i, --bck_itr)
        backwards[bck_itr] = value1[i] - '0';

    std::cout<< "Backwards ints: ";
    for (int i=0; i<Max_Digits; ++i)
        std::cout<< backwards[i] <<",";
    std::cout<<std::endl;

    int forward[Max_Digits] = {0};
    int strLen = value1.length();
    for(int i=0; value1[i]!='\0'; ++i)
        forward[Max_Digits-(strLen-i)] = value1[i] - '0';

    std::cout<< "Forward ints:   ";
    for (int i=0; i<Max_Digits; ++i)
        std::cout<< forward[i] <<",";
    std::cout<<std::endl;
}

For the input:

12345678

The result is:

Enter a number up to 20 ints long >> 
Entered string: 12345678
Backwards ints: 0,0,0,0,0,0,0,0,0,0,0,0,8,7,6,5,4,3,2,1,
Forward ints:   0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,

Example: https://rextester.com/MDTL51590

ps. if you don't know what the constexpr is, then in this case, simply consider it as a beefed up const.

Constantinos Glynos
  • 2,952
  • 2
  • 14
  • 32
  • 1
    Fun fact: `std::string::length` is guaranteed to have constant complexity since C++11. The recomputation will be cheap, but no reason to not cache it anyway. – user4581301 Apr 02 '19 at 23:10
  • @user4581301: Agreed! The only `size()` function I know of that does not recompute every time, is from the `std::array`. – Constantinos Glynos Apr 03 '19 at 08:53