Click here to Skip to main content
15,399,749 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I don't know what is wrong with the code but I can only get result for 16 and 64 then the code stop running.
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

int main() {
    int n[6] = {16, 64, 256, 1024, 4096, 16384};
    int next = 0;
    while (next < 6) {
        int a, b, x;
        int **M = new int *[n[next]];
        for (int i = 0; i < n[next]; i++) {
            M[i] = new int[i];
        }
        for (int i = 0; i < n[next]; i++) {
            for (int j = 0; j < n[next]; j++) {
                M[i][j] = 0;
            }
        }
        size_t m = 13421772800;
        time_t timer;
        timer = clock();
       
        for (int i = 0; i < m; i++) {
            x = rand() % 100 + 1;
            b = rand() % n[next];
            a = rand() % n[next];
            M[a][b] = M[a][b] + x;
        }
        timer = clock() - timer;
        printf("Clock time for n size %d: %.2f seconds", n[next], ((float)timer) / CLOCKS_PER_SEC);
        cout << endl;
        next++;
    }
    return 0;
}


This is what I get
Clock time for n size 16: 80.07 seconds
Clock time for n size 64: 79.99 seconds

[Done] exited with code=3221226356 in 160.458 seconds


What I have tried:

Tried to run the code using vscode
Posted
Updated 2-Oct-21 18:39pm

1 solution

The problem is the value m exceeds the limit of an integer so i will never reach it. Below shows how it can be fixed. I also have some suggestions.

1. As the first statement in your while loop, add this : int nextN = n[next]; and then use nextN everywhere else that n[next] appears. That will make it easier when you are viewing variables while debugging.

2. Release all the memory you are allocating as soon as you are finished with it. If I were you, I would make a function to allocate two dimensional arrays of memory and another one to release them. Since this is c++, normally I would use a vector of vectors or a template function with arrays but neither are likely to be appropriate here. Therefore, I would settle for a pair of functions that could do this. Here are examples:
// define a couple of types

typedef int *	pint;
typedef pint *	ppint;

ppint Allocate2Darray( int dim1, int dim2 )
{
    ppint ptrs = new pint[ dim1 ];
    for( int n = 0; n < dim1; ++n )
        ptrs[ n ] = new int[ dim2 ];
    return ptrs;
}

// a reference is passed so the value can be nulled

void Release2Darray( ppint & ptrs, int dim1 )
{
    for( int n = 0; n < dim1; ++n )
        delete [] ptrs[ n ];
    delete [] ptrs;
    ptrs = nullptr;   // clear the pointer
}
3. Use constant values instead of literal values. To be specific :
C++
const int ValueCount = 6;
Combining all of these, your code could be :
C++
const int ValueCount = 6;

int main()
{
    const int n[ValueCount] = {16, 64, 256, 1024, 4096, 16384};
    int a, b, x;
    int next = 0;
    while (next < ValueCount)
    {
        int nextN = n[ next ];
        ppint M = Allocate2Darray( nextN, nextN );

        for (int i = 0; i < nextN; i++) {
            for (int j = 0; j < nextN; j++) {
                M[i][j] = 0;
            }
        }

        time_t timer;
        timer = clock();

        const size_t count = 13421772800;
       
        for (size_t i = 0; i < count; i++)   // i must be a size_t also
        {
            x = rand() % 100 + 1;
            b = rand() % nextN;
            a = rand() % nextN;
            M[a][b] += x;
        }

        timer = clock() - timer;
        printf("Clock time for n size %d: %.3f seconds\n", n[next], (float)timer / CLOCKS_PER_SEC);

        Release2Darray( M, next );
        next++;
    }
    return 0;
}
Note that I added a few const statements where appropriate.
   
v3
Comments
Rick York 3-Oct-21 1:08am
   
One other thing : the (pseudo) random number generator should be seeded first. This can be done calling
    srand( 47 );
Generally, it is best to seed with a prime number. Using a constant value like this will give a repeatable sequence of values. Meaning every time you run the program you will get the same sequence of values. If you want a unique sequence of values every time then do this :
    srand( time(nullptr) );

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900