### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293`` ``````#include using std::cout; using std::endl; #include using std::setfill; using std::setw; #include "FiboImp.h" #include 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:

 ``1234567891011121314151617181920`` ``````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):

 ``1234567891011`` ``````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:

 ``123456789`` ``````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
 ``12`` ``````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.