Just learning, problem with an excercise.

This is a discussion on Just learning, problem with an excercise. within the C Programming forums, part of the General Programming Boards category; I have to do the following: Read a number and calculate the following sequence: 1-1/2+1/3-1/4+1/5...1/n This is the way I'm ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    16

    Just learning, problem with an excercise.

    I have to do the following:
    Read a number and calculate the following sequence:
    1-1/2+1/3-1/4+1/5...1/n

    This is the way I'm doing it, but it doesn't work:

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int a=0, n, suma=0;
    	float den;
    	
    	printf ("\n Ingrese el número : ");
    	scanf ("%i", &n);
    	
    	while (a<n)
    	{
    		if (a%2==0){
    			a++;
    			den=1/a;
    			suma-=den;}
    		
    		else{
    			a++;
    			den=1/a;
    			suma+=den;}
    	}
    	
    	
    	printf ("\n Resultado secuencia = %i", suma);
    	
    	return 0;
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I personally would (either in my head or on a sheet of paper) see what can be constructively noted about this function.

    1. So it looks like the denominator simply gets incremented by one each time.
    2. Even denominators get subtracted and odds added

    So couldn't you make a very simple loop sheerly from this alone?

    Example:
    Code:
    $denominator = 1
    $sum = 0.0;
    
    while $denominator < $n+1
      if iseven($denominator)
        sum -= 1.0 / $denominator
      else
        sum += 1.0 / $denominator
    loop

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Note also that:
    When a==0 1/a is NaN
    When a==1 1/a is 1
    Every other a 1/a is 0

    Maybe use 1.0/a instead.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yeah but the variable a should surely be 1 to begin with. Oooooooooh ok, nevermind. I just saw that your post was totally directed toward the OP.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    16
    This is how it looks now:

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int a=0, n;
    	float den, suma=0.0;
    	
    	printf ("\n Ingrese el n&#250;mero : ");
    	scanf ("&#37;i", &n);
    	
    	while (a<n)
    	{
    		if (a%2==0){
    			a++;
    			den=1.0/a;
    			suma-=den;}
    		
    		else{
    			a++;
    			den=1.0/a;
    			suma+=den;}
    	}
    	
    	printf("%f", den);
    	printf ("\n Resultado secuencia = %f", suma);
    	
    	return 0;
    }
    But this is what I get:

    Ingrese el n&#250;mero : 2
    Resultado secuencia = -0.500000

    Why the negative number?
    Last edited by medeshago; 10-15-2008 at 07:59 PM.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Read my code (even if it is pseudo code) and read what tabstop suggested. I see a pattern...

    Example:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int a=1, n;
    	float suma=0.0f;
    	
    	printf ("\n Ingrese el n&#250;mero : ");
    	scanf ("&#37;i", &n);
    	
    	while (a<n)
    	{
    		if (a%2==0)
    			suma-=1.0f/a;
    		else
    			suma +=1.0f/a;}
    		a++;
    	}
    	
    	// printf("%d", den); <- why? Who cares
    	printf ("\n Resultado secuencia = %d", suma);
    	
    	return 0;
    }
    Last edited by master5001; 10-15-2008 at 08:01 PM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by medeshago View Post
    This is how it looks now:

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int a=0, n;
    	float den, suma=0.0;
    	
    	printf ("\n Ingrese el número : ");
    	scanf ("%i", &n);
    	
    	while (a<n)
    	{
    		if (a%2==0){
    			a++;
    			den=1.0/a;
    			suma-=den;}
    		
    		else{
    			a++;
    			den=1.0/a;
    			suma+=den;}
    	}
    	
    	printf("%f", den);
    	printf ("\n Resultado secuencia = %f", suma);
    	
    	return 0;
    }
    But this is what I get:

    Ingrese el número : 2
    Resultado secuencia = -0.500000

    Why the negative number?
    Because you start by subtracting? You're currently doing -1 + 1/2 - 1/3 + 1/4 - ...

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    16
    Thanks, now it works great.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You need to start placing your brackets a little more cleanly. But at least you did use [code] tags. So I will simply say: you're welcome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  4. Awkward problem in learning c++
    By TheUnknownFacto in forum C++ Programming
    Replies: 6
    Last Post: 05-17-2007, 02:43 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21