In: Computer Science
C programming Assignment
1. Read two integers into two variables, A and B. Print them on terminals. Write functions to Swap them (1) using a temporary variable and (2) without using a temporary variable.
2. Read 10 integers into an array. Sort the array. Then print the sorted array.
3. Read 16 integers into an array. Use merge sort algorithm to sort it.
4. Read 16 integers into an array. Sort it using merge sort algorithm. Then read a number to be searched in the array. Then in no more than 4 comparisons, decide if the given number is in the array or not and print the result.
SOLUTION 1:
#include<stdio.h>
void swap(int *num1,int *num2);
void withoutSwap(int *num1,int *num2);
void swap(int *num1,int *num2){
//store one variable in temp and swap the
variables
int temp=*num1;
*num1=*num2;
*num2=temp;
}
void withoutSwap(int *num1,int *num2){
// it is like if we 19+20 = 39 if we want to get one
number if we substract number 2 we will get number1 -> 39-19=20
or
// 39-19 = 20 this is as simple as this
*num1=*num1-*num2;
*num2=*num1+*num2;
*num1=*num2-*num1;
}
int main(){
int a,b;
printf("enter Two variable :");
//read values
scanf("%d%d",&a,&b);
printf("number 1 ->%d, number 2->
%d\n",a,b);
swap(&a,&b);
//we need to use the pointers to keep the updated
value
printf("numbers after swapping number 1->%d,number
2-> %d\n",a,b);
//without using the temp variable we are swapping
after swapping so it will get back a,b to normal values
withoutSwap(&a,&b);
printf("numbers after swapping number for 2nd Time
1->%d,number 2-> %d\n",a,b);
return 0;
}
CODE :
OUTPUT :
SOLUTION 2
#include<stdio.h>
int main(){
//declare array of size 10 and i,j for looping over
data and temp variable used in the swaping of the data
int a[10],i,j,temp;
for(i=0;i<10;i++){
printf("enter %d :",i);
scanf("%d",&a[i]);
}
//this is simple bubble sort
// where at every time we will determine the largest
value and keep it at the end and continue till we sort all the
data
for(i=0;i<10;i++ ){
for(j=0;j<10-i-1;j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
//print the sorted data
for(i=0;i<10;i++){
printf("Value at %d is %d
\n",i,a[i]);
}
return 0;
}
CODE:
OUTPUT:
SOLUTION 3:
#include<stdio.h>
// Merge function which will sort the data
void merge(int a[], int left, int mid, int right)
{
//n and n1 are the index to determine the left array elements index
and rihgt aray elements index
int i, j, k,n = mid - left + 1,n1 = right - mid;;
//create temperory arrays to store the data
int Left[n], Right[n1];
// copy the data into the left and right arrays
for (j = 0; j < n1; j++)
Right[j] = a[mid + 1+ j];
for (i = 0; i < n; i++)
Left[i] = a[left + i];
// starting index of the left t subarray
i = 0;
//starting index of right subarray
j = 0;
//index to that is used to store the original merged
array
k = left;
while (i < n && j < n1)
{
//if the element in the right array is less than or equal to the
left side array element push it to the result array
if (Left[i] >= Right[j])
{
a[k] = Right[j];
j++;
}
// if the element in the left side array is small then
copy that element to the original array
else
{
a[k] = Left[i];
i++;
}
k++;
}
//copy the left over elements of the right array to the final
array
while (j < n1)
{
a[k] = Right[j];
j++;
k++;
}
//copy the left over elements of the left array to the final
array
while (i < n)
{
a[k] = Left[i];
i++;
k++;
}
}
//here arr is the array that needs to be sorted and left is the
left index of the array and right is the right index of the
array
// it will divide the data into small parts
void mergeSort(int a[], int left, int right)
{
if (left < right)
{
//First we need to divide the array into parts and then sort them
and we need to combine them
int middle = left+(right-left)/2;
// Sort the individual half left,right
mergeSort(a, left, middle);
mergeSort(a, middle+1, right);
//sort the data and merge the data
merge(a, left, middle, right);
}
}
int main(){
//declare array of size 10 and i,j for looping over
data and temp variable used in the swaping of the data
int a[16],i,j,temp;
for(i=0;i<16;i++){
printf("enter %d :",i);
scanf("%d",&a[i]);
}
// call the merge sort function to do sort
// we need to pass array we need to and the left and
right values here we dont have left so we pass it as 0 and right
as
//length of the array
mergeSort(a,0,15);
//print the sorted data
for(i=0;i<16;i++){
printf("Value at %d is %d
\n",i,a[i]);
}
return 0;
}
CODE IMAGE :
OUTPUT:
SOLUTION 4:
EXPLANATION:
1 Binary search is the algorithm which will search data with log2(n) complecity which is log2(24 )-> 4
2. So for this we have used the binary search
SEARCH ALGORITHM:
int search(int a[], int left, int right, int key)
{
//we will divide the array till the array cant be divided like only
one variable is left that is left equals to right
if (right >= left) {
int mid = left + (right - left) / 2;
// if element found
if (a[mid] == key)
return mid;
//if mid element is greater than means that the search element will
be present on the left side of th epresent element so we
// need to check on the left side
else if (a[mid] > key)
return search(a, left, mid - 1, key);
//if mid element is less than search key means that the search
element will be present on the right side of th epresent element so
we
// need to check on the right side
return search(a, mid + 1, right, key);
}
// We reach here when element is not
// present in array
return -1;
}
CODE:
#include<stdio.h>
int search(int a[], int left, int right, int key);
// Merge function which will sort the data
void merge(int a[], int left, int mid, int right)
{
//n and n1 are the index to determine the left array elements index
and rihgt aray elements index
int i, j, k,n = mid - left + 1,n1 = right - mid;;
//create temperory arrays to store the data
int Left[n], Right[n1];
// copy the data into the left and right arrays
for (j = 0; j < n1; j++)
Right[j] = a[mid + 1+ j];
for (i = 0; i < n; i++)
Left[i] = a[left + i];
// starting index of the left t subarray
i = 0;
//starting index of right subarray
j = 0;
//index to that is used to store the original merged
array
k = left;
while (i < n && j < n1)
{
//if the element in the right array is less than or equal to the
left side array element push it to the result array
if (Left[i] >= Right[j])
{
a[k] = Right[j];
j++;
}
// if the element in the left side array is small then
copy that element to the original array
else
{
a[k] = Left[i];
i++;
}
k++;
}
//copy the left over elements of the right array to the final
array
while (j < n1)
{
a[k] = Right[j];
j++;
k++;
}
//copy the left over elements of the left array to the final
array
while (i < n)
{
a[k] = Left[i];
i++;
k++;
}
}
//here arr is the array that needs to be sorted and left is the
left index of the array and right is the right index of the
array
// it will divide the data into small parts
void mergeSort(int a[], int left, int right)
{
if (left < right)
{
//First we need to divide the array into parts and then sort them
and we need to combine them
int middle = left+(right-left)/2;
// Sort the individual half left,right
mergeSort(a, left, middle);
mergeSort(a, middle+1, right);
//sort the data and merge the data
merge(a, left, middle, right);
}
}
//this is the binary search function where it divides the list half
and checks if the value at the half index is greater or lesser if
it is
//lesser it will check on the right side else it will check on the
right side
int search(int a[], int left, int right, int key)
{
//we will divide the array till the array cant be divided like only
one variable is left that is left equals to right
if (right >= left) {
int mid = left + (right - left) / 2;
// if element found
if (a[mid] == key)
return mid;
//if mid element is greater than means that the search element will
be present on the left side of th epresent element so we
// need to check on the left side
else if (a[mid] > key)
return search(a, left, mid - 1, key);
//if mid element is less than search key means that the search
element will be present on the right side of th epresent element so
we
// need to check on the right side
return search(a, mid + 1, right, key);
}
// We reach here when element is not
// present in array
return -1;
}
int main(){
//declare array of size 10 and i,j for looping over
data and temp variable used in the swaping of the data
int a[16],i,j,temp,key;
for(i=0;i<16;i++){
printf("enter %d :",i);
scanf("%d",&a[i]);
}
// call the merge sort function to do sort
// we need to pass array we need to and the left and
right values here we dont have left so we pass it as 0 and right
as
//length of the array
mergeSort(a,0,15);
//print the sorted data
for(i=0;i<16;i++){
printf("Value at %d is %d
\n",i,a[i]);
}
printf("Enter the element to search");
scanf("%d",&key);
//call the search function and store the return
value
i=search(a,0,15,key);
if(i!=-1){
printf("Element is at Index
%d",i);
}
else{
printf("Element not found");
}
return 0;
}
CODE IMAGE
OUTPUT: