various algorithms

Hello everyone, i have a c++ question. I need to resolve the issue i am having with 25000 elements. i keep getting too many file error. Also i need to know if this is the right way to time sorts. will this give me accurate data.here is part of my code. i can paste more code if it is necessary. thank you for your time and help.


Populating Arrays... <25000 elements>
Sorting
ERROR: There are too many items in the file. (ARRAY_SIZE = 25000)
SUMMARY RESULTS
Elapsed Time <BubbleSort> : 1303 milliseconds
ERROR: There are too many items in the file. (ARRAY_SIZE = 25000)
Elapsed Time <Selection Sort >: 733 milliseconds
ERROR: There are too many items in the file. (ARRAY_SIZE = 25000)
Elapsed Time <Insertion Sort >: 473 milliseconds


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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#define INFILE1    "NumFile100K.txt"
#define INFILE2    "NumFile25K.txt"
#define INFILE3    "NumFile500.txt"
#define INFILE4    "NumFile5K.txt"
 
#define ARRAY_SIZE1 100000
#define ARRAY_SIZE2 25000
#define ARRAY_SIZE3 500
#define ARRAY_SIZE4 5000
 
int* read(int , string, int* );
 
 
void BubbleSort(int*, int);
void SelectionSort(int*,int);
void InsertionSort(int*,int);
 
 
int main()
{
    clock_t beginTime = 0;
    clock_t endTime = 0;
    clock_t elapsedTime = 0;
    long secondsElapsed = 0;
    long milsElapsed = 0;
    // wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
    cout<<" Populating Arrays... <500 elements> "<<endl;
    cout<<" Sorting "<<endl;
 
    int list500[500];
    read(ARRAY_SIZE3,INFILE3 ,list500);
    beginTime = clock();
    BubbleSort(list500, ARRAY_SIZE3);
    endTime = clock();
    cout << " SUMMARY RESULTS" << endl;
    elapsedTime = endTime - beginTime;
    cout << " Elapsed Time <BubbleSort> " <<setw(6)<< ": " << elapsedTime<<" milliseconds"<<endl;
 
    read(ARRAY_SIZE3,INFILE3 ,list500);
    beginTime = clock();
    SelectionSort(list500, ARRAY_SIZE3);
    endTime = clock();
    elapsedTime = endTime - beginTime;
    cout << " Elapsed Time <Selection Sort >: " << elapsedTime<<" milliseconds"<<endl;
 
    read(ARRAY_SIZE3,INFILE3 ,list500);
    beginTime = clock();
    InsertionSort(list500, ARRAY_SIZE3);
    endTime = clock();
    elapsedTime = endTime - beginTime;
    cout << " Elapsed Time <Insertion Sort >: " << elapsedTime<<" milliseconds"<<endl;
 
    //wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
    cout<<endl<<endl<<" Populating Arrays... <5000 elements> "<<endl;
    cout<<" Sorting "<<endl;
 
    int list5000[5000];
    read(ARRAY_SIZE4,INFILE4 ,list5000);
    beginTime = clock();
    BubbleSort(list5000, ARRAY_SIZE4);
    endTime = clock();
    cout << " SUMMARY RESULTS" << endl;
    elapsedTime = endTime - beginTime;
    cout << " Elapsed Time <BubbleSort> " <<setw(6)<< ": " << elapsedTime<<" milliseconds"<<endl;
 
    read(ARRAY_SIZE4,INFILE4 ,list5000);
    beginTime = clock();
    SelectionSort(list5000, ARRAY_SIZE4);
    endTime = clock();
    elapsedTime = endTime - beginTime;
    cout << " Elapsed Time <Selection Sort >: " << elapsedTime<<" milliseconds"<<endl;
 
    read(ARRAY_SIZE4,INFILE4 ,list5000);
    beginTime = clock();
    InsertionSort(list5000, ARRAY_SIZE4);
    endTime = clock();
    elapsedTime = endTime - beginTime;
    cout << " Elapsed Time <Insertion Sort >: " << elapsedTime<<" milliseconds"<<endl;
 
    //wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
    cout<<endl<<endl<<" Populating Arrays... <25000 elements> "<<endl;
    cout<<" Sorting "<<endl;
 
    int list25000[25000];
    read(ARRAY_SIZE2,INFILE2 ,list25000);
    beginTime = clock();
    BubbleSort(list25000, ARRAY_SIZE2);
    endTime = clock();
    cout << " SUMMARY RESULTS" << endl;
    elapsedTime = endTime - beginTime;
    cout << " Elapsed Time <BubbleSort> " <<setw(6)<< ": " << elapsedTime<<" milliseconds"<<endl;
 
    read(ARRAY_SIZE2,INFILE2 ,list25000);
    beginTime = clock();
    SelectionSort(list25000, ARRAY_SIZE2);
    endTime = clock();
    elapsedTime = endTime - beginTime;
    cout << " Elapsed Time <Selection Sort >: " << elapsedTime<<" milliseconds"<<endl;
 
    read(ARRAY_SIZE2,INFILE2 ,list25000);
    beginTime = clock();
    InsertionSort(list25000, ARRAY_SIZE2);
    endTime = clock();
    elapsedTime = endTime - beginTime;
    cout << " Elapsed Time <Insertion Sort >: " << elapsedTime<<" milliseconds"<<endl;
 
    //wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
    cout<<endl<<endl<<" Populating Arrays... <100000 elements> "<<endl;
    cout<<" Sorting "<<endl;
 
    int list100000[100000];
    read(ARRAY_SIZE1,INFILE1 ,list100000);
    beginTime = clock();
    BubbleSort(list100000, ARRAY_SIZE1);
    endTime = clock();
    cout << " SUMMARY RESULTS" << endl;
    elapsedTime = endTime - beginTime;
    cout << " Elapsed Time <BubbleSort> " <<setw(6)<< ": " << elapsedTime<<" milliseconds"<<endl;
 
    read(ARRAY_SIZE1,INFILE1 ,list100000);
    beginTime = clock();
    SelectionSort(list100000, ARRAY_SIZE1);
    endTime = clock();
    elapsedTime = endTime - beginTime;
    cout << " Elapsed Time <Selection Sort >: " << elapsedTime<<" milliseconds"<<endl;
 
    read(ARRAY_SIZE1,INFILE1 ,list100000);
    beginTime = clock();
    InsertionSort(list100000, ARRAY_SIZE1);
    endTime = clock();
    elapsedTime = endTime - beginTime;
    cout << " Elapsed Time <Insertion Sort >: " << elapsedTime<<" milliseconds"<<endl;
    return 0;
 
}
 
int* read(int size , string file, int*storageArray)
{
    int i = 0;
     
    for(i = 0; i < size; i++)
        storageArray[i] = 0;
     
    //  open the file
    ifstream inputHandle(file, ios::in);
     
    // check if the file opened
    if(inputHandle.is_open() == true) {
        // if the file opened, read integers until EOF (end of file) is encountered
        i = 0;
        while( !inputHandle.eof() ) {
            if(i < size) {
                inputHandle >> storageArray[i];
                 
                i++;
            }
            else {
                cout << "ERROR: There are too many items in the file. (ARRAY_SIZE = " << size << ")" << endl;
                break;
            }
        }
         inputHandle.close();
    }
 
    return storageArray;
 
}
Last edited on
You could rewrite read so that you aren't looping on eof, which is generally the wrong thing to do.

Untested:
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std ;

int * read( int size, string file, int* storageArray)
{
    for ( int i=0; i<size ; ++i )
        storageArray[i] = 0 ;

    ifstream in(file) ;

    int index = 0 ;
    while ( index < size && (in >> storageArray[index])  )
        ++index ;

    int dummy ;
    if ( index < size )
    {
        cerr << "ERROR: Unable to read " << size << " items from the file."
                     "Only read " << index << ".\n" ;
    }
    else if ( in >> dummy )
    {
             cerr << "ERROR: There are too many items in the file."
                    "  (ARRAY_SIZE = " << size << ")\n" 
    } 

    return storageArray ;
}


[edit: corrected copy & paste error.]
Last edited on
Is there any other way i could do it, since rewriting read function is giving me errors
I corrected a copy and paste error I made in the code above. You'll find people are better able to help if you give more specific information than "it's giving me errors." What kind of errors? You're unable to compile -- in which case what are the compiler errors?? It causes segmentation faults when you run the program? It just doesn't behave the way you expect it to?
Topic archived. No new replies allowed.