2

Having some trouble finding the sum of a 2D vector. Does this look ok?

int sumOfElements(vector<iniMatrix> &theBlocks)
{
  int theSum = 0;

  for(unsigned i=0; (i < theBlocks.size()); i++)
  { 
    for(unsigned j=0; (j < theBlocks[i].size()); j++)
    {
        theSum +=theBlocks[i][j];
    }
  }

  return theSum;
}

It returns a negative number, however, it should return a positive number..

Hope someone can help :)

Phorce
  • 2,632
  • 13
  • 43
  • 76

4 Answers4

2

The code looks proper in an abstract sense, but you may be overflowing theSum. You can try making theSum type double to see what value you get to help sort out the proper integral type to use for it.

double sumOfElements(vector<iniMatrix> &theBlocks)
{
  double theSum = 0;
  /* ... */
  return theSum;
}

When you observe the returned value, you can see if it would fit in an int or if you need to use a wider long or long long type.

If all the values in the matrix are positive, you should consider using one of the unsigned integral types. which would double your range of allowed values.

jxh
  • 69,070
  • 8
  • 110
  • 193
  • 2
    Why use double - would not long or long long be better (or their unsigned equivalents) – Adrian Cornish Aug 17 '12 at 02:15
  • 1
    @AdrianCornish: It just depends on how much overflow there is, and how big the things are that are being added up. The double is just the first pass, to be replaced by the proper integral type later if possible. – jxh Aug 17 '12 at 02:16
  • Agree - for that purpose it would be fine - I have doubles and floats in nightmares because of precision and == comparisons in my job day to day so I hate them ;-) – Adrian Cornish Aug 17 '12 at 02:21
  • Hey, thank you for your replies.. The vector contains negative numbers, however, the for loop starts from 0.. So it might be the values! – Phorce Aug 17 '12 at 02:22
  • 1
    @AdrianCornish: Preaching to the choir :-) I don't know how much clearer I can make it in my answer though, as I state it twice. Once before the code, and once after. – jxh Aug 17 '12 at 02:22
  • :-) I must admit I was deficit in reading your answer in detail and just saw my nemesis 'double' - already spent a week recoding stuff because 0.005!=0.0050000000001104 grrr = dont send me data as doubles – Adrian Cornish Aug 17 '12 at 02:26
  • @Phorce: You might consider asking dasblinkenlight to make an answer for you to accept to reward him for helping you, or deleting the question. – jxh Aug 17 '12 at 02:26
  • @user315052 Hey I said it first ;-P - not worried though I can live with someone else being right too – Adrian Cornish Aug 17 '12 at 02:29
  • "Are all elements of the matrix positive? – dasblinkenlight 18 mins ago" – Adrian Cornish Aug 17 '12 at 02:30
2

The problem is obviously the int exceeds its boundary (like others said)

For signed data types it becomes negative when overflow, and for unsigned datatypes it starts from zero again after overflow.

If you want to detect overflow pragmatically, you can paste these lines instead of the additional line.

if( theSum > int(theSum + theBlocks[i][j]) )
    //print error message, throw exception, break, ...
    break;
else
    theSum += theBlocks[i][j];

For more generic solution to work with more data types and more operations than addition, check this: How to detect integer overflow?

A solution would be using unsigned long long and if it exceeds its boundary too, you need to use third party libraries for big integers.

Community
  • 1
  • 1
Mahmoud Hassan
  • 598
  • 1
  • 3
  • 15
1

Like Mokhtar Ashour says, it's may be that the variable theSum overflows. Try making it either unsigned if no numbers are negative, or change the type from int (which is 32 bits) to long long (which is 64 bits).

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

I think it may be int overflow problem. to make sure, you may insert a condition after the inner loop finishes to see if your result exceeds the int range.

if(result>sizeof(int))
    cout<<"hitting boundaries";

a better way to test if you exceed the int boundaries is to print the result after the inner loop ends and notice the result.

.if so, just use a bigger data type.

Mokhtar Ashour
  • 600
  • 2
  • 9
  • 21