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

Thread Solved
Reply

Join Date: Oct 2007
Posts: 26
Reputation: bobei89 is an unknown quantity at this point 
Solved Threads: 0
bobei89's Avatar
bobei89 bobei89 is offline Offline
Light Poster

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

 
0
  #1
Jan 22nd, 2008
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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

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

 
0
  #2
Jan 22nd, 2008
What part to you not understand?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 26
Reputation: bobei89 is an unknown quantity at this point 
Solved Threads: 0
bobei89's Avatar
bobei89 bobei89 is offline Offline
Light Poster

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

 
0
  #3
Jan 22nd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

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

 
0
  #4
Jan 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 26
Reputation: bobei89 is an unknown quantity at this point 
Solved Threads: 0
bobei89's Avatar
bobei89 bobei89 is offline Offline
Light Poster

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

 
0
  #5
Jan 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,002
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 172
Aia's Avatar
Aia Aia is offline Offline
Postaholic

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

 
1
  #6
Jan 22nd, 2008
Originally Posted by bobei89 View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 26
Reputation: bobei89 is an unknown quantity at this point 
Solved Threads: 0
bobei89's Avatar
bobei89 bobei89 is offline Offline
Light Poster

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

 
0
  #7
Jan 23rd, 2008
Yea...i knew why it %20 because it increment is 2, instead of 1....Thx ^^
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC