Question:
Searching Arrays C language?
2016-11-06 09:07:16 UTC
You are given an array x of c-strings along with an int variable n that contains the number of elements in the array . You are also given an array of chars representing a string named 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.

My Code :

char* modeList[n] = {"msft", "appl", "msft", "csco", "ibm", "csco", "msft"};
std::string x[n];
int mode1count = 0;
int mode2count = 0;
int mode3count = 0;
int mode4count = 0;

int i;

for(i = 0; i < n; ++i)
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;
}
Four answers:
cja
2016-11-07 05:59:03 UTC
A previous responder suggested sorting, and that's a good idea. Here's an example that should give you an idea for how to finish your program:



#include

#include

#include



#define MAX_STR_LEN 16



const char *strList[] = { "msft", "appl", "msft", "csco", "ibm", "csco", "msft" };

const size_t strListLen = sizeof(strList) / sizeof(strList[0]);

void selectionSortCstr(const char **, size_t);

void mode(const char **, size_t, char *);



int main(int argc, char *argv[]) {

. . char theMode[MAX_STR_LEN];



. . mode(strList, strListLen, theMode);

. . puts(theMode);



. . return 0;

}



void mode(const char **sArray, size_t n, char *mode) {

. . const char **a = calloc(n, sizeof(char *));

. . size_t i, j, modeCnt = 1;

. . int modeIndex = -1;



. . for (i = 0; i < n; i++) {

. . . . a[i] = sArray[i];

. . }

. . selectionSortCstr(a, n);



. . i = 0; j = i + 1;

. . while (j < n) {

. . . . while ((j < n) && (strcmp(a[i], a[j]) == 0)) ++j;

. . . . if ((j - i) > modeCnt) {

. . . . . . modeCnt = j - i;

. . . . . . modeIndex = i;

. . . . }

. . . . i = j; j = i + 1;

. . }

. . if (modeIndex != -1) {

. . . . strcpy(mode, a[modeIndex]);

. . } else {

. . . . strcpy(mode, "no mode");

. . }

. . free(a);

}



void selectionSortCstr(const char **a, size_t n) {

. . int i, j, min;

. . const char *t;



. . for (i = 0; i < n; i++) {

. . . . min = i;

. . . . for (j = i + 1; j < n; j++) {

. . . . . . if (strcmp(a[j], a[min]) < 0) {

. . . . . . . . min = j;

. . . . . . }

. . . . }

. . . . t = a[min];

. . . . a[min] = a[i];

. . . . a[i] = t;

. . }

}



#if 0



Program output:



msft



#endif
2016-11-06 09:24:18 UTC
It would be much more convenient if you would work more general and sort the array of strings in alphabetical order. Then the identical strings would be next to each other and you can determine the mode in one scan regardless of what kind of strings are in there. You could start with Bubble sort, which is easy but is a slow sorting method.

In fact you do not need to sort, but you can compare immediately all strings :



int i,j,m,mode;



mode = 0;

for(i=0;i
{

m = 0;

for(j=0;j
if (m>mode) mode = m;

}



This works in general for all kinds of strings.
Richard
2016-11-06 13:22:57 UTC
You cannot copy a string into a different string variable using an equals. Look at the strcpy function.



x[i] = modeList[i]; will not work, use strcpy(x[i], modeList[i]); instead. Note: this works only with null terminated strings. Use strncpy to copy non-null terminated strings.
roger
2016-11-06 09:43:31 UTC
actually you cannot do that.

You cannot assign to an array.

You can copy the elements of another array into it.

char ar[10];

char a2[10];



....

ar=a2; // oops cannot do that...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...