I gonna post 2 questions , thanks in advanced for anyone!
1) I created a program that takes an array(arr[] in my prog.), and put the number of it in a different array, but only once (check in my prog).
for example , for we'll get for this task I wrote a function isexist wich returns 0 if the number is not found already in "check" and positive number if it's already there.
as you may guess , this program does not work and get stuck at the searching and putting part.
can someone tell my what is the error? it drives me crazy because I can't see why it's not working..
Code:
#include <stdio.h>
int isexist (int x1,int arr1[]);
int main (void)
{
int arr[5];
int x;
printf ("enter 5 numbers: \n");
for (int i=0;i<5;i++)
{
scanf ("%d",&x);
arr[i]=x;
}
int check[5];
int z=0;
for (int j=0;j<5;j)
{
if (isexist(arr[j],check)==0)
{check[z]=arr[j];
z++;
}
}
for (int i=0;i<5;i++)
printf ("%d\n",check[i]);
return 0;
}
int isexist (int x1,int arr1[])
{
int exist=0;
for (int i=0;i<5;i++)
if (x1==arr1[i])
exist++;
return exist;
}
2) I tried to write a function which says how many elements there are in an array. this function brings back weird numbers sometimes, and also stops counting if one of the elements are 0. can someone advise me how to improve this function?
Code:
int howmany (int array[])
{
int i,z;
for (i=0;array[i]!=0;i++)
z=0; //just a garbage code line to give the for loop something to do while i is getting bigger
return i;
}