0% found this document useful (0 votes)
739 views2 pages

Prog 9-Frame Sorting

This C program splits a message string into frames of fixed size, assigns sequence numbers, shuffles the frames randomly, and then sorts the frames back into their original order. It defines structures to store the frame data, functions for assigning sequence numbers, generating random numbers, shuffling frames, and sorting frames. The main function gets user input, splits it into frames, shuffles and sorts the frames, and prints the output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
739 views2 pages

Prog 9-Frame Sorting

This C program splits a message string into frames of fixed size, assigns sequence numbers, shuffles the frames randomly, and then sorts the frames back into their original order. It defines structures to store the frame data, functions for assigning sequence numbers, generating random numbers, shuffling frames, and sorting frames. The main function gets user input, splits it into frames, shuffles and sorts the frames, and prints the output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include<stdio.

h>
#include<string.h>
#define FRAM_TXT_SIZ 3
#define MAX_NOF_FRAM 127
char str[FRAM_TXT_SIZ*MAX_NOF_FRAM];

struct frame // structure maintained to hold frames


{
char text[FRAM_TXT_SIZ];
int seq_no;
}fr[MAX_NOF_FRAM], shuf_ary[MAX_NOF_FRAM];
int assign_seq_no() //function which splits message
{
int k=0,i,j; //into frames and assigns sequence no
for(i=0; i < strlen(str); k++)
{
fr[k].seq_no = k;
for(j=0; j < FRAM_TXT_SIZ && str[i]!='\0'; j++)
fr[k].text[j] = str[i++];
}
printf("\nAfter assigning sequence numbers:\n");
for(i=0; i < k; i++)
printf("%d:%s ",i,fr[i].text);
return k; //k gives no of frames
}

void generate(int *random_ary, int limit) //generate array of random nos


{
int r, i=0, j;
while(i < limit)
{
r = random()%limit;
printf("rno%%d=%d\n",i,r);
for(j=0; j < i; j++)
if( random_ary[j] == r )
break;
if( i==j )
random_ary[i++] = r;
}
}

void shuffle(int no_frames ) // function shuffles the frames


{
int i, k=0, random_ary[no_frames];
generate(random_ary, no_frames);
for(i=0; i < no_frames; i++)
shuf_ary[i] = fr[random_ary[i]];
printf("\n\nAFTER SHUFFLING:\n");
for(i=0; i < no_frames; i++)
printf("%d:%s ",shuf_ary[i].seq_no,shuf_ary[i].text);
}

void sort(int no_frames) // sorts the frames


{
int i,j,flag=1;
struct frame hold;
for(i=0; i < no_frames-1 && flag==1; i++) // search for frames in sequence
{
flag=0;
for(j=0; j < no_frames-1-i; j++) //(based on seq no.) and display
if(shuf_ary[j].seq_no > shuf_ary[j+1].seq_no)
{
hold = shuf_ary[j];
shuf_ary[j] = shuf_ary[j+1];
shuf_ary[j+1] = hold;
flag=1;
}
}
}

int main()
{
int no_frames,i;
clrscr();
printf("Enter the message: ");
gets(str);
no_frames = assign_seq_no();
shuffle(no_frames);
sort(no_frames);
printf("\n\nAFTER SORTING\n");
for(i=0;i<no_frames;i++)
printf("%s",shuf_ary[i].text);
printf("\n\n");
getch();
return 0;
}

You might also like