Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #126411

Re: C program with 2 For loops

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: C program with 2 For loops
Date 2018-02-07 18:32 -0800
Organization None to speak of
Message-ID <lnfu6cyzwj.fsf@kst-u.example.com> (permalink)
References <5a7bafd4$0$710$14726298@news.sunsite.dk>

Show all headers | View raw


arnuld <sunrise@invalid.address> writes:
> AIM:  To solve the problem given down
> PROBLM: Am I missing something ?  Was it really that easy to program in 
> C ?
>
> PROBLM: Input is an integer array A. Return an integer array B such that 
>
> B[i] = product of all elements of A except A[i]
>
>
> #include <stdio.h>
>
> enum { SIZE = 3 };
>
> void printArr(int ai[]);
>
> int main(void)
> {
>   int arrA[] = {1,2,3,4};
>   int arrB[SIZE+1];
>   int m = 1;
>
>   printArr(arrA);
>   for(int i = 0; i <= SIZE; ++i)
>     {
>       for(int j = SIZE; j >= 0; --j)
> 	{
> 	  if(i != j)
> 	    {
> 	      m = m * (arrA[j]);
> 	    }
> 	}
>       arrB[i] = m;
>       m = 1;
>     }
>
>     printArr(arrB);
>   
>     return 0;
> }
>
>
> void printArr(int ai[])
> {
>   for(int i = 0; i <= SIZE; ++i)
>     printf("%d ", ai[i]);
>
>   printf("\n\n");
> }
> ====================== OUTPUT ==========================
> [arnuld@arch64 programs]$ gcc -std=c99 -pedantic -Wall -Wextra g1.c
> [arnuld@arch64 programs]$ ./a.out 
> 1 2 3 4 
>
> 24 12 8 6 

Not bad at all.  Let me suggest some minor improvements.

    enum { SIZE = 3 };

SIZE is actually one less than the number of elements in each array,
which is misleading.  I'd define SIZE as 4 and use "< SIZE" rather than
"<= SIZE" in the loops.  (Though as you'll see some other changes I'm
going to suggest eliminate the need for SIZE anyway.)

I also used "length" rather than "size" to avoid confusion with
"sizeof" (which counts bytes rather than elements).

    int arrA[] = {1,2,3,4};
    int arrB[SIZE+1];

You've defined the number of elements in one place, and the values of
the elements in another.  Never count something yourself when you can
let the computer do it; they're really good at that.  Not only is it
easier, but you can make an update in just one place and the compiler
will figure out the new size.

You define m outside the outer loop, and then reset it to 1 at the
bottom.  Instead, define it above the top of the inner loop.  Since you
get a new m on each iteration of the outer loop, you only need to
initialize it once.

I also added an argument to printArr to indicate the length of the
array.

Here's my modified version:

#include <stdio.h>

void printArr(int ai[], int length);

int main(void)
{ 
  int arrA[] = {1,2,3,4};
  enum { length = sizeof arrA / sizeof arrA[0] };

  int arrB[length];

  printArr(arrA, length);
  for(int i = 0; i < length; ++i)
    { 
      int m = 1;
      for(int j = 0; j < length; ++j)
        { 
          if(i != j)
            { 
              m *= arrA[j];
            }
        }
      arrB[i] = m;
    }

    printArr(arrB, length);

    return 0;
}

    
void printArr(int ai[], int length)
{ 
  for(int i = 0; i < length; ++i)
    printf("%d ", ai[i]);

  printf("\n\n");
}

Again, there's nothing wrong with your version, I just wanted to present
a few ideas.

I note that the problem statement says "Return an integer array B such
that ...".  You can't literally return an array from a function in C.
There are several ways to do something similar, varying in how the
memory for the result is allocated and deallocated.  You've created the
new array inside the main() function, which is fine for a small example
like this, but perhaps not if this were meant to be included in a larger
program.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

C program with 2 For loops arnuld <sunrise@invalid.address> - 2018-02-08 02:03 +0000
  Re: C program with 2 For loops ais523 <ais523@nethack4.org> - 2018-02-08 02:08 +0000
    Re: C program with 2 For loops Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-08 15:02 +0000
  Re: C program with 2 For loops Keith Thompson <kst-u@mib.org> - 2018-02-07 18:32 -0800
  Re: C program with 2 For loops Barry Schwarz <schwarzb@dqel.com> - 2018-02-07 18:36 -0800
    Re: C program with 2 For loops Barry Schwarz <schwarzb@dqel.com> - 2018-02-08 00:52 -0800
  Re: C program with 2 For loops "James R. Kuyper" <jameskuyper@verizon.net> - 2018-02-08 10:35 -0500
    Re: C program with 2 For loops Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-02-08 18:23 +0000
  Re: C program with 2 For loops Clive Arthur <cliveta@nowaytoday.co.uk> - 2018-02-09 13:21 +0000
    Re: C program with 2 For loops williammcnicol@gmail.com - 2018-02-09 05:59 -0800
      Re: C program with 2 For loops Clive Arthur <cliveta@nowaytoday.co.uk> - 2018-02-09 14:39 +0000
      Re: C program with 2 For loops James Kuyper <jameskuyper@verizon.net> - 2018-02-09 10:41 -0500
    Re: C program with 2 For loops Jorgen Grahn <grahn+nntp@snipabacken.se> - 2018-02-09 17:41 +0000
      Re: C program with 2 For loops James Kuyper <jameskuyper@verizon.net> - 2018-02-09 12:56 -0500
        Re: C program with 2 For loops Jorgen Grahn <grahn+nntp@snipabacken.se> - 2018-02-10 07:49 +0000
  C program with 2 For loops asetofsymbols@gmail.com - 2018-02-12 11:14 -0800
    C program with 2 For loops asetofsymbols@gmail.com - 2018-02-12 12:03 -0800
      C program with 2 For loops asetofsymbols@gmail.com - 2018-02-12 12:15 -0800
        C program with 2 For loops Chad <cdalten@gmail.com> - 2018-02-12 17:11 -0800

csiph-web