如何从动态数组C ++中找到最低列

时间:2018-04-15 06:45:25

标签: arrays c++11 dynamic

我正在努力寻找表现最差的销售人员。我一直收到异常:Project4.exe中0x0105317B处的未处理异常:0xC0000005:访问冲突读取位置0x56F48BD5。 当它达到LowPerformance = sales [Products] [0]时出现; 我已经尝试定义行(即LowPerformance = sales [10] [0];)但我得到了同样的错误。关于如何通过此例外的任何建议?

    int main()
    {
    int Products, SalesPerson;
    cout << "Please enter the number of proudcts (1-10): " << endl; cin >> Products;
    cout << "Please enter the number of Sales People (1-10): " << endl;cin >> SalesPerson;

    srand(time(0));
    int** sales = new int*[Products];
    for (int i = 0; i < Products; ++i)
        sales[i] = new int[SalesPerson];
    int LowPerformance, LowPerformanceIndex = 0,TotalProduct = 0, TotalForSalesPerson = 0;

    for (int row = 0; row< Products - 1; row++)
    {
         for (int column = 0; column < SalesPerson - 1; column++)
        {
            sales[row][column] = rand() % 300;
        }
    }

    for (int row = 0; row< Products - 1; row++)
    {
        TotalProduct = 0;
        for (int column = 0; column < SalesPerson - 1; column++)
        {
            TotalProduct += sales[row][column];
        }
        sales[row][SalesPerson - 1] = TotalProduct;
    }

    for (int row = 0; row< SalesPerson; row++)
    {
        TotalForSalesPerson = 0;
        for (int column = 0; column < Products - 1; column++)
        {
             TotalForSalesPerson += sales[column][row];
        }
        sales[Products - 1][row] = TotalForSalesPerson;
    }

    for (int row = 0; row< Products; row++)
    {
        for (int column = 0; column < SalesPerson; column++)
        {
            cout << sales[row][column] << "\t";
        }
        cout << "\n";
    }
    LowPerformance = sales[Products][0];

    for (int row = 0; row< SalesPerson - 1; row++)
    {
        if (LowPerformance > sales[Products][row])
        {
            LowPerformance = sales[Products][row];
            LowPerformanceIndex = row;
        }
    }
    cout << "\n\nThe Lowest Performing Sales Person is: " <<LowPerformanceIndex << endl;
    for (int row = 0; row< Products; row++)
    {
        for (int column = 0; column < SalesPerson; column++)
        {
            if (column != LowPerformanceIndex)
                cout << sales[row][column] << "\t";
    }
    cout << "\n";
    }
system("PAUSE");
return 0;
}

1 个答案:

答案 0 :(得分:0)

将代码中的行更改为突出显示

my_form.addEventListener('submit',function (event){
    event.preventDefault();
    var request = new XMLHttpRequest();
    var request_process = function (){
        if (request.readyState == 4) {
            if(request.status == 200){
                window.location.replace('nice_URL');
            }else if(request.status == 401){
                login_display_error('Authentification error : '+request.responseText);
            }else{
                alert('Server response, '+request.status+' :'+request.responseText);
            }
        }
    };
    var requestURL = new String();
    requestURL = "requestURL.com";

    request.addEventListener("readystatechange", request_process, false);
    request.withCredentials = true;
    request.open("POST",requestURL , true);
    request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    var parameters = new URLSearchParams(new FormData(my_form)).toString();
    request.send(parameters);
});

规则:数组arr [m] [n]具有[m-1] [n-1]的最大可寻址索引,因为索引从0开始;