Thread: Need help in C language

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    6

    Unhappy Need help in C language

    the number is taken from the keyboard, it is known that it is greater than 10 but less than 1000, so find if there are such consecutive numbers whose sum is equal to that number, if there are more than 1 result print all the results, if no such consecutive numbers sum print nothing,
    For Example:15=1+2+3+4+5
    15=7+8 and so on...
    Please if you can help try to explain in simple way so i could draw the flowchart easily
    Thanks

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. ask user to enter the number n
    2. check that it is in range [10,1000]
    3. find numbers i and k such that sum of numbers i, i+1, i+2, ... i+k-1 will give n
    4. print found sequence (for example 15=1+2+3+4+5)
    5. repeat Step 3 and 4 till no such sequence is found
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    6
    Quote Originally Posted by vart View Post
    1. ask user to enter the number n
    2. check that it is in range [10,1000]
    3. find numbers i and k such that sum of numbers i, i+1, i+2, ... i+k-1 will give n
    4. print found sequence (for example 15=1+2+3+4+5)
    5. repeat Step 3 and 4 till no such sequence is found
    can you explain it in more simple way please and in more details if possible
    Thank you

  4. #4
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Hatik View Post
    can you explain it in more simple way please and in more details if possible
    Thank you
    Which steps are you having trouble with in vart's list?

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    6
    Quote Originally Posted by SirPrattlepod View Post
    Which steps are you having trouble with in vart's list?
    how to find such numbers as i and k

  6. #6
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Well, there's lots of interesting things about this assignment/homework. Write down lots of ideas on paper without even necessarily trying to find a solution. Experiment with different numbers. See if you can find patterns. Can you work out if there is a maximum sequence length for the given number? Is there a minimum number the first number in the sequence can be? etc etc etc

  7. #7
    Registered User
    Join Date
    Oct 2013
    Posts
    6
    Quote Originally Posted by SirPrattlepod View Post
    Well, there's lots of interesting things about this assignment/homework. Write down lots of ideas on paper without even necessarily trying to find a solution. Experiment with different numbers. See if you can find patterns. Can you work out if there is a maximum sequence length for the given number? Is there a minimum number the first number in the sequence can be? etc etc etc
    I've tried, found some ideas but finally I am at the deadlock

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Hatik View Post
    I've tried, found some ideas but finally I am at the deadlock
    Really? vart gave you more than enough information to start. Try applying some effort to understanding his advice.

    Unfortunately, no amount of begging helplessly will work here, since this site has a homework policy. The core concept of that policy is that you need to do your own homework.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Hatik View Post
    how to find such numbers as i and k
    Math hint: Look at the average of the answers "1+2+3+4+5" and "7+8".

    Tim S.
    Last edited by stahta01; 10-16-2013 at 06:53 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if you have problem with step 3 - show the code that implements step 1 and 2
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Oct 2013
    Posts
    1
    Hatik, i m new to this... so, could you explain me the problem ?? i don't get it... 'The' number is taken from keyboard.. fyn.. whch means 1 number is taken from user.. then "find if there are consecutive numbers"... from where to find ?? give a detailed example..

    Thank you

  12. #12
    Registered User
    Join Date
    Oct 2013
    Posts
    6
    Quote Originally Posted by sdX View Post
    Hatik, i m new to this... so, could you explain me the problem ?? i don't get it... 'The' number is taken from keyboard.. fyn.. whch means 1 number is taken from user.. then "find if there are consecutive numbers"... from where to find ?? give a detailed example..

    Thank you
    in this example the enters any value that is in the range of 10-1000, it is a known fact,so the program should check if there are such consecutive numbers whose sum is equal to the number taken from user, if yes so print it(also if there are more than two such consecutive sequences print all of them).
    So for example : let's say number taken is 15
    there are two consecutive sequences such as 1+2+3+4+5=15 and 7+8=15
    I hope you understand what i mean

  13. #13
    Registered User
    Join Date
    Oct 2013
    Posts
    6
    Quote Originally Posted by vart View Post
    if you have problem with step 3 - show the code that implements step 1 and 2
    I may misunderstood your question but, for the first two question
    Code:
    int n;
    scanf("%d", &n);
    if (n>10 && n<1000)
    {
    \\the code
    }
    else printf("The number entered is wrong");

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    well for simplicity "\\the code" should be a call to the function like this
    Code:
    void splitNumber(int n)
    {
        int i;
        for(i=1;i<n;i++)
        {
          int k = getSequenceLength(n,i);
          if(k > 0)
             printSequence(n,i,k);
        }
    }
    And then you try to implement missing functions
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by vart View Post
    well for simplicity "\\the code" should be a call to the function like this
    Code:
    void splitNumber(int n)
    {
        int i;
        for(i=1;i<n;i++)
        {
          int k = getSequenceLength(n,i);
          if(k > 0)
             printSequence(n,i,k);
        }
    }
    And then you try to implement missing functions
    I suggest, first, working on printSequence; since you are having issues understanding the function getSequenceLength.

    Edit: I would NOT use this function (getSequenceLength) myself. I would write a function that returned i for a given (n,k) instead. I have not decided if 0 or -1 would be used to indicate no answer exists. My function would be getSequenceStart instead of getSequenceLength.

    Edit2: The getSequenceLength function seems to be a better fit to the OP problem; but, I see my function code better and depending on whether 15=15 is a valid answer should be easy enough to modify to fit the OP problem.

    I really think the OP needs to write out the answer he/she thinks is correct for 15 and for each line place the i and k values.
    I would also suggest placing the average for each line; I know this is what gave me my solution to this problem.


    Tim S.
    Last edited by stahta01; 10-20-2013 at 01:52 PM. Reason: grammar
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is C++ or C language is a high Level language?
    By uthmankhale in forum C++ Programming
    Replies: 5
    Last Post: 08-25-2011, 06:00 PM
  2. ASM to C language
    By TAZIN in forum C Programming
    Replies: 22
    Last Post: 06-03-2009, 06:29 AM
  3. What's the Difference Between a Programming Language and a Scripting Language?
    By Krak in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 07-15-2005, 04:46 PM
  4. C Language And A Scripting Language
    By DarkSpy in forum C Programming
    Replies: 9
    Last Post: 06-26-2003, 08:05 AM
  5. Computer Language VS Spoken Language
    By Isometric in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 02-04-2002, 03:47 PM

Tags for this Thread