CN-Lab Exp-9
CN-Lab Exp-9
Ans:
Below is a C program that demonstrates a simple frame sorting technique using
buffers. This program will read frames into a buffer, sort them based on their
sequence numbers, and then output the sorted frames.
C Program for Frame Sorting Technique in Buffers
c
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int sequence;
char data[100];
} Frame;
int main() {
int n;
printf("Enter the number of frames: ");
scanf("%d", &n);
sortFrames(frames, n);
free(frames);
return 0;
}
Explanation
1. Frame Structure:
o The Frame struct represents a data frame with a sequence number
and data content.
2. sortFrames Function:
o The sortFrames function sorts an array of frames based on their
sequence numbers using a simple bubble sort algorithm.
3. Main Function:
o The main function prompts the user to enter the number of
frames and their sequence numbers and data.
o It then prints the frames before and after sorting to demonstrate
the sorting technique.
Example
Input:
Enter the number of frames: 3
Enter sequence number and data for frame 1: 3 Hello
Enter sequence number and data for frame 2: 1 World
Enter sequence number and data for frame 3: 2 Test
Output:
Frames before sorting:
Frame 1: Sequence: 3, Data: Hello
Frame 2: Sequence: 1, Data: World
Frame 3: Sequence: 2, Data: Test