Re: Help needed for a sorting code in the literature

From: Mok-Kong Shen (mok-kong.shen_at_t-online.de)
Date: 01/01/05


Date: Sat, 01 Jan 2005 18:08:46 +0100


John E. Hadstate wrote:

> "Mok-Kong Shen" <mok-kong.shen@t-online.de> wrote:

>>
>> To just pick a 'hypothetical' case, one can't have in C
>>
>> int a[3][3], b[5][5];
>>
>> and yet have a subprogram such that one could simply
>
> call
>
>> sum(a,3) and sum(b,5) to compute the sum of all
>
> elements,
>
>> if I don't err. You may call that a difference in
>
> 'models',
>
>> I would regard it as an inconvenience for the
>
> programmers.
>
>>To write a corresponding subprogram in C would presumably
>>involve at least a little bit more work than in Fortran or
>>some other PLs. But I may certainly err due to my
>
> ignorance
>
>>and you might readily give us a good solution.
>>
>
>
> You have certainly erred due to your ignorance and I can
> readily give you a good solution:
>
> int sum(int *x, int side)
> {
> int totelem, idx, total;
>
> total = 0;
> totelem = side * side;
>
> for (idx = 0; idx < totelem; idx++)
> total += *(x + idx);
>
> return total;
> }
>
> int main()
> {
> int a[3][3], b[5][5];
>
> int asum, bsum;
>
> asum = sum(&a[0][0], 3);
> bsum = sum(&b[0][0], 5);
> }

O.k. Then try to design a general subprogram for matrix
multiplication. The code would be much less readable than
a Fortran code.

M. K. Shen



Relevant Pages