verify each element to one other in a array pointer - c

C Experts,
I have an array of pointers to strings. I need to compare each array element with all other array elements and throw error if they are same. Here is the piece of code I have written and got stuck. Please help me.
# define FOUND 1
# define NOTFOUND 0
int k,flag,a;
char cmp_string[10]; //used to get one array element to compare with all other array elements
char *values[]={010,020,030,040}; //valid case that's how it should be
char *vales[]={010,020,020,030}; wrong or throw error because in array i should have only unique values
int size=4;
for(k=0; k<=size;k++){
strcpy(values[k],cmp_string);
flag=NOTFOUND;
int counter=k+1;
for(int n=counter;n<=size;n++)
{
a=((strcmp(values[n],cmp_string) || (strcmp(values[k-1],cmp_string)))
// stuck here what if k value is 2 I wont be able to compare with zero or first element of array.
if(a==0){
throw error same name for the operation
flag=FOUND;
break;
}
}//for int n;
}//for int k;
if(flag==NOTFOUND){
True or PASS
}
}

Quick solution: sort the array (using e.g. the builtin qsort function), then scan it comparing adjacent elements; if two are the same, you have a repetition.
You can also know before completing the sort that you have duplicates if in the comparison function you find that the two compared items are the same.

If I understand your question correctly, you're trying to turn strcmp into something that returns nonzero if the strings are the same and zero otherwise:
a = (strcmp(whatever) != 0) || (strcmp(whatever else) != 0);

Related

Sorting integer array by ascending order, segmentation fault: 11

The function is suppose to sort an array of random integers by ascending order. I found a method for solving this problem, the bubble sort, swaping a by b if b < a. However, my implementation, or the lack of it, keeps returning a segmentation fault: 11. Could it have something to do with the parameter "int *tab" or subscripts I'm using during the swaping of elements?
void ft_sort_integer_table(int *tab, int size)
{
int i;
int j;
int t;
i = 1;
j = 0;
t = 0;
while (tab[j] != '\0')
{
if (tab[i] < tab[j])
{
t = tab[i];
tab[i] = tab[j];
tab[j] = t;
}
i++;
j++;
}
}
Unless you are guaranteed beforehand that the value 0 terminates your buffer and doesn’t appear elsewhere in the array (like you are with null terminated strings) you can’t test for tab[i] being zero to determine that you have reached the end of the array. Your function takes size as a parameter too; why not use that?
EDIT: Also, no sorting algorithm runs in O(n). Bubble sort, which looks like what you’re trying to implement, requires two nested loops.
Skipping the correctness of this implementation of the sorting algorithm (as it seems wrong) the segmentation is caused by the null termination check that you are doing. The NULL('\0') character is specified for strings, or char array types in C programming language, and it is used to signal their termination. It doesn't work with int type arrays. You should be using the size argument for iterating the array.
You do not use the size parameter. Instead you are trying find the null-terminator which int array is not supposed to have (unlike C-string). So in case you have to compare j with size and keep swapping till the array is fully sorted.
Also, it is better of using size_t size instead of int size in order to stay pedantic.
You pass an array and a size to your sorting function but do not use the size anywhere so potentially i and j could go out of bounds causing undefined behavior.
An int array can contain 0s so you need to have other criteria for when your sorting is finished. E.g. when you go through all the elements in the array [0..size] and do not do a swap - then it is sorted.
Firstly, your while loop logic is wrong. The character '\0' refers to the null character at the end of string. This doesn't make sense if you compare it with int type.
Secondly, the logic you implemented is comparing side by side elements of an array and not a single element with all others and placing it. I would recommend you study bubble sort. Geekforgeeks is the best source for cse guys. Hope it solves. Cheers !! Feel free to ask questions

Comparing elements from two 2d-arrays in C without strcmp

I am creating a spell-checker in C. I have a dictionary array which is a 2d array. So each word in the dictionary takes a row in the 2d array. In the same way, my input array is also a 2d array. I want to check the spelling of the rows/words in my input array. I cannot use strcmp
An example of input array
['boy','girll','.','friend',' ']-can contain spaces,punctuation and words. We only care about spelling words
if a punctuation/space is compared against a word,we ignore it and move onto the next word.
example of dictionary
['boy','girl','cow'...]-all are words
My code is:
for (int a = 0; a < MAX_INPUT_SIZE + 1; a++)
{
for (int b = 0; b < MAX_DICTIONARY_WORDS; b++)
{
if(tokens[a]==dict_token[b])
{
printf("correct");
}
else
{
printf("wrong");
}
}
}
The output is all "wrong". Though 5 out of the 6 word input should be correct.
Every test returns false because the comparison you're using,
if(tokens[a]==dict_token[b])
is comparing two pointers that are never going to point at the same address, because, the tokens you are testing are in a completely separate bit of memory to the dict_token dictionary that you are comparing them with.
You need to pass the two pointers tokens[a] and dict_token[b] to a comparison function that will perform a letter-by-letter comparison, and which will return one value when it finds a difference between them, and another when it gets to the end of both without finding a difference. In other words, you need to write an implementation of strcmp.

Delete duplicate in an array and insert first occurrence in a new array in C

I have to do a function which, taken as parameters a pointer (int *vect) and the dimension (dim) of the array allocated, returns a new array which contains all the elements of the old array not repeated and all the first occurrence of repeated elements, this is the code where i put the elements not repeated of the first array, but i don't know how to proceed to put the first occurrence of repeated elements (e.g. INPUT : vect={1;2;3;3;4;5;5} OUTPUT : {1;2;3;4;5})
int* deleteDup(int *vect,int dim){
int* vect2,int dim2;
vect2=malloc(sizeof(int)*dim2);
int i,j,temp;
int count=0;
for(i=0;i<dim-1;i++){
temp=vect[i];
for(j=i+1;j<dim;j++){
if(temp==vect[j]){
count++;
}
}
if(count==0){
dim2++;
vect2=realloc(vect2,dim2*sizeof(int));
vect2[dim2]=temp;
}
*dim_nodup=dim2;
return vect2;
}
This looks like homework to me.
You need to first count the number of unique elements, then allocate an array of that size, and then move one instance of each unique element to the new array.
This can be optimised various ways.
I prefer not to give you code or even pseudo code, as you should figure this out yourself when solving the problem.
i don't know how to proceed to put the first occurrence of repeated elements
To do this you must move elements one by one into the array you are returning, and for each item check if it is already in the array.
Good luck! :)

C programming and array ,matrix manipulation

I am trying to take an online test on techgig.com .
The problem is to find the minimum path from top left of the matrix to the bottom right.
I am trying to code this using C language.
The minimumcost function is already given as and I am not allowed to change this .
char* minumumcost(char* input1[],int input2)
where input1 is the matrix and input2 is the number of rows in matrix.
However with my limited knowledge of C , I only know how to manipulate a matrix when it is declared as a two dimensional array.
With two dimensional array I intend to do the following operation.
Here the cost matrix is cost[10][10]
\
while(point1!=n && point2!=n)
{
if(cost[a][j+1]<cost[a+1][j] && cost[a][j+1]<cost[a+1][j+1])
{
min=cost[a][j+1];
s[k]='R';
k++;
j++;
point1=a;
point2=j;
}
else if(cost[a+1][j]<cost[a+1][j+1] && cost[a+1][j]<cost[a][j+1])
{
min=cost[a+1][j];
s[k]='B';
k++;
a++;
point1=a;
point2=j;
}
else if(cost[a+1][j+1]<cost[a+1][j] && cost[a+1][j+1]<cost[a][j+1])
{
min=cost[a+1][j];
s[k]='D';
a++;
j++;
k++;
point1=a;
point2=j;
}
}
\
An example shows that the input to the matrix is given in this form.
{5#2#3#2,8#5#5#3,1#4#7#6,3#3#6#5},4
where 4 is the no of rows and the remaining is the elements of the row .
The question is how do I do this by using input1[] ?
Please help me soon.
The test has a time limit.
The function accepts an array of char*. Note that, in C, arrays and pointers can be used interchangably most of the time.
If you declared a pointer like char* p; you could use it just like an array: p[5]. So in your case, char* input[] is quite the same as an array of arrays of char: input[2][5] will take element #2 from the array (yielding a char*!) and from that element #5.
Or, even more explicitly, you could as well write
char* row = input[2];
char element = row[5];
Yet, how many elements each row of the matrix will have cannot be told from what you posted. Is it a square matrix, are the rows represented as \0 terminated strings? You'll have to check the test question for that info.

Bug While i try to compare

Actually i have a little bug with this code:
printf("Estadio Nemesio Diez\n\n");
for(i=0;i<12;i++)
{
if(ultimoAnoDiez[0]==ultimoAnoDiezOriginal[i]);
{
mes=i;
}
}
i am comparing 2 arrays, and im selecting the index[0], because that array is already sorted, but when i compile and execute the file, the program says that all the values on each array is the same, but if i print the values of each array all are diferents.
I will appreciate the help
BTW Im programmin on C
I think there can be two reasons:
First
your if condition is ending with ; (making {/* othet statement */} a block but not if block)
Second
(ultimoAnoDiez[0]==ultimoAnoDiezOriginal[i])
should be (you should be comparing individual values and not all values(i) to first value(0) only)
(ultimoAnoDiez[i]==ultimoAnoDiezOriginal[i]);
Further to check for array equality you return as soon as you find unequal values so it should be like
int mes = 12;
for(i=0;i<12;i++)
{
if(ultimoAnoDiez[i]!=ultimoAnoDiezOriginal[i])
{
mes=i;
break;
}
}
if(mes < 12){
//UNEqual
}

Resources