الخميس، 7 أبريل 2011

Assignment

1. Write a function that accepts an array of non-negative integers and returns the second largest integer in the array. Return -1 if there is no second largest.


If you are programming in Java or C#, the signature of the function is 


int f(int[ ] a)


If you are programming in C or C#, the signature of the function is 


int f(int a[ ], int len) where len is the number of elements in a.





First answer public static void main()
{
a1(new int[]{1, 2, 3, 4});
a1(new int[]{4, 1, 2, 3});
a1(new int[]{1, 1, 2, 2});
a1(new int[]{1, 1});
a1(new int[]{1});
a1(new int[]{});
}

static int a1(int[] a)
{
int max1 = -1;
int max2 = -1;

for (int i=0; i
{
if (a[i] > max1)
{
max2 = max1;
max1 = a[i];
}
else if (a[i] != max1 && a[i] > max2)
max2 = a[i];
}

return max2;
}












1 التعليقات:

GIGA MIND يقول...

mohamed reda salem

this is my homework
sorry for late

http://www.mediafire.com/?iveanaw07xkbmdw