Thread: Need help on my homework

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    17

    Question Need help on my homework

    Here are my directions:

    Construct a C++ class named “Cruncher” that implements a “factorial” method. The “factorial” method shall take an integer parameter, validate the parameter’s value as being a positive integer, and then calculate the factorial value associated with the parameter’s value. For example, a call such as “cruncher.factorial(5);” will yield a return value of 120, since 5! = 5 * 4 * 3 * 2 * 1. Use a “while” loop to make your calculation.

    You will also implement two accessor methods, getMaxFactorial() and getMinFactorial(). These methods will merely return the values of the maxFactorial and minFactorial instance variables, respectively. In other words, they are just simple “get” methods.

    You will need to ensure that the values of maxFactorial and minFactorial retain the largest and smallest factorials that have been calculated by the Cruncher object since it was instantiated. This implies that your “factorial” method will check to see if the currently calculated factorial is less than the minFactorial value or greater than the maxFactorial value, and, if so, update the instance variables accordingly.
    [Hint: To deal with the initialization of these variables, you can set them to zero. Then, make use of the fact that factorials are necessarily non-zero. The first time your factorial method is called your algorithm will check to see if either instance variable is zero, in which case you will set both instance variables to your calculated factorial value.]

    I am supposed to not use cin and also to end up with three files: Main.cpp, crunher.h and cruncher.cpp.

    here is what i have to use for main.cpp:

    Code:
    #include “stdafx.h”
    #include <string.h>
    
    #include “Cruncher.h”
    
    using std::cout;
    using std::endl;
    
    int main() {
    	Cruncher cruncher;
    
    	int number = 5;
    	cout << “The factorial of “ << number << “ is”
    		<< cruncher.factorial(number) << endl;
    
    	number = 8;
    	cout << “The factorial of “ << number << “ is”
    		<< cruncher.factorial(number) << endl;
    
    	number = 3;
    	cout << “The factorial of “ << number << “ is”
    		<< cruncher.factorial(number) << endl;
    
    	cout << “The maximum factorial calculated was “
    		<< cruncher.getMaxFactorial() << endl;
    
    	cout << “The minimum factorial calculated was “
    		<< cruncher.getMinFactorial() << endl;
    }

    Can anyone tell me where to begin with the cruncher.h and cruncher.cpp files?

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    If necessary, think about what data Cruncher objects will have to remember.

    Write the class declaration in Cruncher.h, with declarations of constructors and methods and private member variables. Then write each particular method in Cruncher.cpp.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    17
    Whoa, that just confused me so much. I'm a newbie at this stuff.

  4. #4
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    First off, do not declare a variable of certain type with that type name (even if some capitalization is different, it is just confusing and will only lead to trouble, even if it is legal).

    Now, are you asking how to build/create the class or how to split it up and put it in the header and implementation files?

  5. #5
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by mejv3
    Whoa, that just confused me so much. I'm a newbie at this stuff.
    Read your textbook; pay attention in class.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    17
    RF:
    This is an online class and I only have the textbook to go by, which is
    confusing to me. The teacher doesn't post any lectures to help us out.

    Enahs: I just need to know to build the header.

  7. #7
    Counter-Strike Master
    Join Date
    Dec 2002
    Posts
    38
    here is how you would go about creating a class. the h file would contain prototype information about the class, and you need to include it in any cpp files that use the class:

    Code:
    //factorial.h
    
    #ifndef FACTORIAL_H
    #define FACTORIAL_H
    
    class Cruncher
    {
    	public:
    		int factorial(int num);
    		int getMinFactorial(void);
    		int getMaxFactorial(void);
    		//place the rest of your function prototypes
    		// here for public access
    	private:
    		//list all the variables that your class will
    		// use internally here
    };
    #endif
    and here is the corresponding cpp file, where you write your code:

    Code:
    //factorial.cpp
    
    #include <iostream>
    #include "factorial.h"
    
    int Cruncher::factorial(int num)
    {
    	//write the actual code for this member function here
    }
    
    //continue in the same format for the rest of your member functions
    You say "Impressive!", but I already know it
    I'm a hardcore player and I'm not afraid to show it

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    17
    thanks

  9. #9

  10. #10
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    that is really funny, but cold
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  11. #11
    Counter-Strike Master
    Join Date
    Dec 2002
    Posts
    38
    i hear that mcdonalds gives competitive pay. maybe you can program their mcflurry machines :P
    You say "Impressive!", but I already know it
    I'm a hardcore player and I'm not afraid to show it

  12. #12
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by xviddivxoggmp3
    that is really funny, but cold
    Nah, I can't say that it's funny at all. Not knowing how to program can be explained in two different ways: either ignorance or lack of mental functions. Being ignorant doesn't make someone dumb.

    slcjoey, you weren't born knowing how to program. Give a newbie some slack.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  13. #13
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    pianorain
    I agree it is a little harsh. Everybody has to start somewhere.
    I know I have asked for help many times on this board.
    I was just saying that was original and really funny.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  14. #14
    Registered User
    Join Date
    Mar 2005
    Posts
    22
    Quote Originally Posted by pianorain
    Nah, I can't say that it's funny at all. Not knowing how to program can be explained in two different ways: either ignorance or lack of mental functions. Being ignorant doesn't make someone dumb.

    slcjoey, you weren't born knowing how to program. Give a newbie some slack.
    It wasn't an attack on his intelligence. I'm sure McDonalds has a lot of smart people working for them, what they aren't chalk full of is people with drive and motivation, people who can take the initiative and do something for themselves.

    This poster is just plain lazy. He will never get anywhere having to come and ask questions on the most basic of topics.

    I mean, given the assignment, is it not safe to assume that he's been taught something relevant to this assignment in class? If not, I'm sure at the very least the instructor refers to a book ?

    And if still a "no" -- buy a book! this stuff isn't rocket science.
    Last edited by slcjoey; 10-11-2005 at 12:10 PM.

  15. #15
    Registered User
    Join Date
    Sep 2005
    Posts
    34
    Quote Originally Posted by slcjoey
    I know a software engineer that works for burger king....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  2. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  3. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM