### Finding mode of int array

Ok, so I was breezing through my homework for my compy class when I came across a problem with finding the mode of an array. Here's the problem that was given:
 12345 //You are given an array x of string elements along with an int variable n that //contains the number of elements in the array. You are also given a string //variable mode that has been declared. Assign to mode the mode value of the //array. (Assume there are no "ties".) // NOTE: The mode is the value that occurs most frequently. // EXAMPLE: Given "msft" "appl" "msft" "csco" "ibm" "csco" "msft", the mode is //"msft" because it appears the most number of times in the list.

What my question is, how do I go about finding the mode? I know it's the most often. So I figure I have to go through the array and count whenever items are the same. I'm just not sure where to go from there. This has been bothering me all weekend so figured I would try asking here.
Last edited on
I'm not sure I get it. Do you have an array of strings containing modes and you have to count which appears the most or do you have an array of structs, each of which has a mode variable and you have to count what is the mode that appears the most comparing them to an array of strings containing all the possible modes?
It's a string array with n elements that I need to find the mode of. I also have another problem similar to this I need to find the mode of an int array. Just not sure about how to count and keep track of the different variables to find the mode.
 12345678910111213141516171819202122232425 // The char* array is used only to initialize the strings array with a loop. // To initialize std::string arrays in the same way you need C++11 support char* modeList[n] = {"msft", "appl", "msft", "csco", "ibm", "csco", "msft"}; std::string x[n]; int mode1count = 0; // msft int mode2count = 0; // appl int mode3count = 0; // csco int mode4count = 0; // imb int i; for(i = 0; i < n; ++i) // Set the array of strings x[i] = modeList[i]; for(i = 0; i < n; ++i) { if(x[i] == "msft") ++mode1count; else if(x[i] == "appl") ++mode2count; else if(x[i] == "csco") ++mode3count; else if(x[i] == "imb") ++mode4count; }

Is this good?
Last edited on
Topic archived. No new replies allowed.