-4
double round(double a)
{
    double b, c, f, g;
    float d[2];
    c = modf(a, &b);
    if (a > 0) {
        f = a - c;
        g = a - c + 1;
        d[0] = f;
        d[1] = g;
        return d[0], d[1];
    }
    else {
        f = a - c;
        g = a - c - 1;
        d[0] = f;
        d[1] = g;
        return d[0], d[1];
    }
}

I need to get 2 numbers it the end(for ex: if I have num 12.34, I want to get 12 and 13)This is my function of rounding for pos and neg numbers. But it returns only 1 value(( So I'm stack...pls, help how to return 2 values?

rsjaffe
  • 5,600
  • 7
  • 27
  • 39
Max
  • 33
  • 7

2 Answers2

2

You cannot return two things in the return, so return d[0],d[1] compiles but doesn't work as you expect. You can use two reference parameters in the function prototype to return. Something like void round(double a, double* result1, double* result2). Into the function, set d[0] to *result1 and d[1] to *result2.

Another thing: Are you sure the line g = a - c - 1; when a is negative is correct? I think you need to do g = a + c - 1;, because a is negative.

SuperG280
  • 271
  • 3
  • 9
0
#include "pch.h"
#include <iostream>
#include <array>
using namespace std;

auto rounding(double x)
{
    int part = static_cast<int>(x);
    if (x < 0.0)
    {
        return array<int, 2> {
            part - 1, part
        };
    }
    else
    {
        return array<int, 2> {
            part, part + 1
        };
    }
}

int main()
{
    double x;
    cout << "Please, enter a float number to round: ";
    cin >> x;

    auto r1 = rounding(x);
    if (x > 0) {
        cout << "A lower value: " << r1[0] << endl << "A bigger value: " << r1[1];
    }
    else {
        cout << "A bigger value: " << r1[0] << endl << "A lower value: " << r1[1];
    }
}
Max
  • 33
  • 7