Help Please - C++

closed account (NC5fSL3A)
Hi every one,

I have assignment that I am struggling with. Can anybody help me?

The Purpose of the assignment: To evaluate the performance of the vector and list containers of STL for various operations (integer values). In particular, I have to measure the CPU time required for each container to perform the following operations:

(i) Insert all the integers from 0 to 999,999 (one by one) into the back of the container.
(ii) Find the element with value 100,000 and place an iterator to point to that element (you have to manually find the element even for the vector container).
(iii) Erase the element found in part (ii), using the erase function.

For each of the three tasks, output on the screen the CPU time (in seconds) required to complete the task for the two
different containers. In particular, perform each experiment 100 times, and output the average times (don't forget to
clear the containers before each run). Comment on the results and also include a screenshot of the output in your
report.

I have Started with the project and this is what I have so far

Vector Case:

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
#include <iostream>
#include <vector>       

using namespace std;

int main()
{
    time_t start,stop;
    cout << "VECTOR CASE" << endl;
    cout << endl;
    cout << "Run Times" << "\t" << "Insertion" << endl;
    
    cout << endl;
   	int counter = 100;       //variable counter determines how many tests will be run


	for(int x=1; x<=counter; x++)
    { 
            
    vector<int> my_vector;  //create a vector of integers
    start=clock();
    
    // Insert element from 0 to 999,999 to the back of the vector
    for( int i = 0; i < 1000000; i++ ) {
         my_vector.push_back(i);
         }
    stop=clock(); 
    
    cout << x << "\t" << "\t" << double(stop-start)/CLOCKS_PER_SEC << endl; // print the time for each run
    
    my_vector.clear();  // clear vector
    }
    
    system("pause");
    return 0;
}


For the List Case:

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
#include <iostream> 
#include <list>      

using namespace std;

int main()
{
    time_t start,stop;
    cout << "LIST CASE" << endl;
    cout << endl;
    cout << "Run Times" << "\t" << "Insertion" << endl;
    
    cout << endl;
   	int count = 100;       //variable timesrun determines how many tests will be run
   	
    // initialize a list of integers
	for(int y=1; y<=count; y++)
    { 
            list<int> list1;
            list<int>::iterator position;
            
            // insert the elements of the list from 0 to 999999
            start=clock();
            for( int i = 0; i < 1000000; i++ ) {
            list1.push_back(i);
            }
            stop=clock();
            cout << y << "\t" << "\t" << double(stop-start)/CLOCKS_PER_SEC << endl;
            list1.clear();
    }
    system("pause");
    return 0;
}


I have been able to insert the values from 0 to 999999, experiment 100 times and print out the time used for each experiment.

Thanks! Your help is greatly appreciated.
closed account (NC5fSL3A)
Ok so I am not quiet sure but I believe I figured out most part except for calculating the average time.

My updated codes look like this:

------------------------------------------------------------------------------------------------------------------------
Vector Case:

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
#include <iostream>
#include <vector>       

using namespace std;

int main()
{
    cout << "VECTOR CASE" << endl;
    cout << endl;
    cout << "Run Times" << "\t" << "Insertion" << "\t" << "Finding 100000" << "\t" << "\t" << "Removing 100000" << endl;
    
    cout << endl;
   	int counter = 100; //variable counter determines how many tests will be run


	for(int x=1; x<=counter; x++)
    { 
            
    vector<int> my_vector;
    time_t start,stop;  //create a vector of integers
    start=clock();
    
    // Insert element from 0 to 999,999 to the back of the vector
    for( int i = 0; i < 1000000; i++ ) {
         my_vector.push_back(i);
         }
    stop=clock();
    double insert_time = double(stop-start)/CLOCKS_PER_SEC;
    
    // Find element with value 100,000
    vector<int> v;
    v.assign( my_vector.begin(), my_vector.end() ); 
    time_t begin, end;
    begin=clock();
    vector<int>::iterator iter1 = v.begin();
    while( iter1 != v.end() && *iter1 != 100000 ) {
    iter1++;
    } 
    end=clock();
    double find_time = double(end-begin)/CLOCKS_PER_SEC;
    
    // Remove element with value 100,000
    time_t time1, time2;
    time1=clock();
    my_vector.erase( my_vector.begin()+100000);
    time2=clock();
    double remove_time = double(time2-time1)/CLOCKS_PER_SEC;
    
    cout << x << "\t" << "\t" << insert_time << "\t" << "\t" << find_time << "\t" << "\t" << "\t" << remove_time << endl; // print the time for each run
    my_vector.clear();  // clear vector
    } 

    system("pause");
    return 0;
}



------------------------------------------------------------------------------------------------------------------------
List Case

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
#include <iostream> 
#include <list>      

using namespace std;

int main()
{
    cout << "LIST CASE" << endl;
    cout << endl;
    cout << "Run Times" << "\t" << "Insertion" << "\t" << "Finding 100000" << "\t" << "\t" << "Removing 100000" << endl;
    
    cout << endl;
   	int count = 100;       //variable timesrun determines how many tests will be run
   	
    // initialize a list of integers
	for(int y=1; y<=count; y++)
    { 
            list<int> list1;
            list<int>::iterator position;
            
            // insert the elements of the list from 0 to 999999
            time_t start,stop;
            start=clock();
            for( int i = 0; i < 1000000; i++ ) {
            list1.push_back(i);
            }
            stop=clock();
            double insert_time = double(stop-start)/CLOCKS_PER_SEC;
            
            //Find Element with value 100,000
            time_t begin, end;
            begin=clock();
            position = find (list1.begin(), list1.end(), 100000);
            end=clock();
            double find_time = double(end-begin)/CLOCKS_PER_SEC;
            
            //Erase Element with value 100,000
            time_t time1, time2;
            time1=clock();            
            list1.erase(position);
            time2=clock();
            double remove_time = double(time2-time1)/CLOCKS_PER_SEC;
            
            
            cout << y << "\t" << "\t" << insert_time << "\t" << "\t" << find_time << "\t" << "\t" << "\t" << remove_time << endl; // print the time for each run
            list1.clear();
    }
    system("pause");
    return 0;
}



I would like to know if you think that I did these parts properly at least. What would be the best approach to evaluate the average time in those cases.
Last edited on
Topic archived. No new replies allowed.