This is a C++ Program, please make sure it can run in visual studio. Thank you!!
ID: 3743127 • Letter: T
Question
This is a C++ Program, please make sure it can run in visual studio. Thank you!! Computer Science 385 Project No.1 (arrays, functions, file processing) 1. Write a program to do the following: i. Create the following text file data.txt and declaration Daniel 33 3.3 William 44 4.3 Kevin 19 2.91 Sarah17 3.11 Raymond 21 3.2 struct PERSON string name; int age float gpa; ii. Complete the following program by writing all functions to produce the given output. The display function is overloaded Int n-5; int main( Sample I/o This is the content of array a Name Age Gpa // copy data from file data.txt into array a // display array a // Display the name of teenager students PERSON a [n]; copyData("data.txt", a); displayData( a ); William..., 44 4.30 Kevin. 19.91 Sarah... 17. 3.11 Raymondr .21 3.20 This is the list of teenagers displayTeens( a) Kevint Sarah Their GPA average is:xx.xx Their AGE average is: xx.xx // Compute their gpa's average float gpaAve averageGpa( a, gpaAve); // Compute their age average float ageAve; ageAve- AverageAge( a; display(gpaAve, ageAve); system("pause"); // Display both averages Sample I/O Original array a: 9 175 0 Original array s: WINNERS NEVER QUIT Original array c:eo u ai //terminate program return 0;Explanation / Answer
Program
// ConsoleApplication7.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
struct PERSON
{
string name;
int age;
float gpa;
};
const int n = 5;
// Function prototypes
void copyData( string ,PERSON []);
void displayData(PERSON[]);
void displayTeens(PERSON[]);
float averageGpa(PERSON []);
float AverageAge(PERSON []);
void display(float,float);
int main()
{
// Variables
PERSON a[n];
float gpaAve,ageAve;
//Copy Data from file data.txt to array a
copyData("data.txt",a);
//Display Array a
cout<<" ===This is the content of Array a === ";
displayData(a);
//Display the name of teenager students
displayTeens(a);
//Compute their gpa's average
gpaAve=averageGpa(a);
//Compute their age average
ageAve=AverageAge(a);
//Display both averages
display(gpaAve,ageAve);
system("PAUSE");
//Terminate program
return 0;
}
void copyData( string inFileName,PERSON a[])
{
ifstream inFile;
//Open the file
//inFile.open(inFileName);
inFile.open(inFileName.c_str());
//Input validation
if (!inFile)
{
cout << "Error to open file." << endl;
cout << endl;
}
// Read data into an array of structs
for(int count = 0; count < n; count++)
{
inFile >> a[count].name >> a[count].age >> a[count].gpa ;
}
// Close input file
inFile.close();
}
void displayData(PERSON a[])
{
cout<<" " "Name" " " "Age" " " "GPA" ;
cout<<" -----------------------";
for(int count = 0; count < n; count++)
{
cout<<" "<< a[count].name <<" "<< a[count].age <<" "<< a[count].gpa ;
}
}
void displayTeens(PERSON a[])
{
cout<<" This is the list of teenagers : " ;
for(int count = 0; count < n; count++)
{
if((a[count].age>12)&&(a[count].age<20))
cout<< a[count].name <<" " ;
}
}
float averageGpa(PERSON a[])
{
float sum=0.0,avg;
int i=0;
for(int count = 0; count < n; count++)
{
if((a[count].age>12)&&(a[count].age<20))
{
sum+=a[count].gpa;
i++;
}
}
avg=sum/i;
return avg;
}
float AverageAge(PERSON a[])
{
float sum=0.0,avg;
int i=0;
for(int count = 0; count < n; count++)
{
if((a[count].age>12)&&(a[count].age<20))
{
sum+=a[count].age;
i++;
}
}
avg=sum/i;
return avg;
}
void display(float gpaAve,float ageAve)
{
cout<<" Their GPA average is :"<<gpaAve ;
cout<<" Their age average is :"<<ageAve ;
}
Output
===This is the content of Array a ===
Name Age GPA
-----------------------
Daniel 33 3.3
William 44 4.3
Kevin 19 2.91
Sarah 17 3.11
Raymond 21 3.2
This is the list of teenagers :
Kevin Sarah
Their GPA average is :3.01
Their age average is :18
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.