Hello, Need a few tips on c++ please

Hello,
Having some trouble with my code. I have a row of numbers, Currently they are 6 decimals long. I want to round them and make them 5 decimal places.

the code im using is.

for(row = 0; row < MAX_ROWS; row++)
{
rowAverage[row]=0;
for(col = 0; col < MAX_COLS; col++)
{
rowAverage[row]=rowSum[row]/MAX_ROWS;
}

}


I have been given some psuedo code, but i dont understnad it.


' ! Round data to n decimal points
Function roundValue(Declare value, decimalPlaces)
i = 0
magnitude = 1
FOR i = 0 To decimalPlaces
magnitude = magnitude * 10
END FOR

value = value * magnitude
value = (int,value + 0.5)
value = value / magnitude
return value
END FUNCTION

FOR row = 1 To MAX_ROWS
total = 0.0
FOR col = 1 To MAX_COLS
total = total + rawData(row,col)
END FOR
avg = total / MAX_COLS
rawRowAvg(row) = roundValue(avg, DECIMAL_PLACES)
END FOR

Can anyone point me in the right direction, where to start looking please.

ive tried the following, but it doesnt work

for(row = 0; row < MAX_ROWS; row++)
{
rowAverage[row]=(int)(rowAverage[row]*10+0.5F);
rowAverage[row]=rowAverage[row]/10;
}

I updated it so the question is better, thank you
Last edited on
Sorry, i have typed it out wrong, the output i have now is to 6 decimal places, but i want to round up or down the last digit, so its its 111119 i want it to go to 111120, and if its 111111 i want it to go to 111110, do you have a link for that?
I think the only way to do this is to multiply the number out, round it and then divide it again. Here is an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double x = 5.536747, y = 9.354934, roundx = 0, roundy = 0;
    x = x * 10000.0f;
    roundx = round(x);
    roundx = roundx / 10000.0f;
    y = y * 10000.0f;
    roundy = round(y);
    roundy = roundy / 10000.0f;
    cout << roundx << "  " << roundy << "\n";
    return 0;
}
Could you show me how to implement it, I have the data already in a row of numbers, can I use what you have put up above to make the current data be rounded and 5 decimal places?
Something similar to this

for(row = 0; row < MAX_ROWS; row++)
{
rowAverage[row]=(float)(rowAverage[row]+.05);
}
Topic archived. No new replies allowed.