Vectors and Loops

0 maranda miller · May 1, 2015
Any help would be appreciated
this is the assignment
Write a main function that does the followings:
--- Create an empty vector of Student class 
--- Add 4 Student objects to the vector using the push_back() method.
--- Use a loop to show GPAs of 4 objects.
--- Insert a new Student object in the middle of the vector.
--- Use a loop to show GPAs of 5 objects.
and i did two different types both of these codes compile but i know i am missing a few things for the assignment like the loops. I was just wondering which code i would use and if i am on the right track. also how would i add in the loops? 
code 1 
#include <string>
#include <vector>
using namespace std;

int main()

{
vector<string> s;          // declare an empty vector of string.
s.push_back("mathScore");      // add a new element at the end.
s.push_back("chemScore");
s.push_back("engScore");
s.push_back("bioScore");

vector<string>::iterator it;         // a regular iterator, can change an element’s value.
vector<string>::const_iterator cit;  // a constant iterator, cannot change an element’s value.
// begin() refers to the first element.
// end() refers to the position past the last element.  
cout << endl;
for (it = s.begin(); it != s.end(); ++it)
  cout << *it << endl;    // dereferencing a pointer.
  cout << endl;
it = s.begin();    // a regular iterator that points to the first element.
*it = "artScore";       // change the element’s value.
 cout << endl;
 for (it = s.begin(); it != s.end(); ++it)
 cout << *it << endl;
 cout << endl;  
 it = s.begin();
   cout << s.size()     << endl;   // num of elements in a vector.
   cout << (*it).size() << endl;   // num of characters in an elements (pointer notation).
   cout << it->size()   << endl;   // num of characters in an elements (another notation).
   cout << endl;
s.insert(s.begin(), "gymScore");   // insert an element at the beginning.
    for (it = s.begin(); it != s.end(); ++it)
    cout << *it << endl;
s.erase((s.begin() + 2));   // erase an element at a position.
    cout << endl;
    for (it = s.begin(); it != s.end(); ++it)
    cout << *it << endl;
return 0;
}


code 2
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Student
{
    protected:
        int ID;
        float mathScore;
        float chemScore;
    public:
        Student (int = 0, float = 0.0f, float = 0.0f);
        void getGPA();
};

Student::Student (int id, float ms, float cs)
{
    ID = id;
    mathScore = ms;
    chemScore = cs;
}

void Student::getGPA ()
{
    float gpa = (mathScore + chemScore)/2.0;
    cout << "ID = " << ID << " PGA = " << gpa << endl;
}
       
int main()

{
  vector<Student> s;                      
  s.push_back(Student(2, 2));      
  s.push_back(Student(3, 3));  
  s.push_back(Student(4, 4));
  vector<Student>::iterator it;        
  cout << endl;
  for (it = s.begin(); it != s.end(); ++it)
{
cout << (*it).computStudentGpa() << endl;  
//cout << it->computeStudentGpa()   << endl;      
}
cout << endl;
it = s.begin();    
*it = Student(1, 1);        
cout << endl;
for (it = s.begin(); it != s.end(); ++it)
{
 cout << (*it).computeStudentGpa() << endl;  
//cout << it->computeStudentGpa()   << endl;      
}
s.insert(s.begin(), Student(6,6));
cout << endl;
for (it = s.begin(); it != s.end(); ++it)
{
cout << (*it).computeStudentGpa () << endl;  
//cout << it->computeStudentGpa ()   << endl;    
}
s.erase((s.begin() + 2));    
cout << endl;
for (it = s.begin(); it != s.end(); ++it)
{
cout << (*it).computeStudentGpa () << endl;  
//cout << it->computeStudentGpa ()   << endl;      
}
return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Dol Lod · May 1, 2015
Wasn't there a project like this posted a few days ago or last week? Seriously ...
0 maranda miller · May 1, 2015
SI just wanted some help on it but never mind 
0 Matthew Cushing · May 2, 2015
I'll help you out when I get off work if you still need it! Just let me know.
  • 1

C++

107,339 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator