Thread: How to make a for-loop for an equation

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    4

    Angry How to make a for-loop for an equation

    Hi, I was wondering if someone can help me with a pretty basic for loop for one of the equation. I've been bashing my head about it for the past 3-4 days and I am nowhere near the exit. It has to be calculated for a number n that has been inputted.
    How to make a for-loop for an equation-untitled-png

    I am grateful for the help

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,729
    So... It's a sum of fractions. Each fraction is the sum of the "n" first odd numbers, all divided by the sum of the squares of the "n" first odd numbers...

    If you were to brute-force it, you need 1 outer loop and 2 inner loops. But if you do a bit of math wizardry, you can reduce it to a single loop. Food for thought, the sum of all natural numbers from 1 to N is equal to N*(N+1)/2. You can derive similar formulas for the sum of all odd numbers and for the sum of the squares too.
    Devoted my life to programming...

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,729
    I leave the math solution up to you to figure out, but the brute force it dead-easy:
    Code:
    for (i = 0; i < N; ++i) {
        for (j = 0; j <= i; ++j) {
            // Sum up the numerator
            // Sum up the denominator
        }
        // Divide the numerator with the denominator and add the result to S 
    }
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    3,864
    Code:
    for (i = 1; i < N; i+=2) {
        for (j = 1; j <= i; i+=2) {
            // Sum up the numerator
            // Sum up the denominator
        }
        // Divide the numerator with the denominator and add the result to S 
    }
    Made what I hope is a correction to GReaper code.

    Edit: Changed "=+" to "+=" and "j = 0" to "j = 1"

    Tim S.
    Last edited by stahta01; 11-27-2017 at 08:43 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,729
    Heh, the funny thing is that we're both right at the same time. Depending on the implementation, both ways can be used. I still had math spinning in my head after finding the formula for the sum of squares, and I was thinking of odd numbers in terms of natural numbers, like (2*n + 1).
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Nov 2017
    Posts
    4
    I've still been bashing my head to no avail. This is the best I've come up with and it doesn't work. Here's the entire code. Feel free to correct me wherever you like. Being new with all this, I have no damn idea what I am doing and where it's all going wrong. I'd honestly be grateful for the guidance. The other idea was to use if(i%2!=0), but that went nowhere.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ()
    {
    int i,j,k=1,n;
    float s,p,q;
    printf("Insert n=");
    scanf("%d",&n);
    s=0; p=0;q=0;
    for (i=1; i<=n;i+=2){
    for (j=1;j<=n;j+=2){
        k*=i;
    
    }
        q+=k;
        p+=i;
    }
    s+=q/p;
    printf("%.2f",s);
    return 0;
    
    }


    All I know is that for number 3, the result given should be 8.00, not 1.80

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,063
    Pick better variable names.
    Code:
    for ( powers = 1 ; powers <= 10 ; powers += 2 ) {
      int numerator = 0;
      for ( sum = 1 ; sum <= powers ; sum += 2 ) {
        numerator += sum;
      }
      printf("The numerator for power=%d is %d\n", power, numerator );
    }
    Do this for denominator as well.

    When you're happy that both are correct, proceed to calculation.

    Looking at a mass of i,j,k tells you nothing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-27-2017, 08:05 PM
  2. How to make a loop
    By Shurmin in forum C++ Programming
    Replies: 11
    Last Post: 03-31-2012, 11:11 PM
  3. Help to make a loop.
    By Ducky in forum C++ Programming
    Replies: 6
    Last Post: 03-09-2008, 06:25 AM
  4. Help with a small equation and loop
    By Zerohero11 in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2005, 10:24 AM
  5. Nested Loop with add-on Equation..?? Seems Impossible!
    By AssistMe in forum C Programming
    Replies: 3
    Last Post: 03-11-2005, 12:28 PM

Tags for this Thread