Home > Blockchain >  Using a for loop and switch statement to evaluate the sum of a geometric series with respect to diff
Using a for loop and switch statement to evaluate the sum of a geometric series with respect to diff

Time:11-03

I'm new to C and I'm trying to write a piece of code that computes the the sum of a geometric series using both an iterated sum and the formula. What I'd like to do is create a loop from 1 to 3 and then design a switch statement which evaluates both both the sum and formula for three different sets of values for a (the first term), r (the common ratio), and n (the number of terms). I wrote the functions to compute the sums separately first, and know that they work without the loop and switch statement. What I'm stuck on is how to include the loop and switch statement into the program. I've given it a try, but it evaluates to nothing and I've no idea how to proceed. So any help would be appreciated! I've included my C code below, which I hope helps illustrate what I'm trying to do.

#include <stdio.h>
#include <math.h>

float sumgeometric1 (float a, float r, int n) //sum by iteration
{
  float sum = 0;
  for (int i = 0; i < n; i  )
    {
      sum = sum   a;
      a = a * r;
    }
  return sum;
}

float sumgeometric2 (float a, float r, int n) //sum by formula
{

  return (a * (float) (1 - pow (r, n - 1)) / (1 - r));
}

int main (){
  int f, i, sum = 0;
  for (i = 1; i <= f;   i)
    {
      int f = 3;
      int sum = sum   i;
      switch (sum)
    {
    case 1:;
      float a1 = 0.01;
      float r1 = 1.1;
      int n1 = 10000;
      printf ("%f\n", sumgeometric1 (a1, r1, n1));
      printf("%f\n", sumgeometric2 (a1,r1,n1) );
    case 2:;
      float a2 = 2.0;
      float r2 = 0.01;
      int n2 = 500;
      printf ("%f\n", sumgeometric1 (a2, r2, n2));
      printf("%f\n", sumgeometric2 (a2,r2,n2) );
     case 3:;
      float a3 = 0.0001;
      float r3 = 2.0;
      int n3 = 100;
      printf ("%f\n", sumgeometric1 (a3, r3, n3));
      printf("%f\n", sumgeometric2 (a3,r3,n3) );
    }
      return 0;
    }
}

CodePudding user response:

First and foremost, this loop and switch design simply overcomplicates this program. You are attempting to make three sets of calls in a known order, which can be accomplished without the loop or the switch through regular procedural programming.

With that said...


f is uninitialized at the start of this program. The loop predicate i <= f will invoke Undefined Behaviour by reading this indeterminate value.

int f = 3; inside the loop has no effect. It simply shadows the f from the surrounding scope.

Likewise, int sum = sum i; shadows the previous definition of sum. It also references itself within its own initialization, which will cause undefined behaviour.


Let's assume you get lucky(!) and everything initializes to 0, so that we can have a look at the rest of the program.

sum is pointless as it simply mirrors i.

Additionally, there will only ever be a single iteration of this loop, as return 0; is located inside the loop. This mistake is counteracted by the mistake of not including a break statement in each case, causing them to fall-through to the one below.

For example, this means that when sum == 1, cases 1, 2, and 3 will execute in order. When sum == 2, cases 2 and 3 will execute in order.

If you are intent on having this loop and switch, it can simply be written as

for (int i = 1; i <= 3; i  ) {
    switch (i) {
    case 1:
        /* ... */;
        break;
    case 2:
        /* ... */;
        break;
    case 3:
        /* ... */;
        break;
    }
}

but again, this is the same as writing the contents of each case one after another in a normal, procedural way.


If you want to make practical use of a for loop, consider the use of arrays, so that you can change the size of your datasets without adjusting the code that handles them.

Here's a quick example of using an array of structures:

void print_sums(float a, float r, float n) {
    printf("%f\n%f\n",
            sumgeometric1(a, r, n),
            sumgeometric2(a, r, n));
}

int main (void) {
    struct {
        float a;
        float r;
        int n;
    } data[] = {
        { 0.01, 1.1, 10000 },
        { 2.0, 0.01, 500 },
        { 0.0001, 2.0, 100 },
        { 1.3, 0.02, 400 }
    };

    for (size_t i = 0; i < (sizeof data / sizeof *data); i  )
        print_sums(data[i].a, data[i].r, data[i].n);
}
  • Related