Help with switch and sorting

I need to create a program that computes terms of the fibonacci sequence both recursively and iteratively for the first 45 terms. It needs to do each term 6 times and report the least amount of time for calculation and the most amount of time. I have the program working (it's connected to a header and another cpp file) to just do one trial of each term and report the time taken (and the value of the term). My issue is in doing 6 trials of each term and reporting the least and greatest computation times. I want to use a switch statement, however I'm getting errors in the first cases (lines 37 and 67), saying I'm missing a ; Also, I have no idea how to effeciently determine which trials took the least and greatest computation times. As can be seen in my code, I'm outputting variables "low1", "high1", "low2", and "high2" without having done anyting to them. I want to fill them with the sorted values of computation time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
using std::cout;
using std::endl;

#include <iomanip>
using std::setfill;
using std::setw;

#include "FiboImp.h"

#include <windows.h>

const int MAXN(45);

int main()
{
  unsigned long int before, after, result, a, b, c, d, e, f, z, y, x, w, v, u, low1, high1, low2, high2;
  int i, j;

  cout << "Execution time for Fibonacci no. implementions (ms)\n";
  cout << setfill('+') << setw(64) << "+" << endl << setfill(' ');
  cout << setw(4) << "n" << setw(30) << "Recursive"
    << setw(30) << "Iterative" << endl;
  cout << setfill('+') << setw(64) << "+" << endl << setfill(' ');
 
  for (int n=0; n<=MAXN; n++) 
  {
    cout << setw(4) << n;

    for(i = 1; i <= 6; i++)
    {
      before = GetTickCount();
        result = FiboRecursive(n);
      after = GetTickCount();

      switch (i)
      {
        case 1;
          a = after - before;
          break;
        case 2;
          b = after - before;
          break;
        case 3;
          c = after - before;
          break;
        case 4;
          d = after - before;
          break;
        case 5;
          e = after - before;
          break;
        default;
          f = after - before;
      }
    }

    cout << setw(20) << result << setw(20) << "(    " << low1 << " ---    " << high1 << ")";

    for(j = 1; j <= 6; j++)
    {
      before=GetTickCount();
        result=FiboIterative(n);
      after=GetTickCount();

      switch (j)
      {
        case 1;
          z = after - before;
          break;
        case 2;
          y = after - before;
          break;
        case 3;
          x = after - before;
          break;
        case 4;
          w = after - before;
          break;
        case 5;
          v = after - before;
          break;
        default;
          u = after - before;
      }
    }

    cout << setw(20) << result << setw(20) << "(    " << low2 << " ---    " << high2 << ")";
    cout << endl;
  }

  return 0;
}
Hi there,

You have a syntax error in your switch statement. case: is written with a colon, not a semicolon:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
switch (i)
      {
        case 1:
          a = after - before;
          break;
        case 2:
          b = after - before;
          break;
        case 3:
          c = after - before;
          break;
        case 4:
          d = after - before;
          break;
        case 5:
          e = after - before;
          break;
        default:
          f = after - before;
      }


As for keeping the highest and lowest times, might I suggest that you use an array to store the tick-values (it would also make your switch statement unnecessary):

1
2
3
4
5
6
7
8
9
10
11
unsigned long int ticks[6];

for(i = 1; i <= 6; i++)
for (i=0; i<6; i++) //allows us to use i as an array index
    {
      before = GetTickCount();
        result = FiboRecursive(n);
      after = GetTickCount();
      
      ticks[i] = after-before;
    }


Now, in order to see which was highest and lowest, all you need to do is iterate the ticks array and keep track of the highest / lowest value:

1
2
3
4
5
6
7
8
9
int highest=0, lowest=0;
for (i=0; i<6; i++)
{
    if (ticks[i] > highest)
        highest = ticks[i];

    if (ticks[i] < lowest)
        lowest = ticks[i];
}



Hope that helps. Please do let us know if you require any further help.

All the best,
NwN
Thank you
@NwN
On your last snippet, I would consider replacing
int highest=0, lowest=0;
with
1
2
int highest, lowest;
highest = lowest = ticks[0];

or any other method to initialize the variables with the first element in the array. For highest it probably doesn't matter, but if you start lowest at zero, no value in the array will replace it.
Topic archived. No new replies allowed.