GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Nov-2004, 16:28
brookeville brookeville is offline
Junior Member
 
Join Date: Oct 2004
Posts: 56
brookeville is on a distinguished road

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:
#include <cmath>
#include <iostream>
using namespace std;


void main() 
{
   double y;
   float number;
   int power;
   int squared;
   
   cout << "Enter a number and I will tell you the square and floor of it:" << endl;
   cin >> number;
   
   cout << "***********************************************" << endl;
   y = floor(number);
   power = static_cast<int>(number); squared = pow(power, 2);  
   cout << "The floor of " << number << " is " << y << endl;
   cout << "The square of " << power << " is " << squared << endl;
   cout << "***********************************************" << endl << endl;
return;
}
I know right off hand that I haven't prototyped the void main() function, as I don't quite know how to do that yet. (I haven't learned about prototyping yet, but there is a future chapter on it in my text). Also, I don't think I have two distinct functions in the above program... I'd be grateful for any help or assistance you can offer with this program!
  #2  
Old 01-Nov-2004, 16:45
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
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  
Old 01-Nov-2004, 19:17
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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:
int main();
int square_number(int number);
int floor_number(double number);
These should up near the top of your program.

Now you should have in your main function written something like:
CPP / C++ / C Code:
int main()
{
  int number1;
  double number2;
  int answer1;
  int answer2;

  //Ask for numbers
  answer1 = square_number(number1);
  answer2 = floor_number(number2);
}

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
  #4  
Old 01-Nov-2004, 19:58
brookeville brookeville is offline
Junior Member
 
Join Date: Oct 2004
Posts: 56
brookeville is on a distinguished road
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:
#include <cmath>
#include <iostream>
using namespace std;

//Function Prototypes
void squared();
void floor();


double number;

int main() 
{
   
   cout << "Enter a number and I will tell you the square and floor of it:" << endl;
   cin >> number;
   squared(); //call squared function
   floor(); //call floor function
   return 0;
}

//*************************************
// Definition of function squared.    *
// This function returns the square   *
// of the integer passed to it.       *
//*************************************
   
void squared()
{
	int convert_2_int;
	int num_squared;
	convert_2_int = static_cast<int>(number);
	num_squared = pow(convert_2_int, 2);
	cout << "The square of " << convert_2_int << " is " << num_squared << endl;
}

//*************************************
// Definition of function floor.      *
// This function returns the floor    *
// of a double passed to it           *
//*************************************

void floor()
{
	double num_floor;
	num_floor = floor(number);
	cout << "The floor of " << number << " is " << num_floor << endl;

}
I have written and declared the functions, floor and squared. Although I don't know if this is acceptable, since I have 3 functions in this program (void floor, void squared, and int main. Plus the library functions to calcluate the floor and pow of the number). The program asks to use two functions to perform two tasks, but I am assuming that adding another, main, should be fine. I will check with my professor to be sure. My questions that remain are:

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  
Old 01-Nov-2004, 20:04
brookeville brookeville is offline
Junior Member
 
Join Date: Oct 2004
Posts: 56
brookeville is on a distinguished road
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  
Old 01-Nov-2004, 20:25
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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...
  1. I think your functions would be better if they actually returned a value to the main function and then you printed that value. So instead of defining them as void, you should define them as the value that they will return.
  2. There are definitely exceptions, but I would always avoid using globals unless you have a very excellent reason for doing so. In this case you don't. number should be declared local to main and then passed to the appropriate functions.
  3. You have a function named floor that calls a different function called floor. Not good. Name your function something different, floor is already taken.

Anyway, it may be easier to see than explain. Here is what I would do:

CPP / C++ / C Code:
#include <cmath>
#include <iostream>
using namespace std;

//Function Prototypes
double squared(double number);
double floored(double number);


int main() 
{
	double number;
	
	cout << "Enter a number and I will tell you the square and floor of it: ";
	cin >> number;
	cout << "The square of your number is: " << squared(number) << endl;
	cout << "The floor of your number is: " << floored(number) << endl;
	
	return 0;
}

//*************************************
// Definition of function squared.    *
// This function returns the square   *
// of the integer passed to it.       *
//*************************************
   
double squared(double number)
{
	double num_squared;

	num_squared = pow(number, 2);
	
	return(num_squared);	
}


//*************************************
// Definition of function floor.      *
// This function returns the floor    *
// of a double passed to it           *
//*************************************

double floored(double number)
{
	double num_floor;
	
	num_floor = floor(number);
	
	return(num_floor);
}


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...
  #7  
Old 01-Nov-2004, 21:10
brookeville brookeville is offline
Junior Member
 
Join Date: Oct 2004
Posts: 56
brookeville is on a distinguished road
Quote:
Originally Posted by dsmith
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...
  1. I think your functions would be better if they actually returned a value to the main function and then you printed that value. So instead of defining them as void, you should define them as the value that they will return.
  2. There are definitely exceptions, but I would always avoid using globals unless you have a very excellent reason for doing so. In this case you don't. number should be declared local to main and then passed to the appropriate functions.
  3. You have a function named floor that calls a different function called floor. Not good. Name your function something different, floor is already taken.

Anyway, it may be easier to see than explain. Here is what I would do:


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...

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  
Old 01-Nov-2004, 21:29
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by brookeville
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!

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...
  #9  
Old 01-Nov-2004, 23:00
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
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:
Originally Posted by dsmith
Anyway, it may be easier to see than explain. Here is what I would do:
CPP / C++ / C Code:
...
double squared(double number)
{
	double num_squared;

	num_squared = pow(number, 2);
	
	return(num_squared);	
}
I'm not sure why you'd write the squared function by calling another function. Why call squared() at all if all it does is call pow(). Calculating a square is simple -- just calculate it....

Quote:
Originally Posted by dsmith
CPP / C++ / C Code:
double floored(double number)
{
	double num_floor;
	
	num_floor = floor(number);
	
	return(num_floor);
}
And this function is even worse... Again, calculate the floor, don't simply call the library function.
__________________

Age is unimportant -- except in cheese
  #10  
Old 02-Nov-2004, 00:56
brookeville brookeville is offline
Junior Member
 
Join Date: Oct 2004
Posts: 56
brookeville is on a distinguished road
Quote:
Originally Posted by dsmith
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...

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 GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 21:24.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.