Re: C program question

From: Barry Schwarz (schwarzb_at_deloz.net)
Date: 10/31/04

  • Next message: Craig Bumpstead: "Trouble with pipe function"
    Date: Sat, 30 Oct 2004 17:05:47 -0700
    
    

    On 30 Oct 2004 13:57:47 -0700, oasf2004@yahoo.com wrote:

    >Hi All,
    >
    >One of the exercises of my C book ("C by Dissection" by Kelley and
    >Pohl) asked me to write a program that reads in an integer value for n
    >and then sums the integers from n to 2 * n if n is nonnegative, or
    >from 2 * n to n if n is negative. I think I was able to wrote
    >successfully the first part of the exercise, but I couldn't write the
    >second part. Please, see bellow the first part of my code:
    >
    >#include <stdio.h>
    >
    >int main()
    >{
    > int n;
    >
    > printf("Input one integer: ");
    > scanf("%d", &n);
    > while (n >= 0)
    > printf("The result is %d\n", n += (2 * n));

    This does not sum the integers from n to 2*n. It only calculates and
    prints 3*n.

    If the user inputs 4, the desired answer is 30 (4+5+6+7+8) while your
    code prints 12 (4+8).

    While there is a formula for the sum of integers from n to 2*n, I am
    pretty sure the object of the exercise is to generate a loop that
    computes the desired value.

    >
    > return 0;
    >}
    >
    >How could I write the second part of my code? I have no idea how could
    >I write a second while loop in order to get the sums from 2 * n to n,
    >if n is negative. I have tried:
    >...
    >while (n < 0) {
    > printf("The result is %d\n", n -= (2 * n));
    >....
    >but of course I didn't get the correct answer.
    >Thanks in advance for any hint!
    >Hoffmann

    Once you have the loop for positive values, the corresponding loop for
    negative ones should be easy.

    <<Remove the del for email>>


  • Next message: Craig Bumpstead: "Trouble with pipe function"

    Relevant Pages

    • Re: Comments needed for attempted proof of a simple integral
      ... > X" problems aren't so difficult because standard math education has been ... > about solving problems of that nature and I can usually verify answers ... > exercise for me to verify, and there are a lot of those in this book. ... >> My thinking is to translate the integral into a sum, ...
      (sci.math)
    • Re: modified "histogram" exercise from K&R2
      ... K&R2 exercise 1-13. ... Here's your printing loop, ... OK, a histogram is like a graph with x and y axis, like your solution ... part of it after you gave me LONG and CLEAR explanation but i can't ...
      (comp.lang.c)
    • Re: While Loops (I think)...
      ... > across an exercise that I can't get to work. ... > Sub Main ... The While loop will execute until a given condition becomes true. ... 'Deal with the valid input ...
      (microsoft.public.dotnet.languages.vb)
    • Steve Summit C notes , exercise
      ... this is the programme i created, for exercise 2, assignment 3 at ... the average of the squares of the numbers from 1 to 10. ... the sum of all ...
      (comp.lang.c)
    • Re: Comments needed for attempted proof of a simple integral
      ... I can usually verify answers to those problems with software. ... It's proofs that are hardest type of exercise for me to verify, ... > such that s is constant on the open subintervals of P. ... > gives the following sum: ...
      (sci.math)