Wednesday, 15 August 2012

How can I get this C program to accept new input without adding it to the old input? -



How can I get this C program to accept new input without adding it to the old input? -

i'm stuck on programming exercise have homework. i've gotten close completing it, can't figure out how have programme inquire user "enter next set of limits" @ end without programme adding new input old input.

here's question appears in book:

write programme requests lower , upper integer limits, calculates sum of integer squares square of lower limit square of upper limit, , displays answer. programme should go on prompt limits , display answers until user enters upper limit equal or less lower limit. sample run should this:

enter lower , upper integer limits: 5 9 sums of squares 25 81 255 come in next set of limits: 3 25 sums of squares 9 625 5520 come in next set of limits: 5 5 done

here's code i've written:

#include <stdio.h> int main(void) { int index, lower, upper, square, total, input; printf("enter lower , upper integer limits: "); (input = scanf("%d %d", &lower, &upper); input == 2; printf("enter next set of limits: \n"), scanf("%d %d", &lower, &upper)) { (index = lower; index <= upper; index++) { square = index * index; total += square; } printf("the sums of squares %d %d %d\n", lower * lower, upper * upper, total); } homecoming 0; }

any help much appreciated! i've been working on over hour.

update, here's have now, it's still not right since doesn't print out "done" when upper , lower limits same:

include

int main(void) { int index, lower, upper, square, total;

printf("enter lower , upper integer limits: "); while (scanf("%d %d", &lower, &upper) == 2) { total = 0; (index = lower; upper > index; index++) { square = index * index; total += square; } printf("the sums of squares %d %d %d\n", lower * lower, upper * upper, total); printf("enter next set of limits: \n"); } homecoming 0;

}

update*****

thanks everyone's help think got it:

include

int main(void) { int index, lower, upper, square, total;

printf("enter lower , upper integer limits: "); while (scanf("%d %d", &lower, &upper) == 2) { while (lower < upper) { total = 0; (index = lower; index <= upper; index++) { square = index * index; total += square; } printf("the sums of squares %d %d %d\n", lower * lower, upper * upper, total); printf("enter next set of limits: \n"); scanf("%d %d", &lower, &upper); } printf("done"); } homecoming 0;

}

just wrap code in :

while( upper > lower ) { ... }

(and initialize upper , lower appropriately loop entered.)

c

No comments:

Post a Comment