You are not logged in.
Pages: 1
Topic closed
using gcc 4.5.0-6
checking.c
#include <stdio.h>
#include <stdbool.h>
//validate that input is an integer
int get_int(void);
//vlaidate that range limits are validate
bool bad_limits(int begin, int end, int low, int high);
//calculate the sum of the squares of the integers
//a through b
double sum_squares(int a, int b);
int main(void)
{
const int MIN = -1000;
const int MAX = +1000;
int start;
int stop;
double answer;
printf("This program computes the sum of the squares of ");
printf("integers in a range.\nThe lower bound should not be ");
printf("less than -1000 and\nthe upper bound should not be ");
printf("more than +1000.\n");
printf("Enter the limits (enter 0 for both limits to quit):\n");
printf("lower limit: ");
start = get_int();
printf("upper limit: ");
stop = get_int();
while(start != 0 || stop != 0)
{
if(bad_limits(start, stop, MIN, MAX))
printf("Please try again.\n");
else
{
answer = sum_squares(start, stop); //// FOR SUM REASON answer EQUALS 0 NOT MATTER WHAT. errrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
// printf("\nanswer = %d\n", answer);
printf("The sum of the squares of the integers from ");
printf("%d to %d is %d\n", start, stop, answer);
}
printf("Enter the limits (enter 0 for both limits to quit):\n");
printf("lower limit: ");
start = get_int();
printf("upper limit: ");
stop = get_int();
}
printf("Done.\n");
return 0;
}
int get_int(void)
{
int input;
char ch;
while(scanf("%d", &input) != 1)
{
while((ch = getchar()) != '\n')
putchar(ch); //dispose of bad input
printf(" is not an integer.\nPlease enter an ");
printf("integer value, such as 25, -178, or 3: ");
}
return input;
}
double sum_squares(int a, int b)
{
double total = 0;
int i;
for(i = a; i <= b; i++)
{
total += (i * i);
// printf("\ni = %d total = %f\n", i, total);
}
// printf("\nreturning total = %f\n", total);
return total;
}
bool bad_limits(int begin, int end, int low, int high)
{
bool not_good = false;
if(begin > end)
{
printf("%s isn't smaller than %d.\n", begin, end);
not_good = true;
}
if(begin < low || end < low)
{
printf("Values must be %d or greater.\n", low);
not_good = true;
}
if(begin > high || end > high)
{
printf("Values must be %d or less.\n", high);
not_good = true;
}
return not_good;
}
the problem is this
answer = sum_squares(start, stop); //// FOR SUM REASON (answer) EQUALS 0 NOT MATTER WHAT. errrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
what's wrong in this function ???
double sum_squares(int a, int b)
{
double total = 0;
int i;
for(i = a; i <= b; i++)
{
total += (i * i);
printf("\ni = %d total = %f\n", i, total);
}
//printf("\nreturning total = %f\n", total);
return total;
}
Last edited by grimx (2010-08-06 04:41:08)
Arch 64Bit
x86_64 AMD Sempron(tm) Processor LE-1300
2 GIGS RAM, 500 GIG Hard Drive
NVIDIA GeForce 6160 SE integrated graphics
Offline
total += (i * i);
I believe this will interpret the result of (i * i) as a double. This is not what you want, I imagine. Try making total an int, long or long long and returning that.
Last edited by saline (2010-08-06 04:51:18)
Offline
that worked.
was the return value too big for the variable answer??
Arch 64Bit
x86_64 AMD Sempron(tm) Processor LE-1300
2 GIGS RAM, 500 GIG Hard Drive
NVIDIA GeForce 6160 SE integrated graphics
Offline
it was the %d in the printf
printf("The sum of the squares of the integers from ");
printf("%d to %d is %d\n", start, stop, answer); ///<<<<<<<<<<<<<<<<<<<<<<<
should have been %f instead of %g.
Last edited by grimx (2010-08-06 05:13:01)
Arch 64Bit
x86_64 AMD Sempron(tm) Processor LE-1300
2 GIGS RAM, 500 GIG Hard Drive
NVIDIA GeForce 6160 SE integrated graphics
Offline
Closing, as it is very likely that this is homework.
See here for details.
Offline
Pages: 1
Topic closed