Docsity

C Programming Complete Notes for Beginners (B.Tech | Exam Ready), Study notes of C programming

Toggle sidebar
bg1
Q&A for Previous Year Questions
Subject: Programming for Problem Solving (B.Tech. I Year)
--------------------------------------------------------------------------------------------------------------------------------------
1
1.
Explain the algorithm for finding roots of quadratic equation.
Ans:
The general form of a quadratic equation is (ax2 + bx + c = 0). The highest degree in a quadratic
equation is 2. Hence, a quadratic equation will have two roots.
Solving a quadratic equation:
The formula to find the roots of a quadratic equation is given as follows
x = [-b +/- sqrt(-b^2 - 4ac)]/2a
The discriminant of the quadratic equation is
k = (b^2 - 4ac).
Depending upon the nature of the discriminant, the roots can be found in different ways.
1. If the discriminant is positive, then there are two distinct real roots.
2. If the discriminant is zero, then the two roots are equal.
3. If the discriminant is negative, then there are two distinct complex roots.
Algorithm to find all the roots of a quadratic equation:
1. Input the value of a, b, c.
2. Calculate k = b*b - 4*a*c
3. If (d < 0)
Display "Roots are Imaginary, calculater1 = (-b +i*sqrt(k))/ 2a and r2 =(b + i*sqrt(k))/ 2a.
else if (d = 0)
Display "Roots are Equal" and calculate r1 = r2 = (-b / 2*a)
else
Display "Roots are real and calculate r1 = -b + sqrt(k) / 2*a and r2 = -b - sqrt(k) / 2*a
4. Print r1 and r2.
5. End the algorithm
UNIT-V
Introduction to Algorithms: Algorithms for finding roots of quadratic equations, finding
Minimum and maximum numbers of a given set, finding if a number is prime number, etc.
Basic searching in an array of elements (linear and binary search techniques),
Basic algorithms to sort array of elements (Bubble, Insertion and Selection sort algorithms),
Basic concept of order of complexity through the example programs
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download C Programming Complete Notes for Beginners (B.Tech | Exam Ready) and more Study notes C programming in PDF only on Docsity!

1. Explain the algorithm for finding roots of quadratic equation.

Ans: The general form of a quadratic equation is (ax^2 + bx + c = 0). The highest degree in a quadratic equation is 2. Hence, a quadratic equation will have two roots. Solving a quadratic equation: The formula to find the roots of a quadratic equation is given as follows x = [-b +/- sqrt(-b^2 - 4ac)]/2a The discriminant of the quadratic equation is k = ( b^2 - 4ac). Depending upon the nature of the discriminant, the roots can be found in different ways.

  1. If the discriminant is positive , then there are two distinct real roots.
  2. If the discriminant is zero , then the two roots are equal.
  3. If the discriminant is negative , then there are two distinct complex roots.

Algorithm to find all the roots of a quadratic equation:

  1. Input the value of a, b, c.
  2. Calculate k = bb - 4a*c
  3. If (d < 0) Display "Roots are Imaginary, calculater1 = (-b +isqrt(k))/ 2a and r2 =(b + isqrt(k))/ 2a. else if (d = 0) Display "Roots are Equal" and calculate r1 = r2 = (-b / 2a) else Display "Roots are real and calculate r1 = - b + sqrt(k) / 2a and r2 = - b - sqrt(k) / 2*a
  4. Print r1 and r2.
  5. End the algorithm

UNIT-V

Introduction to Algorithms: Algorithms for finding roots of quadratic equations, finding Minimum and maximum numbers of a given set, finding if a number is prime number, etc. Basic searching in an array of elements (linear and binary search techniques), Basic algorithms to sort array of elements (Bubble, Insertion and Selection sort algorithms), Basic concept of order of complexity through the example programs

2. Write a C Program for finding roots of quadratic equation. Ans : #include #include int main() { double a, b, c, discriminant, root1, root2, realPart, imaginaryPart; printf("Enter coefficients a, b and c: "); scanf(“%lf %lf %lf”,&a, &b, &c); discriminant = bb-4ac; if (discriminant > 0) // condition for real and different roots { // sqrt() function returns square root root1 = (-b+sqrt(discriminant))/(2a); root2 = (-b-sqrt(discriminant))/(2a); printf("root1 = %.2lf and root2 = %.2lf",root1 , root2); } else if (discriminant == 0) //condition for real and equal roots { root1 = root2 = -b/(2a); printf(“root1 = root2 = %.2lf;”, root1); } else // if roots are not real { realPart = -b/(2a); imaginaryPart = sqrt(-discriminant)/(2a); printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imaginaryPart, realPart, imaginaryPart); } return 0; }

4. Write a C Program to generate prime number series between m and n, where m and n are

integers.

Ans: Program: #include void main() { int m,n, i, j, count; printf("Prime no.series\n"); printf("Enter m and n values\n"); scanf("%d%d",&m, &n); printf("The prime numbers between %d to %d\n",m,n); for(i = m; i <= n; i++) { count = 0; for(j = 1; j <=i; j++) if(i % j == 0) { count++; } if(count == 2) { printf("%d\t", i); } } } Input & Output: Prime no. series Enter m and n Values 2 10 The prime numbers between 2 to 10 2 3 5 7

5. Write an algorithm and program to find the minimum and maximum number in a given set.

Ans: Problem Description: Given an array A[] of size n, you need to find the maximum and minimum element present in the array. Your algorithm should make the minimum number of comparisons. For Example: Input: A[] = { 4, 2, 0, 8, 20, 9, 2} Output: Maximum: 20, Minimum: 0 Searching linearly: Increment the loop by 1 We initialize both minimum and maximum element to the first element and then traverse the array, comparing each element and update minimum and maximum whenever necessary. Pseudo-Code: int [] getMinMax( int A[], int n) { int max = A[ 0 ] int min = A[ 0 ] for ( i = 1 to n- 1 ) { if ( A[i] > max ) max = A[i] else if ( A[i] < min ) min = A[i] } // By convention, let ans[0] = maximum and ans[1] = minimum int ans[ 2 ] = {max, min}; return ans } Program: #include #define MAX_SIZE 100 // Maximum array size int main() { int arr[MAX_SIZE]; int i, max, min, size; printf("Enter size of the array: "); /* Input size of the array */ scanf("%d", &size);

6. What is meant by searching? Explain the linear search algorithm with example?

Ans: Searching:

Searching is an operation or a technique that helps finds the place of a given element or value in the list. Any search is said to be successful or unsuccessful depending upon whether the element that is being searched is found or not. Some of the standard searching techniques that is being followed in the data structure is listed below:

 Linear Search or Sequential Search  Binary Search

Linear Search:  Linear Search is the simplest searching algorithm.  It traverses the array sequentially to locate the required element.  It searches for an element by comparing it with each element of the array one by one.  So, it is also called as Sequential Search. Linear Search Algorithm is applied when-

 No information is given about the array.  The given array is unsorted or the elements are unordered.  The list of data items is smaller.

Linear Search Algorithm:

Consider-

 There is a linear array „a‟ of size „n‟.  Linear search algorithm is being used to search an element „item‟ in this linear array.  If search ends in success, it sets loc to the index of the element otherwise it sets loc to - 1. Linear_Search (a , n , item , loc) { for i = 0 to (n - 1 )

{ if (a[i] == item) { set loc = i Exit } } set loc = - 1 }

Linear Search Example:

Consider-

 We are given the following linear array.

 Element 15 has to be searched in it using Linear Search Algorithm.

Now,

 Linear Search algorithm compares element 15 with all the elements of the array one by one.

 It continues searching until either the element 15 is found or all the elements are searched.

Linear Search Algorithm works in the following steps-

Step- 01 :

 It compares element 15 with the 1st^ element 92.

 Since 15 ≠ 92, so required element is not found.

 So, it moves to the next element.

Step-02:

 It compares element 15 with the 2nd^ element 87.

 Since 15 ≠ 87, so required element is not found.

 So, it moves to the next element.

Step-03:

 It compares element 15 with the 3rd^ element 53.

 Since 15 ≠ 53, so required element is not found.

 So, it moves to the next element.

Step-04:

 It compares element 15 with the 4th^ element 10.

 Since 15 ≠ 10, so required element is not found.

 So, it moves to the next element.

Step-05:

 It compares element 15 with the 5th^ element 15.

 Since 15 = 15, so required element is found.

 Now, it stops the comparison and returns index 4 at which element 15 is present.

8. Expalin the Binary Search Algorithm with an Example?

Ans: Searching-  Searching is a process of finding a particular element among several given elements.  The search is successful if the required element is found.  Otherwise, the search is unsuccessful.

Binary Search:  Binary Search is one of the fastest searching algorithms.  It is used for finding the location of an element in a linear array.  It works on the principle of divide and conquer technique.  Binary Search Algorithm can be applied only on Sorted arrays.

So, the elements must be arranged in-  Either ascending order if the elements are numbers.  Or dictionary order if the elements are strings. To apply binary search on an unsorted array,  First, sort the array using some sorting technique.  Then, use binary search algorithm.

Binary Search Algorithm- Consider-  There is a linear array „a‟ of size „n‟.  Binary search algorithm is being used to search an element „item‟ in this linear array.  If search ends in success, it sets loc to the index of the element otherwise it sets loc to - 1.  Variables beg and end keeps track of the index of the first and last element of the array or sub array in which the element is being searched at that instant.  Variable mid keeps track of the index of the middle element of that array or sub array in which the element is being searched at that instant. int binary_search(int a[], int beg, int end, int item) { Set beg = 0 Set end = n- 1 while ( (beg <= end ) and (a[mid] ≠ item) ) { Set mid = (beg + end ) / 2 if (item < a[mid]) { Set end = mid - 1 } Else if(item >a[mid]) { Set beg = mid + 1 } else

{ Set loc=mid; } } Set loc = - 1 } Explanation Binary Search Algorithm searches an element by comparing it with the middle most element of the array. Then, following three cases are possible- Case- 01

If the element being searched is found to be the middle most element, its index is returned. Case- 02 If the element being searched is found to be greater than the middle most element, then its search is further continued in the right sub array of the middle most element. Case- 03

If the element being searched is found to be smaller than the middle most element, then its search is further continued in the left sub array of the middle most element.

This iteration keeps on repeating on the sub arrays until the desired element is found or size of the sub array reduces to zero.

Binary Search Example- Consider-  We are given the following sorted linear array.  Element 15 has to be searched in it using Binary Search Algorithm.

Binary Search Algorithm works in the following steps-

Step-01:  To begin with, we take beg=0 and end=6.  We compute location of the middle element as- mid= (beg + end) / 2 = (0 + 6) / 2 = 3

 Here, a[mid] = a[3] = 20 ≠ 15 and beg < end.  So, we start next iteration. Step-02:  Since a[mid] = 20 > 15, so we take end = mid – 1 = 3 – 1 = 2 whereas beg remains unchanged.  We compute location of the middle element as- mid= (beg + end) / 2

9. Write a c program to implement Binary Search Algorithm using Recursive and Non- Recursive(Iteartive) approach? Ans: Bianary Searh:

The binary search algorithm is an algorithm that is based on compare and split mechanism. The binary Search algorithm is also known as half-interval search, logarithmic search, or binary chop. The binary search algorithm, search the position of the target value in a sorted array. It compares the target value with the middle element of the array. If the element is equal to the target element then the algorithm returns the index of the found element. And if they are not equal, the searching algorithm uses a half section of that array, Based on the comparison of the value, the algorithm uses either of the first-half ( when the value is less than the middle ) and the second half ( when the value is greater than the middle ). And does the same for the next array half. To implement the binary search we can write the code in two ways. these two ways defer in only

the way we call the function that checks for the binary search element. they are:

Using iterations : this means using a loop inside the function that checks for the equality of the middle element.

Using recursion: In this method, the function calls itself again and again with a different set of values.

Using Iterative or Non- Recursive Approach:

include int iterativeBsearch(int A[], int size, int element); int main() { int A[] = { 0 , 12 , 6 , 12 , 12 , 18 , 34 , 45 , 55 , 99 }; int n= 55 ; printf("%d is found at Index %d \n",n,iterativeBsearch(A, 10 ,n)); return 0 ; } int iterativeBsearch(int A[], int size, int element) { int start = 0 ; int end = size- 1 ; while(start<=end) { int mid = (start+end)/ 2 ; if( A[mid] == element) {

return mid; } else if( element < A[mid] ) { end = mid- 1 ; } else { start = mid+ 1 ; } } return - 1 ;

}

Using Recursive Approach:

#include

int RecursiveBsearch(int A[], int start, int end, int element)

{

if(start>end) return - 1 ; int mid = (start+end)/ 2 ; if( A[mid] == element ) return mid; else if( element < A[mid] ) RecursiveBsearch(A, start, mid- 1 , element); else RecursiveBsearch(A, mid+ 1 , end, element);

}

int main()

{

int A[] = { 0 , 2 , 6 , 11 , 12 , 18 , 34 , 45 , 55 , 99 },n= 55 ; printf("%d is found at Index %d \n",n,RecursiveBsearch(A, 0 , 9 ,n)); return 0 ;

}

Bubble sort starts with very first two elements, comparing them to check which one is greater.

In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare 33 with 27.

We find that 27 is smaller than 33 and these two values must be swapped.

The new array should look like this −

Next we compare 33 and 35. We find that both are in already sorted positions.

Then we move to the next two values, 35 and 10.

We know then that 10 is smaller 35. Hence they are not sorted.

We swap these values. We find that we have reached the end of the array. After one iteration, the array should look like this −

To be precise, we are now showing how an array should look like after each iteration. After the second iteration, it should look like this −

Notice that after each iteration, at least one value moves at the end.

And when there's no swap required, bubble sorts learns that an array is completely sorted.

Algorithm void bubbleSort( int a[], int n ) {

for i = 0 to n- { for j = 0 to n- { /* compare the adjacent elements / if list[j] > list[j+1]) { / swap them */ temp=list[j] list[j]=list[j+1]; list[j+1]=temp; } } } }

12. Write a C Program to implement Bubble Sort Algorithm.

Ans:

Program: Live Demo #include #define MAX 10 int list[MAX] = {1,8,4,6,0,3,5,2,7,9}; void display() { int i; printf("["); // navigate through all items for(i = 0; i < MAX; i++) { printf("%d ",list[i]); } printf("]\n"); }

For the first position in the sorted list, the whole list is scanned sequentially. The first position where 14 is stored presently, we search the whole list and find that 10 is the lowest value.

So we replace 14 with 10. After one iteration 10, which happens to be the minimum value in the list, appears in the first position of the sorted list.

For the second position, where 33 is residing, we start scanning the rest of the list in a linear manner.

We find that 14 is the second lowest value in the list and it should appear at the second place. We swap these values.

After two iterations, two least values are positioned at the beginning in a sorted manner.

The same process is applied to the rest of the items in the array. Following is a pictorial depiction of the entire sorting process −

Algorithm

void selection sort( int list[], int n)// list : array of items n : size of list { for i = 1 to n - 1 { /* set current element as minimum/ min = i / check the element to be minimum / for j = i+1 to n { if (list[j] < list[min]) { min = j; } } / swap the minimum element with the current element*/ temp= list[min] list[min]=list[i] list[i]=temp; } }

14. Write a C program to implement selection sort Algorithm?

Ans: Program: #include #define MAX 7 int intArray[MAX] = { 4 , 6 , 3 , 2 , 1 , 9 , 7 }; void display() { int i; printf("["); // navigate through all items for(i = 0 ;i < MAX;i++) { printf("%d ", intArray[i]); } printf("]\n"); } void selectionSort() { int indexMin,i,j,temp; // loop through all numbers for(i = 0 ; i < MAX- 1 ; i++) { // set current element as minimum indexMin = i;