English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

write a function called Difference that take in an integer array intarr of size 10. the function returns the differernce between the sum of values from cell 0 to 4 and the sum from cell 5 to 9. for example, if intarra[10]={1,1,1,1,1,2,2,2,2,2}, difference (intarr) returns -5 (1+1+1+1+1 - (2+2+2+2+2)). what is the source code?
answer in C PROGRAMMING not in java

thanks

2007-10-15 20:58:57 · 2 answers · asked by iya_obciana 1 in Computers & Internet Programming & Design

2 answers

Note there are 2 DIFF macros - DIFF1 finds the difference, DIFF2 is what you described in the question.


#include

#define ARRAYSIZE 10
#define DIFF1(a,b) ((a)>(b)?((a)-(b)):((b)-(a)))
#define DIFF2(a,b) ((a)-(b))

int difference( int *arr )
{
int i;
int suma=0;
int sumb=0;

for (i=0; i<(ARRAYSIZE/2); i++)
suma+=arr[i];

for (i=(ARRAYSIZE/2); i sumb+=arr[i];

return DIFF1(suma, sumb);

}

int main()
{
int intarra[]={1,1,1,1,1,2,2,2,2,2};

printf("%d\n",difference(intarra));
}

2007-10-16 01:21:03 · answer #1 · answered by Bob R 4 · 0 0

Hi,
Use the following code:
-----------------------------------------------------------------------------------
#include
#include

int difference(int *);

void main()
{
int intarr[10]={1,2,4,5,6,7,5,4,3,2};
clrscr();
printf("Difference: %d ",difference(intarr));
getch();
}

int difference(int *x)
{
int i,result;
result=0;
for(i=0;i<10;i++)
{
if(i<5)
result+=x[i];
else
result-=x[i];
}
return result;
}
-----------------------------------------------------------------------------------

2007-10-16 04:28:34 · answer #2 · answered by iqbal 4 · 0 0

fedest.com, questions and answers