| | |
display array element with passing value...no undesstand~help
Thread Solved |
okay, here is the code my teacher gave...but i do not understand some part...
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <time.h> #include <stdlib.h> void printfAll(int size, int ary[]) { int a; for(a=0; a< size; a++) { if(!(a%10)) //why a%10 and what not equal to it// printf("\n"); printf("%5d", ary[a]); } printf("\n"); } void printOddIdx(int size, int ary[]) { int a; for(a=1; a<size; a+=2) { if(!((a-1)%20)) printf("\n"); printf("%5d", ary[a]); } printf("\n"); } void printfOdd(int size, int ary[]) // print the odd values { int a, num=0, newline =0; for(a=1; a< size; a++) { if(!(num%10)&& !newline) { printf("\n"); newline=1; } if(ary[a]%2) { if(newline) newline=0; printf("%5d", ary[a]); num++; } } } long sum (int size, int ary[]) { int a; long sum=0; for(a=0; a<size; a++) sum += ary[a]; return sum; } int largest (int size, int ary[]) { int a, large_index=0; for(a=0; a<size; a++) if(ary[a] > ary[large_index]) large_index=a; return large_index; } void getNegatives(int ary[]) { int a, b, negAry[100]; for(b=0, a=0; a<100; a++) { if(ary[a]<0) { negAry[b]=ary[a]; b++; } } printf("\n\n---random number is divisible by 3 or 7, is stored as negative number---\n"); printfAll(b, negAry); } main() { int aryA[100], i; srand(time(NULL)); for (i=0; i<100; i++) { aryA[i] = rand()%999+1; if((aryA[i]%3 ==0)|| (aryA[i]%7 ==0)) aryA[i]=-aryA[i]; } printf("\n\n---Prac 2a.Print the array 10 values to a line---\n"); printfAll(100, aryA); printf("\n\n---Prac 2b.Odd numberred index location, ten to a line---\n"); printOddIdx(100, aryA); printf("\n\nPrac 2c.Sum of array %ld\n", sum(100, aryA)); printf("\n\nPrac 2d.Largest index of array is = %d\n", largest(100, aryA)); getNegatives(aryA); }
Those statements are used to print a newline after 10 numbers are printed in a row.
% is the modulus operator.
Comment out those lines and see how the output changes. The values printed will be the same, but the output will be in a single line.
% is the modulus operator.
a%10 is 0 for numbers like 10, 20, and so on. So after the 10th number, the rest of the array is printed in a newline. The same happens after printing the 20th number and so on. Comment out those lines and see how the output changes. The values printed will be the same, but the output will be in a single line.
if(!(a%10))
but what is not (a%10)?
And statement 24th which is:
if(!((a-1)%20))
Why it %20 instead of 10. When i put replace it to 10 it just show 5 value in row then newline.
Also the statement 38th which is:
if(!(num%10)&& !newline)
I removed the newline and it come out that output is became messy. I do not know why.
Thx for helping me.
but what is not (a%10)?
And statement 24th which is:
if(!((a-1)%20))
Why it %20 instead of 10. When i put replace it to 10 it just show 5 value in row then newline.
Also the statement 38th which is:
if(!(num%10)&& !newline)
I removed the newline and it come out that output is became messy. I do not know why.
Thx for helping me.
if( !( a % 10 ) ) What's the programmer trying to achieve here?
Is looking for when the result of the operation a % 10 would be 0, however 0 inside an if statement will never execute since in C, 0 is considerate FALSE. Hence the ! added inside the if() to force its execution.
With this information in hand, think about the other examples you're querying about.
Is looking for when the result of the operation a % 10 would be 0, however 0 inside an if statement will never execute since in C, 0 is considerate FALSE. Hence the ! added inside the if() to force its execution.
With this information in hand, think about the other examples you're querying about.
![]() |
Other Threads in the C Forum
- Previous Thread: scanning char array for \0 problem
- Next Thread: run program
| Thread Tools | Search this Thread |
* ansi api array arrays binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o inches include infiniteloop input interest intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






