Given two arrays of different sizes. Determine which elements of the first array and how many times they occur in the second array. In C language

  • 7
    > Determine which elements of the first array and how many times they occur in the second array. In C language and faster? - DreamChild
  • ahah, +1 - nullptr
  • one
    > it would be nice @ Arts1995 with my comment, I hinted to you that your question was not put together correctly, because the solution of learning tasks for students is not welcome here. Moreover, it is very impolite to write your question in the form of clerical language, as it is written here - no one here owes or owes anything to anyone. If you want to help you with your task, then reformulate your question more correctly and describe what you did yourself and what did not work out - DreamChild
  • one
    @ Arts1995, According to the rules of the forum, questions should not be limited to the decision or the completion of student assignments. Please clarify what you have done yourself and what did not work out. - PaulD

2 answers 2

  1. Solution: make two SQL tables by array values, make LEFT JOIN , and then everything is simple.
  2. Solution: first sort two arrays (lexicographically for a character), then iterate through each of them, simultaneously comparing the corresponding elements. The comparison process is elementary.
  • From what was said above, I did not understand anything (( - Arts1995
  • sir, are you drunk? Can be faster and faster. - Arc
  • one
    I wonder, what's the point with SQL and iterators when it comes to good old C? - DreamChild
  • Oh sorry. I thought you were writing Java \ C # \ C ++ . If you do C then take 2 decisions. Instead of iterators - the usual links. - voipp
  • four
    Humor - it is so, not everyone is given ... - Error

Forgive me moderators, but. That's not how it goes?

#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { int val; int numb; } s; void sort(int arr[], int n) { int i, flag = 1, j=1; while(flag) { flag = 0; for(i = 0; i < nj; i++) if(arr[i] > arr[i+1]) { flag = 1; int t = arr[i]; arr[i] = arr[i+1]; arr[i+1] = t; } j++; } } int search(s arr[], int n, int val) { int a = 0, b = n-1; if(val == arr[a].val) return a; if(val == arr[b].val) return b; if(val < arr[a].val) return -1; if(val > arr[b].val) return -1; while(b - a > 1) { int i = (a+b)/2; if(val == arr[i].val) return i; else if(val < arr[i].val) b = i; else a = i; } return -1; } int main() { int n1, n2, n3, *arr1, *arr2, i, i1; s *arr3; printf("Введите размер первого массива:"); scanf("%d", &n1); arr1 = malloc(sizeof(int)*n1); printf("Введите первый массив:"); for(i = 0; i < n1; i++) scanf("%d", arr1+i); for(i = 0; i < n1; i++) printf("%d ", arr1[i]); putchar('\n'); printf("Введите размер второго массива:"); scanf("%d", &n2); arr2 = malloc(sizeof(int)*n2); printf("Введите второй массив:"); for(i = 0; i < n2; i++) scanf("%d", arr2+i); for(i = 0; i < n2; i++) printf("%d ", arr2[i]); putchar('\n'); sort(arr1, n1); for(i = 1, n3 = 1; i < n1; i++) if(arr1[i-1] != arr1[i]) n3++; arr3 = malloc(sizeof(s)*n3); bzero(arr3, sizeof(s)*n3); for(i=0, i1=0; i<n1; i++) if(!i || arr1[i] != arr1[i-1]) arr3[(i1++)].val = arr1[i]; for(i=0; i<n2; i++) if((i1 = search(arr3, n3, arr2[i])) >= 0) arr3[i1].numb++; for(i=0; i<n3; i++) printf("%10d:%5d\n", arr3[i].val, arr3[i].numb); free(arr3); free(arr1); free(arr2); return 0; } 
  • @alexlz shame on you - DreamChild
  • @DreamChild I'm ashamed - alexlz