User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,536 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,097 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 945 | Replies: 4
Reply
Join Date: Oct 2007
Posts: 26
Reputation: bobei89 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
bobei89's Avatar
bobei89 bobei89 is offline Offline
Light Poster

Question Programing calculate student mark and frequecy..no understand, pls help..

  #1  
Oct 14th, 2007
This program is wrotten by me college's teacher, and i so confuse and do not understand why is like this coding.


  1. #include<stdio.h>
  2.  
  3. void main()
  4. {
  5. int i,j,class_size,mark;
  6. int freq[11]={0}; //why make freq[11] for 11 variables?//
  7.  
  8. printf("This program produces a bar chart\n");
  9. printf("showing marks distribution...\n");
  10. printf("\nHow many students?");
  11. scanf("%d", &class_size);
  12. //make frequecy table
  13.  
  14.  
  15. printf("Key in %d marks (Valid mark 0 to 99)\n\n", class_size);
  16. for( i = 1; i<= class_size; i++)
  17. {
  18. printf("Mark #%d:", i);
  19. scanf("%d", &mark);
  20. if (mark>=0 && mark <=99)
  21. freq[mark/10]+=1; //why need to devide by 10?//
  22. else
  23. freq[10]+=1;}
  24.  
  25. //print bar chart
  26.  
  27. printf("\n\nMarks Distribution\n\n");
  28. printf("Mark Range #students Bar Chart\n");
  29. printf("---------------------\n");
  30. for(i=0;i<=9;i++)
  31. {
  32. printf("%d-%d\t\t %d\t",i*10,i*10+9,freq[i]);
  33. for(j=1;j<=freq[i];j++)//do not understand this part also//
  34. printf("*");
  35. printf("\n");
  36. }
  37.  
  38. if (freq[i]>0) //why freq[i]//
  39. printf("Invalid marks \t %d \n", freq[i]);
  40. printf("\n\n***End of Output***\n\n");
  41.  
  42.  
  43. }

Okay here is the end of the program coding. Thx for viewing.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2006
Posts: 1,569
Reputation: Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold 
Rep Power: 12
Solved Threads: 114
Aia's Avatar
Aia Aia is offline Offline
Posting Virtuoso

Re: Programing calculate student mark and frequecy..no understand, pls help..

  #2  
Oct 14th, 2007
int freq[11]={0};  //why make freq[11] for 11 variables?//

Valid user input is from 0 to 99. You divide that range in units of 10 and you get ten groups.
The extra variable is to hold an extra group for any possible input from the user beyond that range.
In other words what doesn't fit in the 0 - 99 it goes to the 11th element of the array.

if (mark>=0 && mark <=99)

Check to see if the user entered the range of 0-99. If it did:

freq[mark/10]+=1; //why need to devide by 10?//

add one to that group unit. Dividing any given number in the 0-99 rangen, by 10 will neatly fit into one of the 10 units group.

else  
freq[10]+=1;}
If the user entered the wrong range, add one to the invalid group.

 for(j=1;j<=freq[i];j++)//do not understand this part also//
This loop is inside another for loop that its mission is to check the i variable against how many times it loops. Increasing variable i by one each time.
The second loop uses the variable i to reference the subscript of the array.
Example:
If the first loop assigns 2 to variable i then in the second loop freq[i] will be freq[2] referencing the 3th element of the array, the one that is in charge
of keeping score for the group from 20 - 29.

if (freq[i]>0) //why freq[i]// 
After exiting the previous loops, variable i is 10 refering to the 11th element of the array. The one in charge of the wrong input group. If that element
has something bigger than 0 it means that along the process an out range input was given, since in the beginning every element was initialized to 0.

By the way. Your teacher did something wrong. main always returns an integer, so it should have been defined as int main ( void ) instead of void main ( ), having a returning
statement before exiting. Something like return 0;
Last edited by Aia : Oct 14th, 2007 at 1:44 pm. Reason: Minor grammar errors
At the very moment that I find myself in the side of the mayority, I will know that I need to re-think my ideas. ~ In my book.
Reply With Quote  
Join Date: Oct 2007
Posts: 26
Reputation: bobei89 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
bobei89's Avatar
bobei89 bobei89 is offline Offline
Light Poster

Re: Programing calculate student mark and frequecy..no understand, pls help..

  #3  
Oct 15th, 2007
Thankyou Aia. I no really understand how does the array work, do you have any sites suggest me to learn about it?

int freq[11] make 11 variables, and freq[10] is address the 11th variable?

Why does the mark input need to be devide by 10, assume i enter mark is 44 and it become 4.4 what it related? @.@
Reply With Quote  
Join Date: Dec 2006
Posts: 1,569
Reputation: Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold 
Rep Power: 12
Solved Threads: 114
Aia's Avatar
Aia Aia is offline Offline
Posting Virtuoso

Re: Programing calculate student mark and frequecy..no understand, pls help..

  #4  
Oct 15th, 2007
Originally Posted by bobei89 View Post
Thankyou Aia. I no really understand how does the array work, do you have any sites suggest me to learn about it?
int freq[11] make 11 variables, and freq[10] is address the 11th variable?
Why does the mark input need to be devide by 10, assume i enter mark is 44 and it become 4.4 what it related? @.@


C language doesn't have a variable type array or string.
An array or declaration of an array in C is just the setting apart of a group of variables of the same type
in consecutive order. Linked all together only by sequence of succession.
As an example:
An article of 11 pages is an array of 11 elements. Would still be an article is no page numbers were
printed at the bottom of each page? Still it would be an array name article by virtue of the arrangement
in the order of the pages.
For easy access to the pages that we want, numbers are assigned to each page. The questions is:
where do we start counting from? Do we include the cover page as page 0 or page 1?
C is clear in the assignment of reference numbers to each element. It always starts with 0.

With that knowledge let's go back to your example:
int freq[11]; /* this tells the compiler that you want eleven pieces of memory that can hold the value
than an int type can hold, and that you want all of them in consecutive order */

How can you access any element in any particular order? We reference each element with a number inside a squared bracket `[]', sometimes called subscript.
However, remember that for C the first number is 0 and not 1.
Therefore if we want to access the 11th element we will have to tell the compiler that we want to access
freq[10].

Division of integers type in C doesn't keep the decimal portion. It can not be store as an int type.
Therefore the result of 44 / 10 is 4, the result of 45 / 10 is 4 and so on, for any number in the range
from 40 to 49 . Knowing that your teacher uses it as a way of accessing the wanted element in freq.
By using that result 4 it knows that freq[4] += 1; will add one to the score for that group recorded in
the 5th element of the array freq.
  1. freq[4]; /* holds the occurrence of instances of any number in the range from 40 - 49 */

Tutorial
Last edited by Aia : Oct 15th, 2007 at 7:32 pm.
At the very moment that I find myself in the side of the mayority, I will know that I need to re-think my ideas. ~ In my book.
Reply With Quote  
Join Date: Oct 2007
Posts: 26
Reputation: bobei89 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
bobei89's Avatar
bobei89 bobei89 is offline Offline
Light Poster

Re: Programing calculate student mark and frequecy..no understand, pls help..

  #5  
Oct 16th, 2007
Yosh~ I understand alot now. Thankyou very much for your help, Aia.
Add reputation for ya. hehe
Last edited by bobei89 : Oct 16th, 2007 at 2:08 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 4:49 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC