943,604 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1269
  • C RSS
Jan 22nd, 2008
0

display array element with passing value...no undesstand~help

Expand Post »
okay, here is the code my teacher gave...but i do not understand some part...

  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4.  
  5. void printfAll(int size, int ary[])
  6. {
  7. int a;
  8.  
  9. for(a=0; a< size; a++)
  10. {
  11. if(!(a%10)) //why a%10 and what not equal to it//
  12. printf("\n");
  13. printf("%5d", ary[a]);
  14. }
  15. printf("\n");
  16. }
  17.  
  18.  
  19. void printOddIdx(int size, int ary[])
  20. {
  21. int a;
  22. for(a=1; a<size; a+=2)
  23. {
  24. if(!((a-1)%20))
  25. printf("\n");
  26. printf("%5d", ary[a]);
  27. }
  28. printf("\n");
  29.  
  30. }
  31.  
  32. void printfOdd(int size, int ary[]) // print the odd values
  33. {
  34. int a, num=0, newline =0;
  35.  
  36. for(a=1; a< size; a++)
  37. {
  38. if(!(num%10)&& !newline)
  39. {
  40. printf("\n");
  41. newline=1;
  42. }
  43. if(ary[a]%2)
  44. {
  45. if(newline)
  46. newline=0;
  47. printf("%5d", ary[a]);
  48. num++;
  49. }
  50.  
  51. }
  52. }
  53.  
  54. long sum (int size, int ary[])
  55. {
  56. int a;
  57. long sum=0;
  58.  
  59. for(a=0; a<size; a++)
  60. sum += ary[a];
  61.  
  62. return sum;
  63. }
  64.  
  65. int largest (int size, int ary[])
  66. {
  67. int a, large_index=0;
  68.  
  69. for(a=0; a<size; a++)
  70. if(ary[a] > ary[large_index])
  71. large_index=a;
  72. return large_index;
  73. }
  74.  
  75. void getNegatives(int ary[])
  76. {
  77. int a, b, negAry[100];
  78.  
  79. for(b=0, a=0; a<100; a++)
  80. {
  81. if(ary[a]<0)
  82. {
  83. negAry[b]=ary[a];
  84. b++;
  85. }
  86. }
  87.  
  88.  
  89. printf("\n\n---random number is divisible by 3 or 7, is stored as negative number---\n");
  90. printfAll(b, negAry);
  91. }
  92.  
  93.  
  94.  
  95.  
  96. main()
  97. {
  98. int aryA[100], i;
  99.  
  100. srand(time(NULL));
  101. for (i=0; i<100; i++)
  102. {
  103. aryA[i] = rand()%999+1;
  104.  
  105. if((aryA[i]%3 ==0)|| (aryA[i]%7 ==0))
  106. aryA[i]=-aryA[i];
  107. }
  108.  
  109.  
  110. printf("\n\n---Prac 2a.Print the array 10 values to a line---\n");
  111. printfAll(100, aryA);
  112.  
  113. printf("\n\n---Prac 2b.Odd numberred index location, ten to a line---\n");
  114. printOddIdx(100, aryA);
  115.  
  116. printf("\n\nPrac 2c.Sum of array %ld\n", sum(100, aryA));
  117.  
  118. printf("\n\nPrac 2d.Largest index of array is = %d\n", largest(100, aryA));
  119.  
  120. getNegatives(aryA);
  121. }
Reputation Points: 14
Solved Threads: 0
Light Poster
bobei89 is offline Offline
26 posts
since Oct 2007
Jan 22nd, 2008
0

Re: display array element with passing value...no undesstand~help

What part to you not understand?
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Jan 22nd, 2008
0

Re: display array element with passing value...no undesstand~help

statement 11th and 38th...i think that for some sort to arrange the value printed. But no understand how it work. Thx for reading my post
Last edited by bobei89; Jan 22nd, 2008 at 7:00 am. Reason: word mispelling
Reputation Points: 14
Solved Threads: 0
Light Poster
bobei89 is offline Offline
26 posts
since Oct 2007
Jan 22nd, 2008
0

Re: display array element with passing value...no undesstand~help

Those statements are used to print a newline after 10 numbers are printed in a row.
% 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.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Jan 22nd, 2008
0

Re: display array element with passing value...no undesstand~help

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.
Reputation Points: 14
Solved Threads: 0
Light Poster
bobei89 is offline Offline
26 posts
since Oct 2007
Jan 22nd, 2008
1

Re: display array element with passing value...no undesstand~help

Click to Expand / Collapse  Quote originally posted by bobei89 ...
if(!(a%10))
but what is not (a%10)?
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jan 23rd, 2008
0

Re: display array element with passing value...no undesstand~help

Yea...i knew why it %20 because it increment is 2, instead of 1....Thx ^^
Reputation Points: 14
Solved Threads: 0
Light Poster
bobei89 is offline Offline
26 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: scanning char array for \0 problem
Next Thread in C Forum Timeline: run program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC