cout << flush
#include <iostream>
using namespace std;
int main()
{
cout << "Got here ...";
int* A = 0;
A[0] = 25;
cout << " and now I'm here!" << endl;
return 0;
}
Got here ... will be printed out.
~/$ ./prog Segmentation fault (core dumped)
Q: What happened to the "Got here" message?
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
for(int i = 0; i < 10; i++)
{
cout << '*';
sleep(1);
}
cout << endl;
return 0;
}
When I run this, I expect the following:
However, I see nothing for 10 whole seconds, then all at once I see 10 *'s.
Q: What's happening?
When you write things with cout, the characters you write are "bufferred", that means stored temporarily until either enough have been collected to make writing to the screen worthwhile, or some event has occurred to cause the less-than-full buffer to be written to the screen despite having unused capacity.
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
for(int i = 0; i < 10; i++)
{
cout << '*' << flush; // NOTE HERE
sleep(1);
}
cout << endl;
return 0;
}
~/$ ./prob1
Problem p4 is hardest (ave = 48.5294%)
Here's the main function given for you.
int main()
{
int ns, np;
double** P = readGrades(&ns, &np);
double* A = getAveragePercentage(P,ns,np);
int imin = indexOfMin(A,np);
cout << "Problem p" << imin+1 << " is hardest (ave = " << A[imin] << "%)" << endl;
for(int i=0; i < ns; i++)
delete[] P[i];
delete [] P;
delete [] A;
return 0;
}
Define the functions.
Note: You may assume that for every problem, at least one student got full credit for that problem. If the average score for problem X as a percentage of the full credit score for X is less than the average score for problem Y as a percentage of the full credit score for Y, then problem X is "harder" than problem Y. Check out this solution.