I have this code, but it gives errors which are \'{\': missing function header (
ID: 3770309 • Letter: I
Question
I have this code, but it gives errors which are '{': missing function header (old-style formal list?) AND expected a declaration
//
// Sample code to measure execution time in milliseconds
//
#include <iostream>
#include <ctime>
using namespace std;
void ghostFunction();
**** the errors are here. {
time_t t1; // time variable in milliseconds
t1 = clock(); // obtain current time in milliseconds
ghostFunction();
long int time = clock() - t1; // compute elapsed time
cout << "Time to execute function: " << time << " milliseconds ";
return 0;
}
//to do something of long duration
void ghostFunction()
{
double num = 33.66;
for (int i = 0; i < 1000000; i++)
num /= i;
}
Explanation / Answer
you have not written main() function.
the code shouldl be like this:
#include <iostream>
#include <ctime>
using namespace std;
void ghostFunction();
int main(){
time_t t1; // time variable in milliseconds
t1 = clock(); // obtain current time in milliseconds
ghostFunction();
long int time = clock() - t1; // compute elapsed time
cout << "Time to execute function: " << time << " milliseconds ";
return 0;
}
//to do something of long duration
void ghostFunction()
{
double num = 33.66;
for (int i = 0; i < 1000000; i++)
num /= i;
}