Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use the following program code/definitions in Exercises 1, 2, and 3. You may def

ID: 3541552 • Letter: U

Question

Use the following program code/definitions in Exercises 1, 2, and 3. You may define any other variables that you need.


#include <fstream>
using namespace std;
int main ( )
{
int scores[100]; // Array containing students scores
bool passing[100]; // Boolean array
int length = 0; // Holds the actual number of students //in a course
}


1. (10 points)
Write the lines of the main() function that define an input data file, named infile, and open the file (which physically exists in your folder as lab1data.txt).


2. (5 points)
Now, write a void function, named GetData(), that takes infile, scores and length (from exercise 1) as parameters and reads the contents of infile into the scores array (while keeping a running count of the number of values stored in the array with the length variable). (NOTE: There are between 1 and 100 values in lab1data.txt. You do not know exactly how many.)
(NOTE 2: After calling this function, main() will need the filled array and the length.)


3. (10 points)
Write a C++ function, named ProcessPass(), that takes passing, scores, and length as parameters and sets the elements of passing to TRUE whenever the parallel value of scores is 60 or above.
Test your function in main().
data-mce-bogus="1">

Explanation / Answer


#include<iostream>
#include <fstream>
using namespace std;
void GetData(ifstream& infile,int scores[100],int& length)
{
while(!infile.eof())
infile >> scores[length++];
}
void ProcessPass(bool passing[100],int scores[100],int length)
{
for(int i=0; i<length; i++)
passing[i] = (scores[i]>=60);
}
int main ( )
{
int scores[100]; // Array containing students scores
bool passing[100]; // Boolean array
int length = 0; // Holds the actual number of students //in a course
ifstream infile("lab1data.txt");
if(!infile)
{
cout << "unable to open file lab1data.txt, so exiting from program" <<endl;
return 0;
}
GetData(infile,scores,length);
ProcessPass(passing,scores,length);
for(int i=0; i<length; i++)
{
if(passing[i])
cout << "roll no " << (i) << " passed " << endl;
else
cout << "roll no " << (i) << " failed " << endl;
}
return 0;
}