This program is wrotten by me college's teacher, and i so confuse and do not understand why is like this coding.

#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. :D

Aia commented: Thank you for using the proper code tags +4
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 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;

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? @.@

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.

freq[4]; /* holds the occurrence of instances of any number in the range from 40 - 49 */

Tutorial

Yosh~ I understand alot now. Thankyou very much for your help, Aia. :D
Add reputation for ya. hehe :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.