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 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...
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....
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);...
use repil.it edit my code please i already did all part but need to edit more...
use repil.it edit my code please i already did all part but need to edit more its run for some not shwing all intro to C-programin be sure to edit on my code very basic and add good comments thank you 1-Write a program that requests 5 integers from the user and stores them in an array. You may do this with either a for loop OR by getting a string from stdin and using sscanf to store formatted input...
I need code written in java for one of my projects the instructions are Write a...
I need code written in java for one of my projects the instructions are Write a program that interacts with the user via the console and lets them choose options from a food menu by using the associated item number. It is expected that your program builds an <orderString> representing the food order to be displayed at the end. (See Sample Run Below). Please note: Each student is required to develop their own custom menus, with unique food categories, items...
Here is my java code. It works and has the correct output, but I need to...
Here is my java code. It works and has the correct output, but I need to add a file and I am not sure how. I cannot use the FileNotFoundException. Please help! import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,0,0}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: "...
Hello, I need the Matlab code of the Fourier Transform without using the Matlab functions fft...
Hello, I need the Matlab code of the Fourier Transform without using the Matlab functions fft and dft. Applied to discrete signals. If you can with an example.Thank you!!
I need a matlab code for my presentation. Code should include Single Sideband Amplitude Modulation and...
I need a matlab code for my presentation. Code should include Single Sideband Amplitude Modulation and Demodulation. It should figure 3 things: -time domain of given message signal in Amplitude Modulation and Single Sideband Amplitude Modulation -frequency domain of given signal in Amplitude Modulation and Single Sideband Amplitude Modulation -And it should demodulate and show message signal.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT