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

Multimedia Task PDF

This C++ program implements the Shannon-Fano encoding algorithm. It takes in a set of symbols and their frequencies, sorts them by frequency, then recursively assigns binary codes to each symbol based on splitting the range in half at each level based on total frequency. It outputs the encoded symbols. The second part of the code implements a simple run length encoding algorithm that counts repeated characters and outputs the character and count.

Uploaded by

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

Multimedia Task PDF

This C++ program implements the Shannon-Fano encoding algorithm. It takes in a set of symbols and their frequencies, sorts them by frequency, then recursively assigns binary codes to each symbol based on splitting the range in half at each level based on total frequency. It outputs the encoded symbols. The second part of the code implements a simple run length encoding algorithm that counts repeated characters and outputs the character and count.

Uploaded by

Mohamed Zaki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

//c++ shannon fano program

#include <stdlib.h>
#include <sdkddkver.h>
#include <stdio.h>
#include <tchar.h>
#include <stdio.h>

int g_level =0;


void shannon(int start, int end , int arr[20], char code[20][20],int level){
int i =start;
int j = end;
int isum=arr[i], jsum=arr[j];
if(level > g_level){
g_level = level;
}
while (i<(j-1))
{
while (isum>jsum &&i<(j-1))
{
j--;
jsum += arr[j];
}
while (isum<jsum &&i<(j-1))
{
i++;
isum += arr[i];
}
}

if (i == start)
{
code[start][level]= '0';
}else if ((i - start)>=1)
{
for (int k = start; k <= i; ++k)
{
code[k][level]='0';
}
shannon(start, i, arr, code, level + 1);
}
if (j == end)
{
code[end][level]= '1';
}else if ((end - j )>=1)
{
for (int k = j; k <= end; ++k)
{
code[k][level]='1';
}
shannon(j, end, arr, code, level + 1);
}

}
int _tmain(int argc, _TCHAR* argv[]){
int arr[20];
int n, i,j;
printf("Enter the number of symbole: ");
scanf_s("%d", &n);

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


{
int s;
printf("Enter the symbol frquence: ");
scanf_s("%d", &s);
j=i;

while (j && arr[j- 1]<s)


{
arr[j]= arr[j - 1];
j--;
}
arr[j]= s;
}

char code[20][20];
for ( i = 0; i < n; ++i)
{
for (j=0; j < n; j++)
{
code[i][j]='X';
}
}
shannon(0, n-1,arr,code, 0);

printf("\n\n DATA CODE\n");


for (i = 0; i < n; ++i)
{
printf("\n %4d : " ,arr[i]);
for (j = 0; j <= g_level; j++)
{
if (code[i][j] != 'X')
{
printf("%c ", code[i][j]);
}
}
}

system("pause");
return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
// C++ program to implement run length encoding
#include<iostream>
//#include<bits/stdc++.h>
using namespace std;

void printRLE(string str)


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

// Count occurrences of current character


int count = 1;
while (i < n - 1 && str[i] == str[i + 1]) {
count++;
i++;
}

// Print character and its count


cout << str[i] << count;
}
}

int main()
{
string str = "wwwwaaadexxxxxxywww";
printRLE(str);
return 0;
}

You might also like