Finding the minimum, maximum and mode of a string.

Sorry for the insanely long void parameters. I was sure this would work but I
got a wall of weird errors. I'm guessing it's logical. This is my first attempt
at using a void. Would a void function be useful for this?

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
// Find the min, max, and mode of a string
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
inline void keep_window_open(){char ch; cin>>ch;}

void stringtest(vector<string> &storage, string &mode, string &max, string &min, int maxfreq, string x){
	
	sort(storage.begin(),storage.end());
	
	int freq = 0;

	for(int i = 0; i < storage.size(); ++i){
		if (x == storage[i]){
			++ freq;
		}
		if (freq > maxfreq){
			maxfreq = freq;
			mode = x;
		}
	}
	
	max = storage[storage.size()];
	min = storage[0];
	
	cout << "\n";
	cout << "Mode is " << mode << endl;
	cout << "Max is " << max << endl;
	cout << "Min is " << min << endl;
}

int main() {  
	
	int maxfreq = 0;
	string input, mode, max, min;
	string yesno;
	bool resume = true;
	
	vector<string>storage;
	
	while(resume == true){
		
		cin >> input;
		storage.push_back(input);
		stringtest(storage, mode, max, min, maxfreq, input);
		
		cout << "Contine? Y/N" << endl;
		cin >> yesno;
		
		if (yesno == "Y"){
		resume = true;
		}
		
		else if (yesno == "N"){
		resume = false;
		}
	}

	return 0;
}
Last edited on
At a glance:

line 10: you have "string max" twice, pick another name

So you have this function stringtest, that expects a vector of strings, several references to string and a reference to int, but in line 44 you call it with just one parameter - a string.

The first time through the for-loop in line 14, you'll be accessing storage[-1]

You need a semi-colon in line 15

end() does not produce the last element, it goes one past the last element, so in line 24: you are accessing one past the end of the vector

line 36: yesno is a char, you can't compare it like you do in lines 49 and 53. This is comparing a char to a const char *

There's a serious logical incompatibility between stringtest and how you're trying to use it in main.

I've spent a while modifying the code. It spazzed out of control on the first test. Is it even possible to find the mode of a sequence of strings?
There's no reason why you couldn't find the mode, at a very basic level all data is numbers.

If you've modified the code then perhaps you could post the new version for us to look at (did you edit your original post with new code?), maybe we can help you to improve it.
Last edited on
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
// Find the min, max, and mode of a string
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
inline void keep_window_open(){char ch; cin>>ch;}

void stringtest(vector<string> &storage, string &mode, string &max,
string &min, int maxfreq, string x){

        sort(storage.begin(),storage.end());
        int freq = 0;
        
        /* 
         * Careful with array indexes, they start @ 0
         * reading beyond the top index will produce
         * an exception error (Segmentation Fault)
         * and your code will crash.
         */
        for(unsigned int i = 1; i <= storage.size(); i++){
                if (x == storage[i-1]){
                    if (++freq > maxfreq){
                            maxfreq = freq;
                            mode = x;
                    }
                }
        }
        // indexes again.
        max = storage[storage.size()-1];
        min = storage[0];

        cout << "\n";
        cout << "Mode is " << mode << endl;
        cout << "Max is " << max << endl;
        cout << "Min is " << min << endl;
}

int main() {

        int maxfreq = 0;
        string input, mode, max, min;
        string yesno;
        vector<string>storage;

        while(true){

                cin.ignore();
                cin >> input;
                //getline(cin, input);    // use this instead of cin if you want to get a full line rather than the first word.
                storage.push_back(input);
                stringtest(storage, mode, max, min, maxfreq, input);

                cout << "Contine? Y/N" << endl;
                cin >> yesno;

                if (yesno == "Y"){
                    continue;
                }

                else if (yesno == "N"){
                    break;
                }
        }

        return 0;
}
Last edited on
Topic archived. No new replies allowed.