0% found this document useful (0 votes)
36 views3 pages

CN-Lab Exp-9

The document provides a C program that implements a frame sorting technique using buffers, where frames are sorted based on their sequence numbers. It includes a Frame structure, a sorting function using bubble sort, and a main function to handle user input and display the frames before and after sorting. An example input and output illustrate the program's functionality.

Uploaded by

harshsing0009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views3 pages

CN-Lab Exp-9

The document provides a C program that implements a frame sorting technique using buffers, where frames are sorted based on their sequence numbers. It includes a Frame structure, a sorting function using bubble sort, and a main function to handle user input and display the frames before and after sorting. An example input and output illustrate the program's functionality.

Uploaded by

harshsing0009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

9. Write a C program for frame sorting technique used in buffers.

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;

void sortFrames(Frame frames[], int n) {


Frame temp;
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (frames[j].sequence > frames[j+1].sequence) {
temp = frames[j];
frames[j] = frames[j+1];
frames[j+1] = temp;
}
}
}
}

int main() {
int n;
printf("Enter the number of frames: ");
scanf("%d", &n);

Frame *frames = (Frame *)malloc(n * sizeof(Frame));

for (int i = 0; i < n; i++) {


printf("Enter sequence number and data for frame %d: ", i + 1);
scanf("%d %s", &frames[i].sequence, frames[i].data);
}

printf("\nFrames before sorting:\n");


for (int i = 0; i < n; i++) {
printf("Frame %d: Sequence: %d, Data: %s\n", i + 1, frames[i].sequence,
frames[i].data);
}

sortFrames(frames, n);

printf("\nFrames after sorting:\n");


for (int i = 0; i < n; i++) {
printf("Frame %d: Sequence: %d, Data: %s\n", i + 1, frames[i].sequence,
frames[i].data);
}

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

Frames after sorting:


Frame 1: Sequence: 1, Data: World
Frame 2: Sequence: 2, Data: Test
Frame 3: Sequence: 3, Data: Hello
This example demonstrates how the frames are read into the buffer, sorted by
their sequence numbers, and then output in the correct order.

You might also like