function that reads string, converts it to int array, flips number in array.


Hello I am trying to get a program going where I just need about three functions in order to complete the program. Right now I am strugggling with getting the first function going which is readBig, it should read a number and place each digit of the number into its own array element. There are comments at the bottom that explain a little further for the three functions. I mainly need help with the readBig funciton because I am not understanding how to store the array in reverse order but I am thankful for any help provided with the three functions, thanks.


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

#include <iostream>
using namespace std;
//This program will test three functions capable of reading, adding,
//and printing 100-digit numbers.

void readBig(int[]);
void printBig(int[]);
void addBig(int[], int[], int[]);

const int MAX_DIGITS = 100;

//There should be no changes made to the main program
int main()
{
  
int num1[MAX_DIGITS], num2[MAX_DIGITS], sum[MAX_DIGITS];
bool done = false;
char response;
while (not done)
{
cout << "Please enter a number up to "<<MAX_DIGITS<< " digits: ";
readBig(num1);
cout << "Please enter a number up to "<<MAX_DIGITS<< " digits: ";
readBig(num2);
addBig(num1, num2, sum);
printBig(num1);
cout << "\n+\n";
printBig(num2);
cout << "\n=\n";
printBig(sum);
cout << "\n";
cout <<"test again?";
cin>>response;
cin.ignore(900,'\n');
done = toupper(response)!='Y';
}
return 0;
}

//ReadBig will read a number as a string,
//It then converts each element of the string to an integer and stores it in an integer array.
//Finally, it reverses the elements of the array so that the ones digit is in element zero,
//the tens digit is in element 1, the hundreds digit is in element 2, etc.

//AddBig adds the corresponding digits of the first two arrays and stores the answer in the third.
//In a second loop, it performs the carry operation.

//PrintBig uses a while loop to skip leading zeros and then uses a for loop to print the number.
i have done a first function but not understanding the second function give more information about second function

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
void readBig(int arr1[])
{
	string* sarr;
	int arrSize = 0, *reverseArr = 0;
	cout << "\nEnter The Size Of The String Array : ";
	cin >> arrSize;
	sarr = new string[arrSize];
	reverseArr = new int[arrSize];
	cout << "\nEnter The String Elements : ";
	for (int i = 0; i < arrSize; i++)
		cin >> sarr[i];
	// converting string to int
	for (int i = 0; i < arrSize; i++)
	{
		arr1[i] = stoi(sarr[i]);
	}

	// reverse

	for (int i = arrSize-1, j = 0; i >= 0 && j < arrSize; i--, j++)
	{
		reverseArr[j] = arr1[i];
	}
	cout << endl;
	for (int i = 0; i < arrSize; i++)
	{
		cout << reverseArr[i];
	}
	cout << endl;
}
I am not understanding how to store the array in reverse order
1
2
3
4
5
6
7
    // simple loop to swap element at index
    // { 0, n - 1 }, { 1, n - 2 }, { 2, n - 3 }, ...
    // where n is the length of the string
    for( size_t i{}, j{ num_str.length() - 1 }; i < j; i++, j-- ) {
        int temp{ arr[i] };
        arr[i] = arr[j]; arr[j] = temp;
    }

num_str is a variable which holds the number inputted as a string, as suggested by its name.
If you want a Big int, I recommend you to write it in binary. Also why loop it again to reverse it, instead of writing it in reversed order at initialization?

1
2
3
4
int * arrayBigInt=new int[nrString.length()];

for(int i=0; i<nrString.length();++i)
  arrayBigInt[nrString.length()-i]=int(nrString[i]);


Or better use a vector

1
2
3
4
5
std::vector<int> BigInt;

for(int i=0;i<nrString.length();++i)
 BigInt.push_front( int(nrString[i]) );
The output (print) function will only print the number, with no leading zeros. The addition function will just add the two numbers and get a sum.

When I test the program with the readBig from @bird1234 I keep getting stoi undefined in this scope. It's a problem with my g++ terminal cygwin64 to use c++11, but the solutions are a bit vague.

Add string header
#include <string>
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <string>
#include <algorithm>

constexpr std::size_t MAX_DIGITS = 100 ;
using array_type = int[MAX_DIGITS] ;

static bool valid_string( const std::string& str )
{
    // a valid string has size not greater than MAX_DIGITS
    if( str.size() > MAX_DIGITS ) return false ;

    // and every character in the string is a digit
    for( char c : str ) if( !std::isdigit(c) ) return false ;

    return true ;
}

static int to_digit( char c ) // invariant: std::isdigit(c)
{ return c - '0' ; } // '7' - '0' == 7 etc.

// converts each element of the string to an integer and stores it in an integer array.
// Finally, it reverses the elements of the array so that the ones digit is in element zero,
// the tens digit is in element 1, the hundreds digit is in element 2, etc.
static bool to_array( const std::string& str, array_type& array ) // reference to array
{
    if( !valid_string(str) ) return false ;

    // http://en.cppreference.com/w/cpp/algorithm/fill_n
    std::fill_n( array, MAX_DIGITS, 0 ) ; // fill with all zeroes

    // copy from string in reverse order (use reverse_iterator),
    // transforming each character to the corresponding integer digit
    // http://en.cppreference.com/w/cpp/algorithm/transform
    std::transform( str.rbegin(), str.rend(), array, to_digit ) ;
    return true ;
}

static bool readBig( array_type& array )
{
    std::string str ;
    if( std::cin >> str ) return to_array( str, array ) ;
    else return false ;
}

static std::size_t num_digits( const array_type& array )
{
    // get to the most significant digit
    // uses a while loop (as specified) to skip leading zeros
    std::size_t pos = MAX_DIGITS - 1 ;
    while( array[pos] == 0 && pos > 0 ) --pos ;

    return pos + 1 ; // number of significant digits
}

// PrintBig uses a while loop to skip leading zeros
// and then uses a for loop to print the number.
void printBig( const array_type& array )
{
    const std::size_t ndigits = num_digits(array) ;

    // the digits are stored in reverse order; so print them in reverse
    // note: i < ndigits as i is an unsigned integral type
    for( std::size_t i = ndigits - 1 ; i < ndigits ; --i ) std::cout << array[i] ;
    std::cout << '\n' ;
}

int main()
{
    int array[MAX_DIGITS] ;

    std::cout << "Please enter a number up to " << MAX_DIGITS << " digits: ";

    if( readBig(array) ) printBig(array) ;
    else std::cout << "invalid input\n" ;
}

http://coliru.stacked-crooked.com/a/54734d51558b24d0
Hope there are no negative numbers.

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
#include <string>

//
//

void readBig( int *num )
{
   for ( int i = 0; i < MAX_DIGITS; i++ ) num[i] = 0;           // initialise to zero

   string s;
   getline( cin, s );

   int last = s.size() - 1;                                     // STILL TO ADD: CHECK last < MAX_DIGITS
   for ( int i = 0; i <= last; i++ )
   {
      int digit = s[last-i] - '0';                              // STILL TO ADD: check 0 <= digit <= 9
      num[i] = digit;
   }
}

void printBig( int *num )
{
   int last = MAX_DIGITS - 1;
   while ( num[last] == 0 && last > 0 ) last--;                 // strip trailing blanks
   for ( int i = 0; i <= last; i++ ) cout << num[last-i];       // write back in original order
   cout << endl;
}

Topic archived. No new replies allowed.