C++ exercise - using typedef

Hello everyone.

I am doing some C++ exercises and got stuck with one.
I have written 1 version of the program to use vectors to store values, and the other to use lists to store values.
This is the next exercise:
By using a typedef, we can write one version of the program that implements either a vector-based solution or a list-based one.
Write and test this version of the program.


v1. If I understand correctly I am supposed to write a program to declare a type that works the same with lists and vectors.

v2. The other easy version would be that I am supposed to declare shorter declaration names with typedef for example for lists of my list version of the program. And same to my vector based program.

Can anyone help me to understand it right?

Here is the list version of the program(vector based is the same only vectors instead of lists).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <ios>
#include <iomanip>
#include <list>
#include <cstdlib>
using namespace std;
//Data structure for holding students name and final grade
struct Student_info
{
	string name;
	double final;
};
//calculate and return the average from list of homework grades
double average(list<double>& hw)
{
	double sum = 0;
	list<double>::size_type size = hw.size();
	list<double>::iterator i = hw.begin();
	for(i; i != hw.end(); i++){
		sum += *i;
	}	
	return (sum/size);
}
//calculate and return the final grade of a student final exam weights 40%, midterm 20%, homeworks 40%
double grade(list<double>& hw, double midterm, double final)
{
	return final*0.4+midterm*0.2+average(hw);
}
//read homework grades from the input and store them in a list
istream& read_hw(istream& in, list<double>& hw)
{
	if(in){
		hw.clear();
		double x;
		while(in >> x){
			hw.push_back(x);		
		}
		in.clear();
	}
	return in;
}
//read students name, midterm, final and homework grades
istream& read(istream& in, Student_info& s)
{
	list<double> hw;
	double midterm, final;
	in >> s.name >> midterm >> final;	
	read_hw(in, hw);
	s.final = grade(hw,midterm,final);
	return in;
}
//print out stored data
void print_records(list<Student_info>& stud)
{
	streamsize prec = cout.precision();
	list<Student_info>::iterator i = stud.begin();
	for(i; i != stud.end(); i++){
		cout << (*i).name << "  " << setprecision(3) << (*i).final << endl << setprecision(prec);
	}	
}
int main()
{
	Student_info s; 
	list<Student_info> stud;
	while(read(cin,s)){		
		stud.push_back(s);
	}
	print_records(stud);
    	return 0;
}


Thank you in advance.
Last edited on
I believe what they mean is to use a typedef like this:

1
2
3
4
5
6
7
8
//...
typedef std::vector<int> storage_type;

int main() {
   storage_type information;
   //do stuff...
   return 0;
}
Last edited on
Topic archived. No new replies allowed.