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


Groups > comp.lang.c > #126421

Re: C program with 2 For loops

From Barry Schwarz <schwarzb@dqel.com>
Newsgroups comp.lang.c
Subject Re: C program with 2 For loops
Date 2018-02-08 00:52 -0800
Organization A noiseless patient Spider
Message-ID <or3o7dlbucnnakbjp87g0bp2tcr5nl7knp@4ax.com> (permalink)
References <5a7bafd4$0$710$14726298@news.sunsite.dk> <s1dn7dtdb1i70vqpc12kbmnr0nlh09klim@4ax.com>

Show all headers | View raw


On Wed, 07 Feb 2018 18:36:45 -0800, Barry Schwarz <schwarzb@dqel.com>
wrote:

>On 08 Feb 2018 02:03:00 GMT, arnuld <sunrise@invalid.address> wrote:
>
>>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 };
>
>If you change arrA, you have to remember to change this manually.
>
>Capitalized names are usually reserved for macros.  If you used 
>   #define SIZE sizeof arrA

Obviously everywhere I used sizeof arrA it should be 
   sizeof arrA / sizeof *arrA

>the compiler would probably generate the same code since sizeof is
>known at compile time.  It would also allow you to test with different
>size arrays simply by changing only the initialization of arrA.
>
>>void printArr(int ai[]);
>
>It would be much safer if you passed the dimension into this function
>also.
>
>>int main(void)
>>{
>>  int arrA[] = {1,2,3,4};
>>  int arrB[SIZE+1];
>
>Why do you allow for the potential error that arrB will have a
>different number of elements than arrA.  What is wrong with
>   int arrB{sizeof arrA];
>
>>  int m = 1;
>>
>>  printArr(arrA);
>>  for(int i = 0; i <= SIZE; ++i)
>
>The usual idiom is
>    for (int i = 0; i < sizeof arrA; 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 
>>
>>[arnuld@arch64 programs]$ 

-- 
Remove del for email

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