c - Checking array for identical numbers and their value -
as part of program have make, 1 of function need program should check if array has identical numbers same, , if 1 of them bigger/equals given number.
the given number amount of numbers in array
this have far:
int checkarray(int *arr, int num) { int check = num; int check2 = num; int *lor; int *poi; int *another; = arr; lor = arr; poi = arr; int check3 = num; ( ; num > 1; num--) { ( ; check3 >= 0; check3--) { if (*arr == *poi) return 0; poi++; } arr++; poi = another; } ( ; check2 > 0; check2--) { if (*lor >= check) return 0; lor++; } return 1; }
i know made many pointers/int function, that's not problem..
the part of checking given value works fine if i'm not mistaken think can ignore part (that's last 'for' loop)
i know should easy reason can't work...
edit:
i'll give example: if array 0 1 2 3 1 function return 0, cause second , last number identical. function return 0 if given number 5, , 1 of numbers bigger or equals 5, example 0 1 2 5 4.
otherwise, function returns 1.
i create new array i'm going save numbers can check if have repeat number in array. have 1 more argument in function know size of array.
#include <stdio.h> #include <stdlib.h> int checkarray(int *arr, int size, int number){ int i,j; int *countarray = calloc(size,sizeof(int)); for(i=0;i<size;i++){ if(arr[i]>=number){ //check >= number free(countarray); return 0; } for(j=0;j<i;j++){ //check repeat number if(countarray[j]==arr[i]){ free(countarray); return 0; } } countarray[j]=arr[i]; //no repeat number save it. } free(countarray); return -1; //error } int main(){ int arr[6] = {0,8,2,3,4,1}; printf("result %d",checkarray(arr,6,5)); }
i hope can you.
update without new array
int checkarray(int *arr, int size, int number){ int i,j; for(i=0;i<size;i++){ if(arr[i]>=number){ return 0; } for(j=0;j<i;j++){ if(arr[i]==arr[j]){ return 0; } } } return -1; //error }
Comments
Post a Comment