•
•
•
•
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
![]() |
This program is wrotten by me college's teacher, and i so confuse and do not understand why is like this coding.
Okay here is the end of the program coding. Thx for viewing.
c Syntax (Toggle Plain Text)
#include<stdio.h> void main() { int i,j,class_size,mark; int freq[11]={0}; //why make freq[11] for 11 variables?// printf("This program produces a bar chart\n"); printf("showing marks distribution...\n"); printf("\nHow many students?"); scanf("%d", &class_size); //make frequecy table printf("Key in %d marks (Valid mark 0 to 99)\n\n", class_size); for( i = 1; i<= class_size; i++) { printf("Mark #%d:", i); scanf("%d", &mark); if (mark>=0 && mark <=99) freq[mark/10]+=1; //why need to devide by 10?// else freq[10]+=1;} //print bar chart printf("\n\nMarks Distribution\n\n"); printf("Mark Range #students Bar Chart\n"); printf("---------------------\n"); for(i=0;i<=9;i++) { printf("%d-%d\t\t %d\t",i*10,i*10+9,freq[i]); for(j=1;j<=freq[i];j++)//do not understand this part also// printf("*"); printf("\n"); } if (freq[i]>0) //why freq[i]// printf("Invalid marks \t %d \n", freq[i]); printf("\n\n***End of Output***\n\n"); }
Okay here is the end of the program coding. Thx for viewing.
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;}
for(j=1;j<=freq[i];j++)//do not understand this part also//
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]//
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.
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? @.@
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? @.@
•
•
•
•
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.
c Syntax (Toggle Plain Text)
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- fUNTIONS (C++)
- Procedure for MAX/MIN (Pascal and Delphi)
- need someone help me with C++ (C++)
- Javascript Help (JavaScript / DHTML / AJAX)
- Javascript Help (Java)
- Many Errors while doing this assignment (C)
Other Threads in the C Forum
- Previous Thread: reading in arguments for execv()
- Next Thread: Help Neeeded!!!!!!!!!!11



Linear Mode