dhayden
So like I said, please post the actual assignment. |
The assignment is to develop a program which measures the execution time of finding fibonacci numbers using both iterative and recursive approaches.
The program needs to be function based and we have two headers; Stats.h and Prototypes.h
Stats.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#ifndef DT018G_STATS_H
#define DT018G_STATS_H
#include <string>
#include <vector>
typedef unsigned long long long_type;
struct Stats {
std::string type;
std::vector<long_type> values;
long_type nanosec=0, microsec=0, millisec=0;
double sec=0;
};
#endif //DT018G_STATS_H
|
Prototypes.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#ifndef DT018G_PROTOTYPES_H
#define DT018G_PROTOTYPES_H
#include "Stats.h"
#include <iostream>
#include <chrono>
#include <limits>
#include <iomanip>
#include <fstream>
long_type fibonacciIteration(size_t nthNumber);
long_type fibonacciRecursion(size_t nthNumber);
std::vector<Stats> fibonacciTimer(size_t nthNumber);
void printStats(const std::vector<Stats>& stats);
void writeToFile(const Stats& stats);
void mainMenu();
size_t subMenu();
void showMenu();
#endif //DT018G_PROTOTYPES_H
|
Int main() is only allowed to call void mainMenu(), that's it.
The functions fibonacciIteration() and fibonacciRecursion() are the two approaches. Both of these function are to be unchanged.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
//------------------------------------------------------------------------------
// Name: fibonacciIteration
//------------------------------------------------------------------------------
long_type fibonacciIteration(size_t nthNumber) {
size_t x = 0, y = 1, z = 0;
for (size_t i = 0; i < nthNumber; i++) {
z = x + y;
x = y;
y = z;
}
return x;
}
//------------------------------------------------------------------------------
// Name: fibonacciRecursion
//------------------------------------------------------------------------------
long_type fibonacciRecursion(size_t nthNumber) {
if (nthNumber <= 1)
return nthNumber;
return fibonacciRecursion(nthNumber - 1) + fibonacciRecursion(nthNumber - 2);
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
REPEAT {
DETERMINE type of approach
SET Stats.type
MARK start time
REPEAT {
CALL appropriate Fibonacci function
ADD Fibonacci value to Stats container
FOR EACH 5th sequence OUTPUT information on screen**
} IF iteration IS NOT 0*
MARK end time
CALCULATE duration of all needed formats
SET duration for relevant Stats members
} FOR each Fibonacci approach
RETURN container filled with two Stats objects
*Repeat execution from nthNumber until (and including) 0. If nthNumber to find is 30, then all Fibonacci values within the sequence range of 30...0 will be included.
**The information printed should contain type of approach, sequence and Fibonacci value. The value needs to be printed with right alignment.
|
mainMenu() should call fibonacciTimer(), and the fibonacciTimer should do the pseudo code above.
The time spent executing the full range of sequences for each Fibonacci approach will be measured and stored in Stats with various precision. A time stamp will mark the start before execution and another time stamp to mark the end when the full range of sequences has been calculated. The duration will be determined simply by subtracting the start time from end time and stored in a Stats structure with values for nanosec, microsec, millisec and sec. Note that the first three members is of type integer long_type
while the last member is a double!
There is also pseudo code for the menus, but I think I have them somewhat done:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
Pseudo Code, mainMenu()
REPEAT {
PRINT menu
REPEAT {
user input
} IF input NOT valid
BREAK IF exit
CALL subMenu
CALL fibonacciTimer
CALL printStats
CALL writeToFile
} IF repeat is wanted
|
1 2 3 4 5
|
Pseudo Code, subMenu() Example output
REPEAT {
user input
} IF input NOT valid
return user input
|
When all of this is done, fibonacciTimer should return a vector with Stats object to mainMenu() which in turn calls printStats().
printStats() is supposed to print statistical information on the screen, something like this:
Duration of running Fibonacci sequences within the range of 30..0
===================================================================
Nanosec Microsec Millisec Sec
Iteration: 4987200 4987 4 0.004
Recursion: 15133700 15133 15 0.015
===================================================================
|
All information needed should be pulled from the Stats object and at the top of the output there should be a header showing the range of the calculations. (0-30 in this case)
Seconds should be shown without trailing zeroes.
Each line of the table should consist of relevant data found in the Stats object, and based on above requirements the order should be:
stats.type | stats.nanosec | stats.microsec | stats.millisec | stats.sec
The last function is writeToFile:
mainMenu() should have a vector filled with two Stats objects both of which containing a vector of Fibonacci values, one for each sequence. The function writeToFile() accepts one
Stats object as parameter and needs to create a corresponding file containing these values.
Two files shall be created, one for each approach, and their names will consist of a composition between the value of Stats.type and .txt. Both files must be saved in the _Resources directory:
/_Resources/Iteration.txt
/_Resources/Recursion.txt
Both files must have identical contents and the information needs to be stored in conformity with the format:
sequence: value.
Requirements
- You may not change or modify existing code, only add to stated entities. This includes the Stats structure which should not contain any methods or overloaded constructors / operators!
- You may create additional functions as deemed necessary.
- You may use as many variables and constants as you wish for the assignment, but variables may only be passed between entities as arguments and return values. Global variables will never be allowed!
- Each Fibonacci sequence calculated needs to be stored in std::vector<long_type> values in relevant Stats object.
- All members of Stats needs to have intended values.
- Every 5th Fibonacci sequence calculated must be printed to the screen in accordance to stated requirements.
- std::chrono must be used to measure time spent for executions.
- Each and every entity (function / struct) needs to have its responsibilities documented, for functions this also includes its full specification (parameters & return value). Place such comments together with declarations (prototypes) in header files.
- Any comments describing implementation needs to be placed in source files together with relevant definition.
- main() may not contain any code besides calling mainMenu():
int main() {
mainMenu();
return 0;
}
That is it! I thought I'd divide it into smaller pieces, but maybe it was stupid not to show you the full assignment!