Hi there, I have a hard time creating this code . May I ask for help please ? I
ID: 3733433 • Letter: H
Question
Hi there,
I have a hard time creating this code . May I ask for help please ? I need the whole code with the extra credit part .
The question is :
You are going to create your own classroom! Write a program and ask the user how many students they want in your classroom.
The program should then ask the user for the name, address (street and city), and age for each student in the classroom.
You should then sort the records by age and then separately print out the students names and addresses from youngest to oldest. Then print out the average age of the class.
You must do the following:
Use a nested struct like this:
struct Address
{
char sStreet[255];
char sCity[255];
};
struct Student
{
char sName[255];
Address address;
int nAge;
};
Create a dynamically allocated array of these structs to hold the data (using new and delete [])
Your program must use three functions that accept the array of Studentstructs by address (i.e., pass a Student pointer):
void initializeData(Student * pStudents, int size)
void sortData(Student * pStudents, int size)
void displayData(Student * pStudents, int size)
As usual, you must provide sample output demonstrating your program works correctly and submit via Canvas.
Sample output:
Jane, 123 East St., Chicago, age 15
Robert, 34 eagle st., Iselin, age 20
Mark, 213 deamon ave., Miami, age 25
Average age is 20.
Extra credit: (15 points)
Do not use pre-allocated char arrays for the names, streets and city variables, but use char * and allocated and de-allocate the memory when needed, ie.
struct Address
{
char * pStreet;
char * pCity;
};
struct Student
{
char * pName;
Address address;
int nAge;
};
Explanation / Answer
//Initial Solution
#include <iostream>
using namespace std;
// structures as defined by the question
struct Address{
char sStreet[255];
char sCity[255];
};
struct Student {
char sName[255];
Address address;
int nAge;
};
//various functions
void initializeData(Student *pStudents, int size);
void sortData(Student *pStudents, int size);
void displayData(Student *pStudents, int size);
//main method
int main(){
cout<<"Enter the number of students: ";
int size;
cin>>size;
Student *pStudents = new Student[size]; //dynamically allocating memory
initializeData(pStudents,size); //calling function to input values
return 0;
}
void initializeData(Student *pStudents, int size){
int i;
for(i=0;i<size;i++){
cin.ignore(); //to skip so that string can be input next;
cout<<"Enter Student Name: ";
gets(pStudents[i].sName);
cout<<"Enter Street Name: ";
gets(pStudents[i].address.sStreet);
cout<<"Enter City Name: ";
gets(pStudents[i].address.sCity);
cout<<"Enter Age: ";
cin>>pStudents[i].nAge;
}
sortData(pStudents,size); //calling Sorting function
}
void sortData(Student *pStudents, int size){
//Sorting the data using Bubble sort on the basis of age
int i,j;
for(i=0;i<size-1;i++){
for(j=0;j<size-i-1;j++){
if(pStudents[j].nAge > pStudents[j+1].nAge){
Student temp = pStudents[j];
pStudents[j] = pStudents[j+1];
pStudents[j+1] = temp;
}
}
}
displayData(pStudents,size); //calling print function
}
void displayData(Student *pStudents, int size){
int i;
float average_age = 0; //varible to calculate average age
for(i=0;i<size;i++){
cout<<pStudents[i].sName<<", "<<pStudents[i].address.sStreet<<", "<<pStudents[i].address.sCity<<", age "<<pStudents[i].nAge<<endl;
average_age += pStudents[i].nAge; //adding ages of all students
}
average_age = float(average_age)/size; //dividing with number of students to obtain average
cout<<"Average age is "<<average_age<<".";
//deallocating memory using delete
delete []pStudents;
}
//Bonus Solution
#include <iostream>
using namespace std;
// structures as defined by the question
struct Address{
char *pStreet;
char *pCity;
};
struct Student {
char *pName;
Address address;
int nAge;
};
//various functions
void initializeData(Student *pStudents, int size);
void sortData(Student *pStudents, int size);
void displayData(Student *pStudents, int size);
//main method
int main(){
cout<<"Enter the number of students: ";
int size;
cin>>size;
//dynamically allocating memory
Student *pStudents = new Student[size];
int i;
for(i=0;i<size;i++){
pStudents[i].pName = new char[255];
pStudents[i].address.pStreet = new char[255];
pStudents[i].address.pCity = new char[255];
}
initializeData(pStudents,size); //calling function to input values
return 0;
}
void initializeData(Student *pStudents, int size){
int i;
char *temp = new char[255];
for(i=0;i<size;i++){
cin.ignore(); //to skip so that string can be input next;
cout<<"Enter Student Name: ";
gets(pStudents[i].pName);
cout<<"Enter Street Name: ";
gets(pStudents[i].address.pStreet);
cout<<"Enter City Name: ";
gets(pStudents[i].address.pCity);
cout<<"Enter Age: ";
cin>>pStudents[i].nAge;
}
sortData(pStudents,size); //calling Sorting function
}
void sortData(Student *pStudents, int size){
//Sorting the data using Bubble sort on the basis of age
int i,j;
for(i=0;i<size-1;i++){
for(j=0;j<size-i-1;j++){
if(pStudents[j].nAge > pStudents[j+1].nAge){
Student temp = pStudents[j];
pStudents[j] = pStudents[j+1];
pStudents[j+1] = temp;
}
}
}
displayData(pStudents,size); //calling print function
}
void displayData(Student *pStudents, int size){
int i;
float average_age = 0; //varible to calculate average age
for(i=0;i<size;i++){
cout<<pStudents[i].pName<<", "<<pStudents[i].address.pStreet<<", "<<pStudents[i].address.pCity<<", age "<<pStudents[i].nAge<<endl;
average_age += pStudents[i].nAge; //adding ages of all students
}
average_age = float(average_age)/size; //dividing with number of students to obtain average
cout<<"Average age is "<<average_age<<".";
//deallocating memory using delete
delete []pStudents->pName;
delete []pStudents->address.pStreet;
delete []pStudents->address.pCity;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.