Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!news.unit0.net!eternal-september.org!feeder.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: C program with 2 For loops Date: Wed, 07 Feb 2018 18:32:28 -0800 Organization: None to speak of Lines: 142 Message-ID: References: <5a7bafd4$0$710$14726298@news.sunsite.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="4b68e4c537f577c5d8fb9e16f2734e7e"; logging-data="9472"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+47byt0znj0fwL6cmi/OkO" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:8zFzElOW1+kzAofMTXNqsczlVmE= sha1:tVHPvw2kXFs0jxJL+z5LxopSbEI= Xref: csiph.com comp.lang.c:126411 arnuld 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 > > 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 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 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"