Rounding

How can I write a program that takes an array of float and prints its contents to the nearest integer using print integers and void integer?


I've no idea what a void integer is, but here's a helpful thread:
http://www.cplusplus.com/forum/beginner/3600/
In C++11 you have std::round that can be used to round to the nearest integer value.
http://en.cppreference.com/w/cpp/numeric/math/round
I have to write two functions in addition to main??

int round (float x)

which would round any float to the nearest int, and return the int value

and

void print_integers(float a [ ], int size)

which would print the contents of array of size, size a already rounded to the nearest int.


Anyone able to help?
You haven't even made an attempt to code either function, or you haven't posted code. I'm not going to right it for you, but, I'll give you an overview of what the functions should do:

1
2
3
4
5
6
7
8
9
10
11
12
13
// Parameters: x - a float
// Return value: int
// What: Returns the rounded value of x
// How: Possibly using the floor/ceil functions in cmath
//        Could also use some math skills and type cast the returned value
int round(float x);

// Parameters: a - array of floats, size - size of the array - a
// Return value: None
// What: Displays integer values of each float in a
// How: Loops a variable from zero to size - 1, calls round on that float
//         and displays the returned value
void print_integers(float a [ ], int size);
Last edited on
I'm glad you didn't write it for him.

This is the same assignment that I have and I am trying to get it to run.

BUT I do like how you formatted your answer. Do you do that type of process for all your functions? Its very organized and it helped me as well!!
No, I haven't come up with a good commenting scheme yet. I've seen a lot that I liked, and they're all based around the same concept, what, how, returns and parameters. Some have a few extra fields naming the creator of the function, date/time, etc. It's hard to find something that's professional looking without overdoing it, yet those comments take up more lines than my actual code would.
how about just typecasting to int?
Topic archived. No new replies allowed.