0

my assignment is to do the following:

  1. Generate some rows of numeric digits. Use the random number generator.

  2. Ask the user for the number of rows and columns of digits. 2x4 would look like this...

    0655

    6476

  3. Treat each row of digits as a single number. Show the sum using addition. Example:

    0655

    6476

    7131 < sum of 0655 and 6476

Now I've gotten it to mostly work with the code below.

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

int main(int argc, char *argv[])
{
    int x;
    int y;
    int total = 0;
    int sum[100];
    int num;
    string val= "";
    srand((unsigned)time(NULL));
    cout << "\nHow many rows?\n" << endl;
    cin >> x;
    cout << "\nHow many columns?\n" << endl;
    cin >> y;
    int rows = x;
    int columns = y;
    for (int i=0; i<rows; i++) {
        cout << "    ";
        for (int j = 0; j < columns; j++) {
            int r = rand() % 10;
            sum[j]= r;
            if (j == columns - 1){
                for (int k = 0; k <= j; k++ ) {
                    val = val + to_string(sum[k]);
                }
                num = stoi(val);
                total += num;
                val = "";
            }
            cout << r;
        }
        cout << endl;
    }
    cout << "    "<< total << endl;
    cout << endl;
    cout << "program complete.\n" << endl;
    return 0;
}

However, whenever the y value from user input is 10 or higher I get the error:

terminate called after throwing an instance of 'std::out_of_range'
  what():  stoi

No errors from any x value from user input as far as I can tell.

Is this just a bad way of doing this? Should I scrap the code and try a different method?

  • Whatever is in `val` when `stoi` raises the exception does not fit inside an `int`. Suggestion: Catch the exception and print out `val` and then exit the program. Knowing the contents of `val` should put you on the road to solving the problem. – user4581301 Oct 19 '22 at 00:07
  • Another good alternative is to run the program in a debugger. The debugger should halt when it spots an uncaught exception and allow you to inspect the crash site, including the value of `val`. The debugger should be one of your go-to tools. It is second only to the optimizing compiler when it comes to improving programmer productivity. Sooner you get good at using them, the sooner you can reap the benefits and have extra time for other tasks. Or grabbing a drink with your friends. – user4581301 Oct 19 '22 at 00:12
  • 1
    Side note: `x` and `y` seem to exist only to get input from the user and then be copied into `row` and `column`. You might as well read directly into `row` and `column` and discard `x` and `y`. Code that doesn't exist has no bugs. – user4581301 Oct 19 '22 at 00:16
  • 1
    Side note: `int` has a fairly finite size, typically being able to hold values between -2,147,483,648 and 2,147,483,647. Count up the digits and you'll get a pretty big hint about why the program fails at 10. If you have to support rows longer than 10, you cannot use `int`. If you have to support up to 100, there is no commonly supported integer type and may have to abandon numeric types and do the math like a grade school student digit by digit in a string of digits, complete with carry. – user4581301 Oct 19 '22 at 00:25

1 Answers1

-1

The range of 32bit integer is 2e9+. Use 'long long' instead of int.
But wait, 'stoi()' will not work then.
You do not need to use string as well as stoi() function. I have changed the data type to long long. Have a look at this:

#include <iostream>
#include <cstdlib>
#include <string>
#include <time.h>
typedef long long ll;
using namespace std;

int main(int argc, char *argv[])
{
    ll x;
    ll y;
    ll total = 0;
    ll sum[100];
    ll num;
    srand((unsigned)time(NULL));
    cout << "\nHow many rows?\n" << endl;
    cin >> x;
    cout << "\nHow many columns?\n" << endl;
    cin >> y;
    ll rows = x;
    ll columns = y;
    for (int i=0; i<rows; i++) {
        cout << "    ";
        for (int j = 0; j < columns; j++) {
            ll r = rand() % 10;
            sum[j]= r;
            if (j == columns - 1){
                ll val= 0;
                for (int k = 0; k <= j; k++ ) {
                    val = val*10 + sum[k];
                }
                num = val;
                total += num;
            }
            cout << r;
        }
        cout << endl;
    }
    cout << "    "<< total << endl;
    cout << endl;
    cout << "program complete.\n" << endl;
    return 0;
}
Mahfuz
  • 21
  • 4
  • 1
    `sum[100]` suggests that 100 digits is a possibility and if `long long` is 64 bit, you can handle at most 20 digits. You'd need a 512 bit integer type, and those are in short supply these days. I suspect the problem is posed to force the student to write a simple "Big Integer" class. – user4581301 Oct 19 '22 at 17:23