944,093 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1650
  • C++ RSS
Jun 6th, 2007
0

My solution to 2 beginner questions

Expand Post »
  • Quote ...
    Write a program which finds the factorial of a number entered by the user. (check for all conditions) (Beginner).
C++ Syntax (Toggle Plain Text)
  1. void findfactor(const unsigned int number)
  2. {
  3. unsigned int n = number;
  4. printf("The factors ot the input number are:\n");
  5.  
  6. if (0 == n) {
  7. printf("%d\n", n);
  8.  
  9. return;
  10. }
  11. for(unsigned int i = 1; i < n / i + 1; i++) {
  12. if (n % i == 0 ) {
  13. printf("%d\n", i);
  14. printf("%d\n", n / i);
  15. }
  16. }
  17. }

You can see, that I don't store the factors. Acturally, I don't

know how to store the factors. The number of the factors are

unknown. I can't use fixed size array to store. What about list, stack

or dynamic array? Yeah, they could solve this problem. But if you

are in an interview, and you are not allowed to use these

encapsulated data structure, you may implement these at that time?

And it's a question for beginner, maybe I should use some beginner's

way.
  • Quote ...
    Create a program which generates fibonacci series till a number 'n' where 'n' is entered by the user. For eg. if the user enters 10 then the output would be: 1 1 2 3 5 8 (beginner)
C++ Syntax (Toggle Plain Text)
  1. void generate(int a, int b)
  2. {
  3. int total = a + b;
  4. if (total < n) { //n is the number inputed by user
  5. printf("total = %d\n", total);
  6. generate(b, total);
  7. }
  8. }

Storing the data is a question. And if the question is "Create a

program which begin to generate fibonacci series untill a number 'n'

is entered by the user. Then output the series below n.", is it still for

beginner ?

Some basic questions still make me feel uneasy, the way is long

long...
Last edited by kinggarden; Jun 6th, 2007 at 11:27 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kinggarden is offline Offline
16 posts
since Jun 2007
Jun 6th, 2007
0

Re: My solution to 2 beginner questions

>And it's a question for beginner, maybe I should use some beginner's way.
Maybe you should read the question more carefully. The problem asks you to calculate a factorial (that is, N!), not the factors of a given number. You're trying to solve the wrong problem.

>Storing the data is a question.
Why?

>is it still for beginner ?
Yes, the Fibonacci series is very easy to calculate.
Last edited by Narue; Jun 6th, 2007 at 12:00 pm.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 6th, 2007
0

Re: My solution to 2 beginner questions

Click to Expand / Collapse  Quote originally posted by Narue ...
>And it's a question for beginner, maybe I should use some beginner's way.
Quote ...
Maybe you should read the question more carefully. The problem asks you to calculate a factorial (that is, N!), not the factors of a given number. You're trying to solve the wrong problem.
Sorry, I misunderstood the question.

>Storing the data is a question.
Quote ...
Why?
Because I think I don't know the amount of the data, so I have to

use some dynamic array to store the data. Right?

Quote ...
>is it still for beginner ?
Yes, the Fibonacci series is very easy to calculate.
But I think it turns to be a multithreads question.

A thread generates Fibonacci serials.

A thread listen to the input.

And the two threads have to communicate in some way.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kinggarden is offline Offline
16 posts
since Jun 2007
Jun 7th, 2007
0

Re: My solution to 2 beginner questions

Click to Expand / Collapse  Quote originally posted by kinggarden ...
But I think it turns to be a multithreads question.
Threads are not for beginners. And a Fibonacci series is easy to calculate with a simple loop.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Jun 7th, 2007
0

Re: My solution to 2 beginner questions

>Because I think I don't know the amount of the data, so I have to
>use some dynamic array to store the data. Right?
Wrong. Both factorials and Fibonacci numbers are a linear series. You can calculate them with a trivial loop. You're making things ridiculously complicated.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Question, basic encrpytion?
Next Thread in C++ Forum Timeline: Global, yet not global....





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC