average in c programming






Program to find the total and average sales of all the employee using array structure

#include <stdio.h>

struct Employee
{
    char name[20];
    float sale;
    float average;


};
void main()
{

    int no_of_employee;
struct Employee e[no_of_employee];
printf("###Enter the Details of Employees###");
printf("\nEnter Total Number of Employees:");
scanf("%d", &no_of_employee);
int TotalSales = 0;
int i;
for (i = 0; i < no_of_employee; i++)
{
    printf("\nEnter the employee Name:");
    scanf("%s", e[no_of_employee].name);
    printf("\nEnter the Total Number of Sales done by %s:", e[no_of_employee].name);
    scanf("%d", &e[no_of_employee].sale);
    TotalSales = TotalSales + e[no_of_employee].sale;

}

printf("Total Sales of  Employee:%d", TotalSales);
//Finding  Average of sales

float Average = (no_of_employee) / TotalSales;
printf("Average sales of all the employee:%f", Average);
     
    
}

 In this article we are going to see how to write program average  sales of an employee using structure.

so Let's start the explanation of above program line by line.

In the first line  of code we are including stdio.h header file which will help us to some of the built-in function's like printf() and scanf() it will help us for printing the output on console and taking input from console respectively.

In the next line of code, we have created one structure with name Employee and with 3 field name,sales,average in which name field is of char(Character) data type with size 20 mean's our name can of 20 character.sale is of int(Integer) type,we will use this field to store how much sale is done in numbers and average field is of float type and will store the average of sale in this field.We can create any number of instance of this Employee Structure.

from the next line of code,we are starting our main function,in this we will write our whole programming logic,we can also say that the main function is a starting point of any c program.

In the next line of code,we have declared one variable of type int with name no_of_employee,in this variable we are going to store the total.of employee of whom we have to store data and we  will use this variable in for loop as well.

In the next line  code we are creating e object array of type Employee with size no_of_employee using struct keyword,here we are using less memory for array by using dynamic size of e array.

In the next line,we have printed one statement "###Enter the Details of Employees###" it will help user to indicate that we have to provide some input and after this we have also printed one more statement "\nEnter Total Number of Employees:" here we have used \n because we want this statement into the next line and here the execution will pause since in the next line we have used scanf("%d", &no_of_employee);which will actually pause the execution,here we have used %d format specifier because we are storing the input in variable no_of_employee and of type int.

In the next line we have declared one more variable TotalSales and assigned 0 to it,In this variable we will total the total_no_sales done by all employee.In the next line we have declared one more variable i of type int,we will use this variable as a iterative variable in below for loop.

from the next line of code we have used one for loop which will actually helps us to take user input for all employee one by one 

The sample value of no_of_employee = 3

1st Iteration

i=0 //(for i=0 ;i<no_of_employee;i++) The base value of i is 0 and 

0<3 // Condition Satisfied since i < no_of_employee Therefore the control will go inside the for loop 

Inside the for loop

printf("\nEnter the employee Name:");//This statement will help user to understand that we have provide employee name 

scanf("%s", e[no_of_employee].name);//This statement will take user input and store it in e[0].name means this name will be stored in 0th index of e and in name field of it and here we have used %s format specifier because we are using here character array which is nothing but worked as string.

 printf("\nEnter the Total Number of Sales done by %s:", e[no_of_employee].name);//here we are printing one more statement 








If you any have doubt,suggestion regarding this post or website then feel free to share with us.
 
If you have learned something new from this post then share this post with your family and
friends.
 
Happy Learning :)😎  


Previous Next

2 Comments

Previous Post Next Post