You are not logged in.

#1 2010-08-06 04:37:46

grimx
Member
Registered: 2010-07-31
Posts: 27

C programming problem

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

#2 2010-08-06 04:50:55

saline
Member
Registered: 2010-02-20
Posts: 86

Re: C programming problem

grimx wrote:
    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

#3 2010-08-06 04:58:02

grimx
Member
Registered: 2010-07-31
Posts: 27

Re: C programming problem

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

#4 2010-08-06 05:02:16

grimx
Member
Registered: 2010-07-31
Posts: 27

Re: C programming problem

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

#5 2010-08-06 06:35:39

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: C programming problem

Closing, as it is very likely that this is homework.

See here for details.

Offline

Board footer

Powered by FluxBB