Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Compare the size of elements in an array in C language
Compare the size of elements in an array in C language

Sometimes, array elements can also be used as function arguments

Example: There are two arrays a and b, each with 10 elements. Compare them one by one (i.e. a[0] is compared with b[0], a[1] is compared with b[1]...). If the number of elements in array a is greater than the number of corresponding elements in array b than the number of elements in array b is greater than the number of elements in array a (for example: a[i]>b[i] 6 times, b[i]>a[ i] 3 times, where i is a different value each time), it is considered that array a is greater than array b, and the number of times that the corresponding elements of the two arrays are greater than, equal to, or less than are counted.

#include "stdafx.h"

#include

#include

int _tmain(int argc, _TCHAR* argv[])

{

int large(int x,int y);

int a[10],b [10],i,n=0,m=0,k=0;

printf("Input array a:");

for (i=0;i< 10;i++)

{

scanf_s("%d",&a[i]);

}

printf(" \n");

printf("Input array b:");

for(i=0;i<10;i++)

{< /p>

scanf_s("%d",&b[i]);

}

for (i=0;i<10;i++)

{

if(large(a[i],b[i])==1)

{

n=n+1;

}

else if(large(a[i],b[i])==0)

{

m =m+1;

}

else

{

k=k+1;

}

}

printf("a[i]>b[i] %d times\na[i]=b[i] %d times\na[i]

if (n>k)

{

printf("array a Greater than array b\n");

}

else if (n

{

printf("array a Less than array b\n");

}

else

{

printf("Array a equals array b");

}

getchar();

getchar();

return 0;

}

int large(int x,int y)

{

int flag;

if(x>y)

< p>{

flag=1;

}

else if (x

{

flag=-1;

}

else

{

flag=0;

}< /p>

return flag;

}