![]() |
|
#1
|
|||
|
|||
Please help with functions...Hello again, I have run into another problem with programming, and I have decided to turn to GidForums for assistance. I seem to be having trouble working out this program to meet the required specifications... Any help with this program dilemma is, as always, greatly appreciated. :-)
The question asks the following: Write a program that uses 2 functions to perform 2 tasks. The first function must return the square of the integer passed to it, and the second function must return the floor of a double passed to it. All functions must be prototyped, including main. Here is what I have come up with so far: CPP / C++ / C Code:
|
|
#2
|
|||
|
|||
|
Do you want us to write the functions for you ? I dont think anyone in this forum like to do that. What we can definitely do is, try and solved the problem in your functions (if you have any) once you post them.
prototyping main() ? I am sorry, but I havent heard or seen it before. What I would suggest to you is, post complete program with the other two functions and ask specific questions on problems that you are facing. We will be more than happy to help you. But with all due respect, don't ask us to do your homework for you. If you are lucky, and any one in the forum is having free time to do that, they might as well do it for you. So you can either sit and wait or work and post. Choice is yours. Good Luck. |
|
#3
|
||||
|
||||
|
Hello brookeville.
Yeah, it seems kind of wierd to prototype main, but definitely can be done. One thing to note is that on many compilers (including mine ) it is an error if main does not return an int. So I always define main to return an int. I am not sure if you understand what prototyping means, but it simply means pre-defining what your functions are, what they will return and what parameters that they can take, like so:CPP / C++ / C Code:
Now you should have in your main function written something like: CPP / C++ / C Code:
Now you need to write the functions. They will be very small and it may seem silly to write such small functions, but this is the basis of modular programming. Also, I have noticed that if your program is split into logical, smaller functions it is much easier to debug, maintain and understand. HTH __________________
The best damn Sports Blog period. |
|
#4
|
|||
|
|||
|
Hello again, and thank you both for your help. After reviewing the chapters on prototypes and functions, I have come up with this:
CPP / C++ / C Code:
1) Did I prototype all functions correctly? (dsmith, you are correct that I haven't learned prototyping yet, but I read up a little on it) 2) Are there any logical errors or directions I haven't followed? Thanks again for your help! |
|
#5
|
|||
|
|||
|
Another quick question I have involves the floor function. I have not used the floor function before in other programs, and I was wondering if I correctly used it in this program. As I understand, floor returns the closest integer value to the number in question.
If I were to enter 5.75 as the number to be "processed," should floor round it up to 6 or should it round it down to 5? Right now, the program rounds down to the closest whole integer (5). Again, I have no previous knowledge of the floor function and this is my first time using it. |
|
#6
|
||||
|
||||
|
Hi brookeville.
First of all floor is not round. It returns the lowest integer, ie 5.99 would be 5. Also, incidentally it returns a floating point value, ie 5.99 is actually 5.00. Now for you code, I have a couple of comments...
Anyway, it may be easier to see than explain. Here is what I would do: CPP / C++ / C Code:
Do you see the difference? I know if may not make much sense with such a small program & functions, but I can now reuse these functions over & over throughout the program. They should only do one thing: compute the answer, not print it... __________________
The best damn Sports Blog period. |
|
#7
|
|||
|
|||
|
Quote:
Thanks dsmith. I now understand about the floor function and how it works, thanks to your help. It is definitely a good idea to rename the floor function to "floored," as it can get confusing the way I had it... The same goes for variables, and my two-time using of the floor function should be split into separate functions. I see what you did regarding computing the values, rather than using cout to display them. I have one question regarding the code you gave me. I think the question asks to return the square of the integer for the number entered. So, does that mean that if the user enters 3.3, the computer should convert 3.3 to the whole number 3 (And then output 9 as the square)? I had that in an earlier program posted above, where I used static_cast<int> to convert the number entered to an int. What should I do with the statement of code you suggested, double squared(double number)? Should I change double number to int number? Thanks again! |
|
#8
|
||||
|
||||
|
Quote:
Ahhh right... I think you should probably change the squared function to accept & return an integer in that case. I am not sure if your program should asked for a seperate number to round or just pass the integer value of the double. On a positive note your code formatting is very nice. I would encourage you to keep your formatting clean and readable. It really helps with debugging and just makes it much easier on the eyes... __________________
The best damn Sports Blog period. |
|
#9
|
||||
|
||||
|
I have to disagree with you here, D. I think I know why you are saying this but I feel it's steering brookville in the wrong direction programatically.
Quote:
Quote:
__________________
Age is unimportant -- except in cheese |
|
#10
|
|||
|
|||
|
Quote:
OK, I'll have to modify it so it converts the number to an int first... should be easy! Thanks for complimenting on my code formatting! :-) I agree, formatting one's code by indenting, commenting, etc... helps make it easier to read and review. |
Recent GIDBlog
Meeting the local Iraqis by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| variables in functions help | dopee | MySQL / PHP Forum | 5 | 16-Oct-2004 20:20 |
| Having problem in calling functions inthe main | harsha | C Programming Language | 1 | 13-Oct-2004 00:05 |
| conflict between printf and stdarg.h va functions | mirizar | C Programming Language | 3 | 12-Jul-2004 08:11 |
| Understanding functions | tommy69 | C Programming Language | 15 | 15-Mar-2004 17:59 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The