This code tells us or helps us to understand the use of linear buffer usage.
The code starts with including the header files ‘stdio.h’ and ‘conio.h’ and assigns MAX a value of 5 which means we can use MAX anywhere in our code in place of digit 5.
#include <stdio.h>
#include <conio.h>
#define MAX 5
The code is a menu driven program which gives the user 4 options to choose from that is,
- Checking the buffer valid or not!
- to write elements in buffer
- to write elements in buffer
- exit
In case 1
The user can check if the buffer is valid or not and if the buffer is valid he can also check is the buffer is full then print the elements and if the buffer is not full then he can enter more elements in the buffer
case 1:
printf("Enter the buffer start value 0 only!");
scanf("%d",&start);
buff_check();
break;
In case 2
If the user select case two he can just input the elements in the buffer and the again select from all the cases
In case 3
If the buffer gets full, the user can read all the elements or view all the elements in the buffer
In case 4
If anytime during the program the user wants to get out, then he can enter in case 4 which exits the code
There is also a next case known as default which
Means if the user doesn’t select an option from the given list then he would enter this case which made the list of options display again.
There are 3 functions in the code namely :-
buff_write()
This function asks the user to input elements in buffer whose limit is denoted by MAX and start counter is used to denote or check is the buffer is full or not and if buffer is not full you can enter more elements but in case it gets full it displays a message saying buffer is full so read the elements.
void buff_write()
{
printf("Enter the elements in the buffer up to full!");
if(MAX > start)
{
printf("Enter the element!");
scanf("%d",&arr[start]);
++start;
}
if(MAX==start)
printf("Buffer is full so read the elements from the buffer!");
}
buff_read()
This function is used to display all the elements of the buffer if the buffer gets full, it creates a loop which displays all the elements from the start and as all the elements get displayed the value of start is set to 0 again so more elements could be added, but in case the buffer is not full and still the user selects case 3 then the message displays saying to enter more elements as in case to full the buffer
void buff_read()
{
if(MAX==start)
{
printf("Printing the buffer elements!");
for(start=0;MAX>start;start++)
printf("%d ",arr[start]);
if(MAX==start)
{
start=0;
buff_write();
}
}
else
printf("but buffer is not full so,continue the write elements in buffer!");
}
buff_check()
This functions just checks if the buffer is valid or not which means if it’s value is zero or not as the buffer needs to write from zero and if the value isn’t zero the it shows invalid buffer or buffer is not valid but in case the user enters a value more than the limit of buffer then the screen shows the message of overflow.
void buff_check()
{
if(start!=0)
printf("Buffer is not valid one");
else if(start==0)
printf("buffer is valid");
else if(start>=MAX)
{
printf("overflow the buffer!");
printf("so read the elements!");
}
else
{
printf("buffer is not full");
printf("so enter the elements");
}
}
And this ends our code, the code is simple if you understand the logic.
Download: C Program: Linear Buffer Usage
[sociallocker]
Download: C Program: Linear Buffer Usage
[/sociallocker]