Question

In: Computer Science

Hello, I need to divide my code in this different parts. Use a standard approach to...

Hello, I need to divide my code in this different parts.

Use a standard approach to source files in this assignment: a .cpp for each function (you have at least two – main and ageCalc) a header file for the student struct, properly guarded

Code:

#include <iostream>

#include <string>

#include <fstream>

#include <algorithm>

#include <vector>

#include <iomanip>

using namespace std;

#define nullptr NULL

#define MAX_SIZE 100

struct Student

{

    string firstName;

    char middleName;

    string lastName;

    char collegeCode;

    int locCode;

    int seqCode;

    int age;

};

struct sort_by_age

{

    inline bool operator()(const Student *s1, const Student *s2)

    {

        return (s1->age < s2->age);

    }

};

int ageCalc(Student *studs[], Student *&youngest);

int main()

{

    Student *students[MAX_SIZE] = {nullptr};

    ifstream Myfile;

    Myfile.open("a2data.txt");

    if (!Myfile.is_open())

    {

        cout << "Could not open the file!";

        return 1;

    }

    vector<string> words;

    string line;

    while (!Myfile.eof())

    {

        getline(Myfile, line);

        line.c_str();

        int i = 0;

        string word = "";

        while (line[i] != '\0')

        {

            if (line[i] != ' ')

            {

                word = word + line[i];

                i++;

            }

            else

            {

                if (word != "")

                    words.push_back(word);

                word = "";

                i++;

            }

        }

        words.push_back(word);

    }

    Myfile.close();

    int count = 0;

    string fname = words.at(count);

    count++;

    int n = 0;

    while (count < words.size() - 2)

    {

        Student *s = new Student;

        s->firstName = fname;

        string mname = words.at(count);

        s->middleName = mname[0];

        count++;

        s->lastName = words.at(count);

        if (words.at(count).size() >= 12)

        {

            if (words.at(count)[1] >= '0' && words.at(count)[1] <= '9')

            {

                count--;

                s->middleName = ' ';

                s->lastName = words.at(count);

            }

        }

        count++;

        string id = words.at(count);

        count++;

        s->collegeCode = id[0];

        string loc = "";

        loc = loc + id[1];

        loc = loc + id[2];

        s->locCode = stoi(loc);

        string seq = "";

        for (int j = 3; j < 9; j++)

        {

            seq = seq + id[j];

        }

        s->seqCode = stoi(seq);

        string age = "";

        age = age + id[9];

        age = age + id[10];

        age = age + id[11];

        s->age = stoi(age);

        fname = id.erase(0, 12);

        students[n] = s;

        n++;

    }

    words.clear();

    sort(students, students + n, sort_by_age());

    cout << setw(15) << left << "Last Name";

    cout << setw(15) << left << "Midlle Name";

    cout << setw(15) << left << "First Name";

    cout << setw(15) << left << "College Code";

    cout << setw(12) << left << "Location";

    cout << setw(12) << left << "Sequence";

    cout << setw(12) << left << "Age" << endl;

    cout << "=======================================================================================" << endl;

    for (int i = 0; i < n; i++)

    {

        cout << setw(15) << left << students[i]->lastName;

        cout << setw(15) << left << students[i]->middleName;

        cout << setw(20) << left << students[i]->firstName;

        cout << setw(13) << left << students[i]->collegeCode;

        cout << setw(10) << left << students[i]->locCode;

        cout << setw(12) << left << students[i]->seqCode;

        cout << students[i]->age << endl;

    }

    cout << endl;

    Student *youngest = NULL;

    int avg_age = ageCalc(students, youngest);

    cout << "Average age is: " << avg_age << endl;

    cout << "Youngest student is " << youngest->firstName << " " << youngest->middleName << " " << youngest->lastName << endl;

    return 0;

}

int ageCalc(Student *studs[], Student *&youngest)

{

    youngest = studs[0];

    int i = 0;

    int avg_age = 0;

    while (studs[i] != NULL)

    {

        avg_age += studs[i]->age;

        if (youngest->age > studs[i]->age)

            youngest = studs[i];

        i++;

    }

    return avg_age / i;

}

File needed:

https://drive.google.com/file/d/15_CxuGnFdnyIj6zhSC11oSgKEYrosHck/view?usp=sharing

Solutions

Expert Solution

//data.cpp

#include<iostream>
using namespace std;

struct Student

{

string firstName;

char middleName;

string lastName;

char collegeCode;

int locCode;

int seqCode;

int age;

};

struct sort_by_age

{

inline bool operator()(const Student *s1, const Student *s2)

{

return (s1->age < s2->age);

}

};

//readFile.cpp

#include<iostream>
#include <vector>
#include <fstream>
using namespace std;
void readFile(vector<string> &words){
  
  
ifstream Myfile;

Myfile.open("a2data.txt");

if (!Myfile.is_open())

{

cout << "Could not open the file!";

return ;

}
   string line;

while (!Myfile.eof())

{

getline(Myfile, line);

line.c_str();

int i = 0;

string word = "";

while (line[i] != '\0')

{

if (line[i] != ' ')

{

word = word + line[i];

i++;

}

else

{

if (word != "")

words.push_back(word);

word = "";

i++;

}

}

words.push_back(word);

}

Myfile.close();
}

//ageCalc.cpp

#include"data.cpp"

int ageCalc(Student *studs[], Student *&youngest)

{

youngest = studs[0];

int i = 0;

int avg_age = 0;

while (studs[i] != NULL)

{

avg_age += studs[i]->age;

if (youngest->age > studs[i]->age)

youngest = studs[i];

i++;

}

return avg_age / i;

}

//main.cpp

#include <string>
#include <algorithm>
#include "readFile.cpp"
#include"ageCalc.cpp"
#include <iomanip>
#define nullptr NULL
#define MAX_SIZE 100


int ageCalc(Student *studs[], Student *&youngest);

int main()

{

Student *students[MAX_SIZE] = {nullptr};


vector<string> words;

readFile(words);

int count = 0;

string fname = words.at(count);

count++;

int n = 0;

while (count < words.size() - 2)

{

Student *s = new Student;

s->firstName = fname;

string mname = words.at(count);

s->middleName = mname[0];

count++;

s->lastName = words.at(count);

if (words.at(count).size() >= 12)

{

if (words.at(count)[1] >= '0' && words.at(count)[1] <= '9')

{

count--;

s->middleName = ' ';

s->lastName = words.at(count);

}

}

count++;

string id = words.at(count);

count++;

s->collegeCode = id[0];

string loc = "";

loc = loc + id[1];

loc = loc + id[2];

s->locCode = stoi(loc);

string seq = "";

for (int j = 3; j < 9; j++)

{

seq = seq + id[j];

}

s->seqCode = stoi(seq);

string age = "";

age = age + id[9];

age = age + id[10];

age = age + id[11];

s->age = stoi(age);

fname = id.erase(0, 12);

students[n] = s;

n++;

}

words.clear();

sort(students, students + n, sort_by_age());

cout << setw(15) << left << "Last Name";

cout << setw(15) << left << "Midlle Name";

cout << setw(15) << left << "First Name";

cout << setw(15) << left << "College Code";

cout << setw(12) << left << "Location";

cout << setw(12) << left << "Sequence";

cout << setw(12) << left << "Age" << endl;

cout << "=======================================================================================" << endl;

for (int i = 0; i < n; i++)

{

cout << setw(15) << left << students[i]->lastName;

cout << setw(15) << left << students[i]->middleName;

cout << setw(20) << left << students[i]->firstName;

cout << setw(13) << left << students[i]->collegeCode;

cout << setw(10) << left << students[i]->locCode;

cout << setw(12) << left << students[i]->seqCode;

cout << students[i]->age << endl;

}

cout << endl;

Student *youngest = NULL;

int avg_age = ageCalc(students, youngest);

cout << "Average age is: " << avg_age << endl;

cout << "Youngest student is " << youngest->firstName << " " << youngest->middleName << " " << youngest->lastName << endl;

return 0;

}


Related Solutions

Hello, I need to divide my code in this different parts. Use a standard approach to...
Hello, I need to divide my code in this different parts. Use a standard approach to source files in this assignment: a .cpp for each function (you have at least two – main and ageCalc) a header file for the student struct, properly guarded Code: #include <iostream> #include <string> #include <fstream> #include <algorithm> #include <vector> #include <iomanip> using namespace std; #define nullptr NULL #define MAX_SIZE 100 struct Student {     string firstName;     char middleName;     string lastName;     char collegeCode;     int locCode;     int...
Hello, I need to divide my code in this different parts. Use a standard approach to...
Hello, I need to divide my code in this different parts. Use a standard approach to source files in this assignment: a .cpp for each function (you have at least two – main and ageCalc) a header file for the student struct, properly guarded Code: #include <iostream> #include <string> #include <fstream> #include <algorithm> #include <vector> #include <iomanip> using namespace std; #define nullptr NULL #define MAX_SIZE 100 struct Student {     string firstName;     char middleName;     string lastName;     char collegeCode;     int locCode;     int...
Hello, I need to divide my code in this different parts. Use a standard approach to...
Hello, I need to divide my code in this different parts. Use a standard approach to source files in this assignment: a .cpp for each function (you have at least two – main and ageCalc) a header file for the student struct, properly guarded Code: #include <iostream> #include <string> #include <fstream> #include <algorithm> #include <vector> #include <iomanip> using namespace std; #define nullptr NULL #define MAX_SIZE 100 struct Student {     string firstName;     char middleName;     string lastName;     char collegeCode;     int locCode;     int...
Hello I need a small fix in my program. I need to display the youngest student...
Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...
Hello, I need an expert answer for one of my JAVA homework assignments. I will be...
Hello, I need an expert answer for one of my JAVA homework assignments. I will be providing the instructions for this homework below: For this lab, you will write the following files: AbstractDataCalc AverageDataCalc MaximumDataCalc MinimumDataCalc As you can expect, AbstractDataCalc is an abstract class, that AverageDataCalc, MaximumDataCalc and MinimumDataCalc all inherit. We will provide the following files to you: CSVReader A simple CSV reader for Excel files DataSet This file uses CSVReader to read the data into a List>...
C++ Hello .I need to convert this code into template and then test the template with...
C++ Hello .I need to convert this code into template and then test the template with dynamic array of strings also if you can help me move the function out of the class that would be great.also There is a bug where the memory was being freed without using new operator. I cant seem to find it thanks in advance #include using namespace std; class DynamicStringArray {    private:        string *dynamicArray;        int size;    public:   ...
Hello, I need to do a research and I choose my research question: Do the commercialization...
Hello, I need to do a research and I choose my research question: Do the commercialization and use of civilian drones respect our individual freedoms and privacy? Can you help me with ideas of Research Methodology including: 1. Restatement of research tasks: hypothesis or research questions; 2. Study population and sampling: description of study areas (where? why?), populations and the procedures for the sample selection; 3. Data collection: description of the tools (i.e. the survey instrument) and methods used (i.e....
Hello! I need to present my xml file on the Windows form application I tried but...
Hello! I need to present my xml file on the Windows form application I tried but it dosent work at button 2, I cann't show Xml_file. What is problem here please? have you any suggest or solution . Here is my form code! namespace MyFirstWindos { public partial class Form1 : Form { const string XML1 = "path"; const string XML2 = "Path"; const string ResultFile = "Path"; public Form1() { InitializeComponent(); } private void Run(object sender, EventArgs e)//run button...
I have a problem with the code for my project. I need the two clients to...
I have a problem with the code for my project. I need the two clients to be able to receive the messages but the code I have done only the server receives the messages. I need the clients to be able to communicate with each other directly not throught the server. The requirements of this "local chat" program must meet are: 1. The chat is performed between 2 clients and a server. 2. The server will first start up and...
This is my code I need to complete it? //The code package arraylists; import java.util.ArrayList; import...
This is my code I need to complete it? //The code package arraylists; import java.util.ArrayList; import java.util.Scanner; /** * * */ public class SoftOpening {    public static void main(String[] args) {       Scanner input = new Scanner(System.in);    ArrayList foodList = generateMenu();    User user = generateUser(input); user.introduce();    userBuyFood(foodList, user, input); user.introduce(); } public static ArrayList generateMenu(){    ArrayList foodList = new ArrayList<>();    Food pizza1 =new Food(1,"pizza","Seafood",11,12); Food pizza2 =new Food(2,"pizza","Beef",9,10); Food Friedrice =new Food(3,"fried rice","Seafood",5,12);...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT