Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Assignment # 04

Name : M . Zuhair Mehdi

Reg No : FA20-EEE-020

Submitted To : Mam Nayab


Task # 01
#include <stdio.h>
/*
* Ŝnd nearest index of given key in given array
* @param ptr_array, array of şoat numbers
* @param size, size of array
* @param key to be search
* @param tolerance
*/
int search_nearest(şoat *ptr_array, int size, şoat key, şoat tolerance){
int i=0; // declare and initialize variable i
// search for exact match
for (i=0; i<size; i++){
if(ptr_array[i] == key){ // if exact match is present
return i; // return current index
}
}
// if exact match is not present
// then search with given tolerance
// search for key with given tolerance
int searchIndex = -1;
// variable to store previous difference between key and array element lies in
given tolerance range
şoat dis = 0.0;
for(i=0; i<size; i++){
// calculate absolute difference
şoat diff = ptr_array[i] - key;
if ( diff < 0){
diff = diff * -1;
}
if (diff < tolerance){
// if key is present with given tolerance
if (searchIndex == -1){
searchIndex = i;
dis = diff;
}
}
}
return searchIndex;
}
int main(){
// declare and initialize array of şoat numbers
şoat nums[] = {1.5, -6.7, 7.8, 3.25, 5.0, 6.8};
// Ŝnd index of key
int index = search_nearest(nums, 6, 3.2, .1);
// print index of key;
printf("index = %d",index);
}
Task # 02
#include <stdio.h>
#include<string.h>
//function deŜnation of compare_string
int compare_string(char str1[],char str2[])
{ //declaration of relevant variables
int l1,l2,i=0;
while(1)
{ //if both string are reached to last character and still equal return 0
if(str1[i]=='\0' &&str2[i]=='\0')
return 0;
//if character are not equal return the difference
if(str1[i]!=str2[i])
{
return (str1[i]-str2[i]);
}
i=i+1;
}
}
int main()
{ int diff; char str1[]="hello",str2[]="hey";
diff=compare_string(str1,str2);
printf("differnce between string is: %d",diff);
return 0;
}

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>

using namespace std;

/*
* 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;

// Chosing the partition key as the last element of the array.


float partition_key = *(ptr_array+size-1);

// 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++;
}
}

// Copy over the left partition to the original array.


int index = 0;
for(int i=0; i<index_left; i++, index++) {
*(ptr_array+index) = temp_array_left[i];
}

// Copy over the right partition to the original array.


for(int i=0; i<index_right; i++, index++) {
*(ptr_array+index) = temp_array_right[i];
}

// Return the index of the partition key in the array.


return index_left-1;
}
The main method to test the partition method:

int main() {
float arr[7] = {-6.4, 10.1, 6.7, -1.4, 8.3, -9.6, 3.3};

// Printing the original array.


printf("Printiing the original array: \n");
for(int i=0; i<7; i++) {
printf("%0.1f ", arr[i]);
}
printf("\n");

// Partitioning the array, 3.3 should be used as the partition key.


int index = partition_array(arr, 7);

// Printing the index and partitioned array


// The index should be 3, and all the elements less than 3.3 should be to the
left.
printf("Partition index: %d\n", index);
printf("Printiing the partitioned array: \n");
for(int i=0; i<7; i++) {
printf("%0.1f ", arr[i]);
}

printf("\n");
return 0;
}

Task # 05
#include <stdio.h>

void matrixAddition(int a[2][2],int b[2][2], int c[2][2]){


//track row of the arrays
for(int i = 0;i<2;i++){
//track column of the arrays
for(int j = 0; j<2;j++){
//add corresponding elements
c[i][j] = a[i][j] + b[i][j];
}
}
}

void matrixSubtraction(int a[2][2],int b[2][2], int c[2][2]){


//track row of the arrays
for(int i = 0;i<2;i++){
//track column of the arrays
for(int j = 0; j<2;j++){
//subtract corresponding elements
c[i][j] = a[i][j] - b[i][j];
}
}
}

int main()
{
//array declaration
int a[2][2],b[2][2],c[2][2];
int choice;

//input first array elements


for(int i = 0; i<2;i++){
for(int j = 0; j<2; j++){
printf("Enter a[%d][%d] = ",i,j);
scanf("%d",&a[i][j]);
}
}

//input second array elements


for(int i = 0; i<2;i++){
for(int j = 0; j<2; j++){
printf("Enter b[%d][%d] = ",i,j);
scanf("%d",&b[i][j]);
}
}

printf("\n\n1. Add matrix\n2. Subtract matrix\nEnter your choice(1/2): ");


scanf("%d",&choice);
//calling function matrixAddition() if choice = 1
if(choice == 1)
matrixAddition(a,b,c);

//calling function matrixSubtraction() if choice = 2


else if(choice == 2)
matrixSubtraction(a,b,c);

else{
printf("Invalid choice!! ");
return 0;
}

//print first matrix


printf("\n\n\nMatrix a:\n");
for(int i = 0; i<2;i++){
for(int j = 0; j<2; j++){
printf("%5d ",a[i][j]);
}
printf("\n");
}
//print second matrix
printf("\nMatrix b:\n");
for(int i = 0; i<2;i++){
for(int j = 0; j<2; j++){
printf("%5d ",b[i][j]);
}
printf("\n");
}

//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

// note - the implementation of this function is assumed to given


unsigned char median(unsigned char * ptr_array, int size);
int filter_median(unsigned char * src, unsigned char
* dst, int rows, int cols, int filter_size);

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;
}

You might also like