Question

In: Computer Science

C programming Assignment 1. Read two integers into two variables, A and B. Print them on...

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.

Solutions

Expert Solution

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:


Related Solutions

Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and...
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and then find the index of the largest element in the array. You are to write two functions, printArray() and getIndexLargest(), which are called from the main function. printArray() outputs integers to std::cout with a space in between numbers and a newline at the end. getIndexLargest () returns the index of the largest element in the array. Recall that indexes start at 0. If there...
C++ programming language. Write a program that will read in id numbers and place them in...
C++ programming language. Write a program that will read in id numbers and place them in an array.The array is dynamically allocated large enough to hold the number of id numbers given by the user. The program will then input an id and call a function to search for that id in the array. It will print whether the id is in the array or not. Sample Run: Please input the number of id numbers to be read 4 Please...
This is a C programming problem: Construct a doubly linked list: • Read non-zero integers from...
This is a C programming problem: Construct a doubly linked list: • Read non-zero integers from the input and insert them into the linked list. • If an input integer is 0, the program should print all the integers in the list(from tail to head) and then exit.
C++ Write a whole program to read 50 salaries from the file “Salaries.txt” and print them...
C++ Write a whole program to read 50 salaries from the file “Salaries.txt” and print them on the screen (each value on a separate line), you have to also save the grades (each value on a separate line) into another file “SalariesWithBonus.txt” after you add a bonus of two percent to each salary (example For the salary 100000 a bonus of 2000 will be added as 100000 X 0.02 = 2000). Your code should check whether the file “Salaries.txt” opened...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
Problem Definition: Problem: Given an array of integers print all pairs of integers a and b...
Problem Definition: Problem: Given an array of integers print all pairs of integers a and b where a + b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a + b = 16 A= [ 10, 4, 6, 15, 3, 5, 1, 13] The following are pairs that sum to 16: 13, 3 6, 10 15, 1 Your program should print these...
Assignment in C programming class: read the text file line by line, and place each word,...
Assignment in C programming class: read the text file line by line, and place each word, in order, in an array of char strings. As you copy each word into the array, you will keep a running count of the number of words in the text file and convert the letters of each word to lower case. Assume tgat you will use no more than 1000 words in the text, and no more than the first 10 characters in a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT