which is the better path

D

Darklight

Question:
Write a function named sumarrays() that accepts two arrays that are the same
size. The function should add each element in the array together and place
the values in the thrid array.

Which answer is the better practice;

/*EX9.C*/
#include<stdio.h>
#define MAX 5

int array2[MAX] = {1,2,3,4,5};
int array3[MAX] = {6,7,8,9,10};
int sumarray(int array2[], int array3[]);

int main(void)
{
sumarray(array2,array3);
return 0;
}

int sumarray(int array2[], int array3[])
{
int ctr;
int total[MAX];

for(ctr = 0; ctr < MAX; ctr++)
{
total[ctr] = array2[ctr] + array3[ctr];
printf("Total %d\n",total[ctr]);
}
return total[count];
}
____________________________________________
/* EX9-1.C*/
#include<stdio.h>
#define SIZE 5

void sumarray(int [], int []);
int main(void)
{
int a[SIZE] = {1,2,3,4,5};
int b[SIZE] = {6,7,8,9,10};

sumarray(a,b);
return 0;
}

void sumarray(int first[], int second[])
{
int total[SIZE];
int ctr;

for(ctr = 0; ctr < SIZE; ctr++)
{
total[ctr] = first[ctr] + second[ctr];
printf("Total %d\n",total[ctr]);
}

}
 
J

Joona I Palaste

Darklight said:
Question:
Write a function named sumarrays() that accepts two arrays that are the same
size. The function should add each element in the array together and place
the values in the thrid array.

That's all? The function doesn't need to DO anything with the third
array?
Which answer is the better practice;
/*EX9.C*/
#include<stdio.h>
#define MAX 5
int array2[MAX] = {1,2,3,4,5};
int array3[MAX] = {6,7,8,9,10};
int sumarray(int array2[], int array3[]);
int main(void)
{
sumarray(array2,array3);
return 0;
}
int sumarray(int array2[], int array3[])
{
int ctr;
int total[MAX];
for(ctr = 0; ctr < MAX; ctr++)
{
total[ctr] = array2[ctr] + array3[ctr];
printf("Total %d\n",total[ctr]);
}
return total[count];

count is undeclared.
}
____________________________________________
/* EX9-1.C*/
#include<stdio.h>
#define SIZE 5
void sumarray(int [], int []);
int main(void)
{
int a[SIZE] = {1,2,3,4,5};
int b[SIZE] = {6,7,8,9,10};
sumarray(a,b);
return 0;
}
void sumarray(int first[], int second[])
{
int total[SIZE];
int ctr;
for(ctr = 0; ctr < SIZE; ctr++)
{
total[ctr] = first[ctr] + second[ctr];
printf("Total %d\n",total[ctr]);
}

I like the second one better, because it has the arrays local to main()
instead of global.
You don't appear to use the return value from sumarray() in the first
one for anything either.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The day Microsoft makes something that doesn't suck is probably the day they
start making vacuum cleaners."
- Ernst Jan Plugge
 
B

Bruno Desthuilliers

Darklight said:
Question:
Write a function named sumarrays() that accepts two arrays that are the same
size. The function should add each element in the array together and place
the values in the thrid array.

Which third array ?
Which answer is the better practice;

/*EX9.C*/
#include<stdio.h>
#define MAX 5

int array2[MAX] = {1,2,3,4,5};
int array3[MAX] = {6,7,8,9,10};

Why are those arrays globals ?
int sumarray(int array2[], int array3[]);

You don't need this if you put the whole function first.
int main(void)
{
sumarray(array2,array3);

summarray() returns an int, why do you do with it ?
And where is the result ?
return 0;
}

int sumarray(int array2[], int array3[])
{
The size of arrays shoud be passed as a param. And why 'array2' and
'array3' ? Where is 'array1' ?
One usually uses 'i' for loop indices.
int total[MAX];
(bis) The size of arrays shoud be passed as a param
for(ctr = 0; ctr < MAX; ctr++)
{
total[ctr] = array2[ctr] + array3[ctr];
printf("Total %d\n",total[ctr]);

What is this printf() doing here ?
}
return total[count];

Er... did you ever try and *compile* this code ?
}
____________________________________________
/* EX9-1.C*/
#include<stdio.h>
#define SIZE 5

void sumarray(int [], int []);
int main(void)
{
int a[SIZE] = {1,2,3,4,5};
int b[SIZE] = {6,7,8,9,10};
Ok, they are at least at the right place
sumarray(a,b);

Where is the result ?
return 0;
}

void sumarray(int first[], int second[])
{
array size should be passed as a parameter.
int total[SIZE];
int ctr;
same as above
for(ctr = 0; ctr < SIZE; ctr++)
{
total[ctr] = first[ctr] + second[ctr];
printf("Total %d\n",total[ctr]); same as above
}

}



Both are somewhat ugly. Try again.
Tips :
- pass the array len and the 'result' array as params
the proto for sumarray() should look like :
int * sumarray(int first[], int second[], int total[], size_t len);

- move the printf() out of sumarray()
- use 'i' for the loop indice

HTH
Bruno
 
P

pete

Darklight said:
Question:
Write a function named sumarrays() that accepts two arrays that are the same
size. The function should add each element in the array together and place
the values in the thrid array.

Which answer is the better practice;

/*EX9.C*/
#include<stdio.h>
#define MAX 5

int array2[MAX] = {1,2,3,4,5};
int array3[MAX] = {6,7,8,9,10};
int sumarray(int array2[], int array3[]);

int main(void)
{
sumarray(array2,array3);
return 0;
}

int sumarray(int array2[], int array3[])
{
int ctr;
int total[MAX];

for(ctr = 0; ctr < MAX; ctr++)
{
total[ctr] = array2[ctr] + array3[ctr];
printf("Total %d\n",total[ctr]);
}
return total[count];
}
____________________________________________
/* EX9-1.C*/
#include<stdio.h>
#define SIZE 5

void sumarray(int [], int []);
int main(void)
{
int a[SIZE] = {1,2,3,4,5};
int b[SIZE] = {6,7,8,9,10};

sumarray(a,b);
return 0;
}

void sumarray(int first[], int second[])
{
int total[SIZE];
int ctr;

for(ctr = 0; ctr < SIZE; ctr++)
{
total[ctr] = first[ctr] + second[ctr];
printf("Total %d\n",total[ctr]);
}

}

/* EX9-1.C*/

#include <stdio.h>

#define N(A) (sizeof A / sizeof *A)

void sumarray(int *, int *, int *, size_t);

int main(void)
{
int a[] = {1,2,3,4,5};
int b[] = {6,7,8,9,10};
int total[N(a)];

sumarray(a, b, total, N(a));
return 0;
}

void sumarray(int *first, int *second, int *total, size_t size)
{
while (size-- != 0) {
*total = *first++ + *second++;
printf("Total %d\n", *total);
++total;
}
}
 
P

pete

pete said:
Question:
Write a function named sumarrays() that accepts two arrays that are the same
size. The function should add each element in the array together and place
the values in the thrid array.

Which answer is the better practice;

/*EX9.C*/
#include<stdio.h>
#define MAX 5

int array2[MAX] = {1,2,3,4,5};
int array3[MAX] = {6,7,8,9,10};
int sumarray(int array2[], int array3[]);

int main(void)
{
sumarray(array2,array3);
return 0;
}

int sumarray(int array2[], int array3[])
{
int ctr;
int total[MAX];

for(ctr = 0; ctr < MAX; ctr++)
{
total[ctr] = array2[ctr] + array3[ctr];
printf("Total %d\n",total[ctr]);
}
return total[count];
}

/* EX9-1.C*/

#include <stdio.h>

#define N(A) (sizeof A / sizeof *A)

void sumarray(int *, int *, size_t);

int main(void)
{
int a[] = {1,2,3,4,5};
int b[] = {6,7,8,9,10};

sumarray(a, b, N(a));
return 0;
}

void sumarray(int *first, int *second, size_t size)
{
while (size-- != 0) {
printf("Total %d\n", *first++ + *second++);
}
}
 
C

CBFalconer

Mark said:
Darklight wrote:
int sumarray(int array2[], int array3[]);

You don't need this if you put the whole function first.

True, but prototypes are a Good Thing, and to be encouraged.

Disagree. Unnecessary prototypes are a maintenance nightmare and
ought to be eliminated. It should never be necessary to write
down the same information twice. Also any function not to be
accessed outside the source file should be marked static.

This has been the way of things since 1066.
 
A

Anupam

#include <stdio.h>

#define N(A) (sizeof A / sizeof *A)

Please note that it is inadvisable to write a macro and leave it
unprotected by surrounding brackets.
<snip>
 
J

Joona I Palaste

Please note that it is inadvisable to write a macro and leave it
unprotected by surrounding brackets.

You mean that it should be like this?
#define N(A) (sizeof (A) / sizeof *(A))
If so, then I agree with you.
 
B

Bruno Desthuilliers

Darklight said:
Bruno Desthuilliers wrote:>>
return total[count]; /* thats a typing error */

Er... did you ever try and *compile* this code ?


Yes both compile and both work Thanks for your tips.
I doubt *this* compile :
int sumarray(int array2[], int array3[])
{
int ctr;
int total[MAX];

for(ctr = 0; ctr < MAX; ctr++)
{
total[ctr] = array2[ctr] + array3[ctr];
printf("Total %d\n",total[ctr]);
}
return total[count];
}

If it does, then your compiler is broken beyond all hope of repair.
Tip : use copy/paste when you post code, it avoids typos !-)

Bruno.
 
A

Arthur J. O'Dwyer

You mean that it should be like this?
#define N(A) (sizeof (A) / sizeof *(A))
If so, then I agree with you.

Devil's advocate: Pete's way works, as long as you only use the
macro in its intended form:

T my_array[foo];
something involving N(my_array);

Certainly if you try to do

something involving N(my_array+42);

it will fail horribly, but then Joona's modification would also fail
horribly given that input. So in this case it's a choice between
icky behavior on wrong input and... icky behavior on wrong input.
So in this case I prefer pete's formulation, because it uses fewer
parentheses to achieve the same effect. Easy to read is a Good Thing.
(-:

-Arthur
 
J

Joona I Palaste

Devil's advocate: Pete's way works, as long as you only use the
macro in its intended form:
T my_array[foo];
something involving N(my_array);
Certainly if you try to do
something involving N(my_array+42);
it will fail horribly, but then Joona's modification would also fail
horribly given that input. So in this case it's a choice between
icky behavior on wrong input and... icky behavior on wrong input.

Is it because the (my_array+42) bit has type T* instead of type
T[foo]? What does it take to change the type? Addition,
parantheses, or both? What types do these have?
(my_array)
my_array+0
(my_array+0)
my_array+42
 
M

Martin Dickopp

Joona I Palaste said:
Devil's advocate: Pete's way works, as long as you only use the
macro in its intended form:
T my_array[foo];
something involving N(my_array);
Certainly if you try to do
something involving N(my_array+42);
it will fail horribly, but then Joona's modification would also fail
horribly given that input. So in this case it's a choice between
icky behavior on wrong input and... icky behavior on wrong input.

Is it because the (my_array+42) bit has type T* instead of type
T[foo]?
Yes.

What does it take to change the type? Addition, parantheses, or both?
What types do these have?
(my_array)

Array type.
my_array+0
(my_array+0)
my_array+42

Pointer type. There is no special exception for the type of additive
expressions if one operand is zero.

Few expressions yield an array type. Among them are the (possible
parenthesesed) identifier referring to the array, and certain
expressions involving the . or the -> operator. Anything I've forgotten?

Since `sizeof foo->bar' and `sizeof *foo->bar' are grouped like
`sizeof (foo->bar)' and `sizeof (*(foo->bar))' anyway (likewise for the
.. operator), no parentheses are needed for this case.
 
P

pete

Joona said:
You mean that it should be like this?
#define N(A) (sizeof (A) / sizeof *(A))
If so, then I agree with you.

I negelected the parentheses accidentally.
My own policy is that it's better to use them
around all macro arguments, than it is to figure out
just exactly when they are or aren't useful.
 
D

Dan Pop

In said:
Darklight wrote:
int sumarray(int array2[], int array3[]);

You don't need this if you put the whole function first.

True, but prototypes are a Good Thing, and to be encouraged.

Non sequitur. If the function's prototype definition precedes its usage,
no declaration is needed. This reduces the program's maintenance
overhead.

Dan
 
D

Dan Pop

In said:
Devil's advocate: Pete's way works, as long as you only use the
macro in its intended form:
T my_array[foo];
something involving N(my_array);
Certainly if you try to do
something involving N(my_array+42);
it will fail horribly, but then Joona's modification would also fail
horribly given that input. So in this case it's a choice between
icky behavior on wrong input and... icky behavior on wrong input.

Is it because the (my_array+42) bit has type T* instead of type
T[foo]? What does it take to change the type? Addition,
parantheses, or both? What types do these have?
(my_array)
my_array+0
(my_array+0)
my_array+42

Ever considered actually learning C? This issue has been beaten to death
in this very newsgroup.

Dan
 
D

Dan Pop

In said:
You mean that it should be like this?
#define N(A) (sizeof (A) / sizeof *(A))
If so, then I agree with you.

Why? What does the extra pair of parentheses buy you? Either the macro
is correctly invoked, and then it works in either form, or it isn't, and
then it doesn't work in either form.

Dan
 
D

Dan Pop

In said:
Few expressions yield an array type. Among them are the (possible
parenthesesed) identifier referring to the array,

Huh?

char array[3];
char *p = array;

Anything wrong with this snippet that, according to you, assigns an array
to a pointer?
and certain expressions involving the . or the -> operator.

Could we have some examples?

3 Except when it is the operand of the sizeof operator or the
unary & operator, or is a string literal used to initialize an
array, an expression that has type ``array of type'' is converted
to an expression with type ``pointer to type'' that points to
the initial element of the array object and is not an lvalue.
Anything I've forgotten?

You've made up far too much...

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,260
Messages
2,571,039
Members
48,768
Latest member
first4landlord

Latest Threads

Top