Hello, I wrote this c program that will rearrange the numbers in an array such that all the even numbers come first and that will also calculate the sum. However, when I tested it on mipsmark software ( for correcting assembly language programs) I have a test fails.
Here's what it say: Did 0 swaps
evens=-36 odds=0
int arr[LENGTH] = {1,2,3,4,5,6,7,8,9};
void rearrange (int a[], int length)
{
/* Initialize left (i) for outer loop iteration and right index (j) for inner loop iteration */
int i = 0, j = 0;
/* repeat i loop n times */
for (i = 0; i < length; i++)
{
/* start the j loop from the element next to i && set j = i + 1 */
for (j = i + 1; j < length; j++)
{
/* check if 1st element is odd && 2nd is even. If so, call swap function */
if (a[i] % 2 == 1 && a[j] % 2 == 0)
{
/* once elements are exchanged, compare the 1st element with the others */
swap (&a[i], &a[j]);
break;
}
}
}
}
int firstodd (const int a[], int length)
{
int i, j;
for (i = 0; i < length; i++)
{
/* repeat loop n times until if finds an odd number */
if (a[i] % 2 == 1)
break;
}
return i;
}
int sum (const int a[], int length)
{
/* set s to 0 */
int i, sum = 0;
for (i = 0; i < length; i++)
/* adding each element of array with current sum value */
sum = sum + a[i];
return sum;
}