78 Introduction to C++ Programming and Graphics
}return 0;
}The internal C++ functionrandgenerates random integers ranging from 0 up
to the maximum value ofRANDMAX. Converting these integers to real numbers
and normalizing by the maximum generates the requisite list. The output of
the code is:
1 0.84019
2 0.39438
3 0.78310
4 0.79844
5 0.91165
6 0.98981Student grades
In the third application, we discuss a code contained in the filegrades.cc
that reads student names and grades from filegrades.dat, and prints them nicely
formatted on the screen:
#include <iomanip>
#include <fstream>
using namespace std;int main()
{
ifstream file2("grades.dat");string lastname[201], firstname[201]; // 200 students max
float grade[201][11]; // 10 grades maxint i=1; // assume one student/*------loop over students----------------*/while(file2 >> lastname[i]) // read the last name
{
cout << setw(3) << right << i <<"";file2 >> firstname[i]; // read the first namecout << setw(15) << left << lastname[i]+" "+firstname[i] << " ";int j=1;