Pregunta

Here is some code I have. I am trying to keep a running total of a 2d array. I have a random number generator to generate a x and y location in a 2d array. the location gets a 2 added to the x and y position and the locations directly below, above, to the right, and to the left get a 1 added there. this can happen multiple time. I need to add up all the values entered into the array.

I cant get the running total to work. im not sure how to add the values entered into the 2d array. does anyone know how to do this?

int paintSplatterLoop(int ary [ROWS][COLS])
{
double bagCount,
       simCount,
       totalCupCount = 0.0;//accumulator, init with 0

double totalRowCount = 0, totalColCount=0;

double simAvgCount = 0;
double cupAvgCount;

for (simCount = 1; simCount <= 1; simCount++)
{
    for (bagCount = 1; bagCount <= 2; bagCount++)
    {
        for (int count = 1; count <= bagCount; count++);
        {
            int rRow = (rand()%8)+1;
            int rCol = (rand()%6)+1;
            ary[rRow][rCol]+=2; 
            ary[rRow-1][rCol]+=1; 
            ary[rRow+1][rCol]+=1;
            ary[rRow][rCol-1]+=1;
            ary[rRow][rCol+1]+=1;
        }
        totalRowCount += ary [rRow][rCol];
        totalColCount += rCol;
    }

}
totalCupCount = totalRowCount + totalColCount;
cout<<"total cups of paint "<<totalCupCount<<"\n"<<endl;

 return totalCupCount;
}
¿Fue útil?

Solución

This is how I would sum the contents of your two-dimensional array:

int sum_array(int array[ROWS][COLS])
{
    int sum = 0;

    for (int i = 0; i < ROWS; ++i)
    {
        for (int j = 0; j < COLS; ++j)
        {
            sum += array[i][j];
        }
    }

    return sum;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top