Professional Documents
Culture Documents
Assignment 4 FA20 EEE 020
Assignment 4 FA20 EEE 020
Reg No : FA20-EEE-020
Task # 03
#include <stdio.h>
#include <string.h>
int string_contains(char *str1,char*str2);
int main()
{
char str1[]="world";
char str2[]="hello world hello";
int index;
index=string_contains(str1,str2);
printf("index=%d",index);
return 0;
}
int string_contains(char *str1,char *str2)
{
/Calculating lenghts/
int len1=strlen(str1);
int len2=strlen(str2);
int i,index,count=0;
/Running loop for str2 to check whether it contains str1/
for(i=0;i<len2;i++)
{
/If charcter of str1 and character of str2 matches/
if(str1==(str2+i))
{
count++;
/Incrementing str1/
str1=str1+1;
/* Storing the index of str2*/
if(count==1)
{
index=i;
}
}
/* If count equals to length of str1*/
else if(count==len1)
{
return index;
}
else
{
count=0;
}
}
/If count equals to length of str1/
if(count==len1)
{
return index;
}
else
{
return -1;
}
}
Task # 04
#include <iostream>
#include <vector>
#include <algorithm>
/*
* This method partitions the given array using a partition key.
* The last element of the array is chosen as the partition key.
* All elements less than or equal to the partitiion key is moved
* to the left of the partition key, and all numbers greater than
* the partition key is moved to its right.
*
* @param ptr_array: the array to be partitioned.
* @param size: size of the array.
* @return: the index of the partition key in the new array.
*/
int partition_array(float* ptr_array, int size) {
// two temporary arrays to store the left and right values of the partition key.
float temp_array_left[size], temp_array_right[size];
// index variables to keep track of the next number to be filled in each partition.
// for example, if index_left is 1, the next element to the left of partition key will
// be stored at index 1 in the above temporary array.
// We initialize both the indexes to 0.
int index_left = 0, index_right = 0;
// We iterate over the array and partition elements into the temporary array.
// The partition key will be the last element in the left array.
for(int i=0; i<size; i++) {
if(*(ptr_array+i) <= partition_key) {
temp_array_left[index_left] = *(ptr_array+i);
index_left++;
} else {
temp_array_right[index_right] = *(ptr_array+i);
index_right++;
}
}
int main() {
float arr[7] = {-6.4, 10.1, 6.7, -1.4, 8.3, -9.6, 3.3};
printf("\n");
return 0;
}
Task # 05
#include <stdio.h>
int main()
{
//array declaration
int a[2][2],b[2][2],c[2][2];
int choice;
else{
printf("Invalid choice!! ");
return 0;
}
//resultant matrix
//print first matrix
printf("\nResultant matrix c:\n");
for(int i = 0; i<2;i++){
for(int j = 0; j<2; j++){
printf("%5d ",c[i][j]);
}
printf("\n");
}
return 0;
}
Task # 06
#include <stdio.h>
#include <stdlib.h>
// function declarations
int main()
{
// no sample run, as no data given
return 0;
}
// function definition
int filter_median(unsigned char * src, unsigned char
* dst, int rows, int cols, int filter_size)
{
// required variables
int i, j, k, l, n = filter_size*filter_size, size=0, half = filter_size/2;
unsigned char *ptr_array;
// maximum size of pointer array can be square of filter_size
ptr_array = (unsigned char *)malloc(n * sizeof(unsigned char));
// loop for all rows
for(i=0; i<rows; i++)
{
for(j=0; j<cols; j++)
{
// find its neighbours
size = 0;
// note - filter_size is always odd
for(k = ((i-half)>=0)? i-half: 0; k<=(i+half) && k<rows; k++)
{
for(l = ((j-half)>=0)? j-half: 0; l<=(j+half) && l<cols; l++)
{
// put neighbour in array
*(ptr_array + size) = *(src + k*cols + l);
// update size
size++;
}
}
// find median and assign to (i, j)th pixel
*(dst + i*cols + j) = median(ptr_array, size);
}
}
return 1;
}