PPS II Mid Answers
PPS II Mid Answers
85 53 96 35 27 87 37 12 90 23
Sorting is the process of arranging the data in some logical order. Bubble sort is an algorithm to
sort various linear data structures. The logical order can be ascending and descending. which each
pair of adjacent elements is compared and the elements are swapped if they are not in order. This
algorithm is not suitable for large data sets as its average and worst case complexity are of O(n2)
where n is the number of items. in the case of numeric values or dictionary order in the case of
alphanumeric values.
Algorithm:
Step 1: Start
begin
for j= 0 to n - 1 increment j by 1
begin
begin
t = a[j];
a[j + 1] = t;
end
end
end
Step 4: for i: 0 to n
Print a[i]
Step 5: Stop
The complexity of sorting algorithm is depends upon the number of comparisons that
are made.
Total comparisons in Bubble sort is: n ( n – 1) / 2 ≈ n 2 – n
Best case : O (n2 )
Average case : O (n2 )
Worst case : O (n2)
Step1:
Comparing adjacent Elements
85 53 96 35 27 87 37 12 90 23
53 85 96 35 27 87 37 12 90 23
53 85 96 35 27 87 37 12 90 23
85 53 35 96 27 87 37 12 90 23
85 53 35 27 96 87 37 12 90 23
85 53 35 27 87 96 37 12 90 23
85 53 35 27 87 37 96 12 90 23
85 53 35 27 87 37 12 96 90 23
85 53 35 27 87 37 12 90 96 23
85 53 35 27 87 37 12 90 23 96
Step 2:
85 53 35 27 87 37 12 90 23 96
53 85 35 27 87 37 12 90 23 96
53 35 85 27 87 37 12 90 23 96
53 35 27 85 87 37 12 90 23 96
53 35 27 85 87 37 12 90 23 96
53 35 27 85 87 37 12 90 23 96
53 35 27 85 37 87 12 90 23 96
53 35 27 85 37 12 87 90 23 96
53 35 27 85 37 12 87 90 23 96
53 35 27 85 37 12 87 23 90 96
Step3:
53 35 27 85 37 12 87 23 90 96
35 53 27 85 37 12 87 23 90 96
35 27 53 85 37 12 87 23 90 96
35 27 53 85 37 12 87 23 90 96
53 35 27 37 12 85 87 23 90 96
53 35 27 37 12 85 87 23 90 96
53 35 27 37 12 85 23 87 90 96
Step4:
53 35 27 37 12 85 23 87 90 96
35 53 27 37 12 85 23 87 90 96
35 27 53 37 12 85 23 87 90 96
35 27 37 53 12 85 23 87 90 96
35 27 37 12 53 85 23 87 90 96
35 27 37 12 53 85 23 87 90 96
35 27 37 12 53 23 85 87 90 96
Step5:
27 35 37 12 53 23 85 87 90 96
27 35 37 12 53 23 85 87 90 96
27 35 12 37 53 23 85 87 90 96
27 35 12 37 53 23 85 87 90 96
27 35 12 37 23 53 85 87 90 96
Step6
27 35 12 37 23 53 85 87 90 96
27 12 35 37 23 53 85 87 90 96
27 12 35 37 23 53 85 87 90 96
27 12 35 23 37 53 85 87 90 96
Step7:
27 12 35 23 37 53 85 87 90 96
12 27 35 23 37 53 85 87 90 96
12 27 35 23 37 53 85 87 90 96
12 27 23 35 37 53 85 87 90 96
Step8:
12 27 23 35 37 53 85 87 90 96
12 27 23 35 37 53 85 87 90 96
12 23 27 35 37 53 85 87 90 96
Write a program for searching a key element in the array using Linear Search method.
Linear search in C to find whether a number is present in an array. If it's present, then at what
location it occurs. It is also known as a sequential search. It is straightforward and works as
follows: we compare each element with the element to search until we find it or the list ends.
Program:
#include<stdio.h>
int main()
{
int i, a[20], n, key, flag = 0;
printf("Enter the size of an array \n");
scanf("%d", &n);
printf("Enter the array elements");
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("Enter the key elements");
scanf("%d", &key);
for(i = 0; i < n; i++)
{
if(a[i] == key)
{
flag = 1;
break;
}}
if(flag == 1)
printf("The key elements is found at location %d", i);
else
printf("The key element is not found in the array");
return 0;
}
#include<stdio.h>
int main()
{
int a[20][20],b[20][20],c[20][20];
int m,n,p,i,j,k;
printf("Enter rows and columns of matrix A\n");
scanf("%d%d",&m,&p);
printf("Enter rows and columns of matrix B\n");
scanf("%d%d",&n,&p);
if(n!=p)
{
printf("Matrix multiplication not possible\n");
return 0;
}
printf("Enter elements of matrix A\n");
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter elements of matrix B\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<p;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("Product of two matrices is\n");
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
printf("%d\n",c[i][j]);
}
}
}
Example:
#include <stdio.h>
const int M = 3;
const int N = 3;
int main()
{
int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
print(arr);
return 0;
}
Illustrate the use of any five String manipulation functions using programs?
C supports a number of string handling functions. All of these built-in functions are
aimed atPerforming various operations on strings and they are defined in the header file
string.h
strlen( )
This function is used to find the length of the string excluding the NULL character. In
other words, this function is used to count the number of characters in a string. Its
syntax is as follows:
syntax:
int cse_c(string)
Example: char str1[ ] = “WELCOME”;
int n;
n = strlen(str1);
Program:
#include<stdio.h>
#include<string.h>
int main()
{
char string1[50];
int length;
printf("\n Enter any string:");
gets(string1);
length=strlen(string1);
printf("\n The length of string=%d",length);
}
ii). strcpy( )
Strcpy(str1,str2);
where string1 and string2 are one-dimensional character arrays.This function copies the
content of string2 to string1.
Program:
#include<stdio.h>
#include<string.h> int main( )
{
char string1[30],string2[30];
printf("\n Enter first string:");
gets(string1);
printf("\n Enter second string:");
gets(string2);
strcpy(string2,string1);
printf("\n First string=%s",string1);
printf("\n Second string=%s",string2);
}
(iii). strcmp ( )
This function compares two strings character by character (ASCII comparison) and
returns one of three values {-1,0,1}. The numeric difference is „0‟ if strings are equal .If
it is negative string1is alphabetically above string2 .If it is positive string2 is alphabetically
above string1.
int strcmp(string1,string2);
Program:
#include<stdio.h>
#include<string.h>
main()
{
char string1[30],string2[15];
int x;
printf(“\n Enter first string:”);
gets(string1);
printf(“\n Enter second string:”);
gets(string2);
x=strcmp(string1,string2);
if(x==0)
printf(“\n Both strings are
equal”); else if(x>0)
printf(“\n First string is bigger”);
else
printf(“\n Second string is bigger”);
}
iv). strcat ( )
This function is used to concatenate two strings. i.e., it appends one string at the end of
thespecified string. Its syntax as follows:
strcat(string1,string2);
where string1 and string2 are one-dimensional character arrays. This function joins two
strings together. In other words, it adds the string2 to string1 and the string1 contains the
final concatenated string.
E.g., string1 contains prog and string2 contains ram, then string1 holds program after
execution of the strcat() function.
#include<stdio.h>
#include<string.h>
main()
{
char string1[30],string2[15];
printf(“\n Enter first string:”);
gets(string1);
printf(“\n Enter second string:”);
gets(string2);
strcat(string1,string2);
printf(“\n Concatenated string=%s”,string1);
}
strrev( ):
strrev( ) function reverses a given string in C language. Syntax for strrev( ) function is given
below.
char *strrev(char *string);
Program:
#include <stdio.h>
#include <string.h>
int main() {
char string[100];
char ch, *ptr;
printf("Enter any string\n");
gets(string);
printf("Enter any character to search\n");
scanf("%c", &ch);
ptr = strchr(string, ch);
if(NULL == ptr){
printf("'%c' not found in string \"%s\"\n", ch, string);
} else {
printf("String after %c is \"%s\"\n", ch, ptr);
}
return(0);
}
Increment/Decrement of a Pointer
Comparison of pointers
Increment/Decrement of a Pointer
Increment: When you increment a pointer in C, it moves to the next memory location based on
the size of the data it points to.
For example, if you have an integer pointer and you increment it, it will jump to the next integer-
sized space in memory (usually 4 bytes). The same applies to float pointers; they move by the size
of a float (typically 4 bytes) when incremented. This way, pointers stay aligned with the data they
point to.
If you have an integer pointer and you decrement it, it moves back by 4 bytes (assuming a
standard 4-byte integer on most systems).
If you have a float pointer and you decrement it, it also moves back by 4 bytes (assuming a
standard 4-byte float).
When you add an integer to a pointer, the integer value is first multiplied by the size of the data
type to which the pointer points, and then the result is added to the pointer’s address.
example:
When you add the integer 5 to it with the expression ptr = ptr + 5, the final address stored
in ptr will be calculated as follows: 1000 + sizeof(int) * 5, which is equal to 1020.
When you subtract an integer from a pointer, the integer value is first multiplied by the size of the
data type to which the pointer points, and then the result is subtracted from the pointer’s address.
example:
When you subtract the integer 5 from it with the expression ptr = ptr - 5, the final address
stored in ptr will be calculated as follows: 1000 - sizeof(int) * 5, which is equal to 980.
#include<stdio.h>
int main(){
int number=50;
p=p-3;
return 0;
}
Write a program to reverse a given string without using built-in function strrev( ).
#include <stdio.h>
#include<string.h>
void main()
{
char s[10],d[10];
int len,i,flag;
printf("enter the data into s: ");
gets(s);
len=strlen(s);
for(i=0;i<len;i++)
{
if(s[i]!=s[len-i-1])
{
flag=1;
break;
}
}
if(flag==0)
{
printf("yes");
}
else{
printf("no");
}
}
Write a C program for exchanging of two values using Call by reference mechanism.
#include<stdio.h>
void swap(int *,int *) ; // function declaration
void main(){
int num1, num2 ;
num1 = 10 ;
num2 = 20 ;
printf("\nBefore swap: num1 = %d, num2 = %d", num1, num2) ;
swap(&num1, &num2) ; // calling function
printf("\nAfter swap: num1 = %d, num2 = %d", num1, num2);
}
void swap(int *a, int *b) // called function
{
int temp ;
temp = *a ;
*a = *b ;
*b = temp ;
}
How to create array of strings? Explain with suitable example
An array is essentially a collection of elements. And the data types for all elements
must be the same and stored at the contiguous memory location. Similarly, an array of
strings means a collection of several strings.
1-D Array Of Strings:
String is a one-dimensional array of characters which is terminated by delimiter \0
(null). Thus, C uses variable-length delimited strings in programs.
Syntax:
str[6]=”Hello”
Program:
#include<stdio.h>
int main()
{
char str[8];
printf("Enter a String: "); scanf("%s", &str);
printf("%s", str);
}
2-D array of Strings:
An array of strings is a two-dimensional array of character-type arrays where each character
array (string) is null-terminated..
Syntax:
data_type arrayName [size1][size2] = {string1, string2, ..., string
{
sum=sum+*(a+i);
}
printf("The sum of Array is: %d",sum);
return 0;
}
malloc() is a function that creates one calloc() is a function that assigns a specified number of
1. block of memory of a fixed size. blocks of memory to a single variable.
4. malloc() has high time efficiency calloc() has low time efficiency
6. Syntax : void* malloc(size_t size); Syntax : void* calloc(size_t num, size_t size);
Develop a C program to create file and copy the content of that file into another?
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fptr1, *fptr2;
char filename[100];
int c;
printf("Enter the filename to open for reading: ");
scanf("%s", filename);
// Open one file for reading
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s\n", filename);
exit(1);
}
printf("Enter the filename to open for writing: ");
scanf("%s", filename);
a) getc(); It is used to read characters from file that has been opened for read operation.
This statement reads a character from file pointed to by file pointer and assign to c. It returns an
end-of-file marker EOF, when end of file as been reached
b) fscanf();
This function is similar to that of scanf function except that it works on files.
Example fscanf(f1,”%s%d”,str,&num)
The above statement reads string type data and integer types data from file.
c) getw();
d) fgets():
This function reads a string from a file pointed by a file pointer. It also copies the string to a
memory location referred by an array.
e) fread():
This function is used for reading an entire structure block from a given file.
Syntax: fread(&struct_name,sizeof(struct_name),1,filepointer);
a. putc(): This function writer a character to a file that has been opened in write mode.
Syntax: This statement writes the character contained in character variable c into a file whose
pointer is fp.
Syntax: fprintf(f1,”%s,%d”,str,num);
d) fputs(): This function writes a string into a file pointed by a file pointer.
Synatax: fputs(string,filepointer);
e) fwrite(): This function is used for writing an entire block structure to a given file.
Syntax: fwrite(&struct_name,sizeof(struct_name),1,filepointer);
Closing a file:
Syntax:
fclose(file_pointer_name);
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
FILE *fptr;
char str[100];
fptr=fopen("ABC.TXT","r");
printf("%d\n",fptr);
do
{
fscanf(fptr,"%s",str);
printf("%s ",str);
}
while(!feof(fptr));
fclose(fptr);
}