My solution to 2 beginner questions

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2007
Posts: 16
Reputation: kinggarden is an unknown quantity at this point 
Solved Threads: 0
kinggarden kinggarden is offline Offline
Newbie Poster

My solution to 2 beginner questions

 
0
  #1
Jun 6th, 2007
  • Write a program which finds the factorial of a number entered by the user. (check for all conditions) (Beginner).
  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.
  • 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)
  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.
Rock Mu

I will keep walking!

Shanghai, China
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,600
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 712
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: My solution to 2 beginner questions

 
0
  #2
Jun 6th, 2007
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 16
Reputation: kinggarden is an unknown quantity at this point 
Solved Threads: 0
kinggarden kinggarden is offline Offline
Newbie Poster

Re: My solution to 2 beginner questions

 
0
  #3
Jun 6th, 2007
Originally Posted by Narue View Post
>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.
Sorry, I misunderstood the question.

>Storing the data is a question.
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?

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

I will keep walking!

Shanghai, China
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: My solution to 2 beginner questions

 
0
  #4
Jun 7th, 2007
Originally Posted by kinggarden View Post
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,600
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 712
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: My solution to 2 beginner questions

 
0
  #5
Jun 7th, 2007
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC