cs11001/cs11002 programming and data structures (pds...
TRANSCRIPT
![Page 1: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/1.jpg)
CS11001/CS11002Programming and Data Structures
(PDS) (Theory: 3-0-0)
Sudeshna Sarkar
Department of Computer Science and EngineeringIndian Institute of Technology Kharagpur
![Page 2: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/2.jpg)
Euclid’s Algorithm
• Basic Observation: If d divides both m, n, then d divides m-n also, assuming m > n.
• Proof: m=ad, n=bd, so m-n=(a-b)d.
• Converse is also true: If d divides m-n and n, then it divides m too.
• m, n, m-n have the same common divisors.
• The largest divisor of m,n is also the largest divisor of m-n,n.
• Observation: Instead of finding GCD(m,n), we might as well find GCD(n, m-n).
![Page 3: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/3.jpg)
Example
Should we guess that GCD(m,n) = GCD(m%n, n)?
This is not true if m%n = 0, since we have defined GCD only for positive integers. But we can save the situation, as Euclid did.
Euclid’s theorem: Let m>n>0 be positive integers. If n divides m then GCD(m,n) = n. Otherwise GCD(m,n) = GCD(m%n, n).
![Page 4: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/4.jpg)
GCDint main( ) {
int m, n, rem, temp;
scanf (%d %d”, &m, &n);
if (m > n) {
temp = m; m = n; n = temp;
}
while (n != 0) {
rem = m % n;
m = n;
n = rem;
}
printf (“The GCD is %d”, m);
return 0;
}
Are we sure that it
• Terminates?
• Gives the correct answer?
![Page 5: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/5.jpg)
Invariants
• Let M, N be the values typed in by the user into variables m, n.
• We can make the following claim.Just before and just after every iteration, the values of m, n might be different, however, GCD(m,n) = GCD(M,N). Further m,n > 0.
• Such a statement which says that certain property does not change due to the loop iteration is called a loop invariant.
![Page 6: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/6.jpg)
Proof of the invariant
• Clearly, the invariant is true just before the first
iteration.
• Assume invariant is true on entry to some iteration.
• In the iteration, the new values assigned to m,n are as
per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N)
must be true at the end.
• m,n > 0 because we entered the loop only when
m%n != 0.
• But the above argument applies to all iterations.
![Page 7: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/7.jpg)
Proof of (normal) termination
• The value of the variable n must decrease in each iteration. This is because remainder must be less than n
• But n must always be a positive integer in every iteration
• Thus n cannot decrease indefinitely, it cannot go below 1.
• n starts with the value N, thus the algorithm must terminate after at most N iterations.
![Page 8: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/8.jpg)
Termination + Invariant = correctness
• The algorithm terminates, so m%n must have been 0.
• But in this case GCD(m,n) = n, which is what the algorithm prints.
• But this is correct because by the invariant, GCD(m,n) = GCD(M,N) which is what we wanted.
![Page 9: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/9.jpg)
Print all composite numbers from 1 to n
#include<stdio.h>
#include<math.h>
int main() {
int i,j,n, sqrtn;
printf("Enter a number:");
scanf("%d",&n);
for(i=2;i<=n;i++){
sqrtn = (int)(sqrt ((double)(i)));
for(j=2;j<= sqrtn; j++){
if(i%j==0){
printf("%d is composite\n",i);
break;
}
}
}
return 0;
}
![Page 10: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/10.jpg)
Numerical Integration
• Integral from p to q = area under curve.
• Approximate area by rectangles.
• More the better! Suppose n rectangles.
• Total width of rectangles = q-p
• Width of a rect = (𝑞−𝑝)𝑛
• 𝑖th rect begins at p+iw.
• Height of ith rectangle = f(p+iw)
p q
f
![Page 11: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/11.jpg)
Numerical integration to calculate ln(x)#define INTERVALS 1000
int main( ){
float x, area=0, w;
int i;
scanf (“%f”, &x) ;
w = (q-p)/INTERVAL;
for(i=0;i<INTERVAL;i++){
area= area+w*1/(1+i*w);
}
printf(“ln %f = %f\n”,
x, area) ;
return 0;
}
ln(x) = natural logarithm = area under the curve f(x)=1/x from 1 to x.
![Page 12: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/12.jpg)
Arrays
![Page 13: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/13.jpg)
Basic Concept
• Many applications require multiple data items that have common characteristics.
– In mathematics, we often express such groups of data items in indexed form:
• x1, x2, x3, …, xn
• Why are arrays essential for some applications?
![Page 14: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/14.jpg)
Arrays
• Homogenous data types
• All the data items constituting the group share the same name.
int x[10];
• Individual elements are accessed by specifying the index.
x[0] x[1] x[2] x[9]
x is a 10-element one
dimensional array
![Page 15: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/15.jpg)
Computer’s view of Arrays
• The computer’s view of arrays. This will help us better understand:- Where are the elements stored in memory- What happens when an index out of range is used- Function calls using arrays
int q[4] = {11,12,13,14};
• Assuming a single int uses one four byte word, 4 consecutive words of memory are allocated to q.
• Allocated memory used to store the variables q[0], q[1], q[2], q[3] in order.
• Initial values stored.
![Page 16: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/16.jpg)
In general
elemtype aname[alength];
• Block of memory of length S*alength is allocated, where S = size in bytes of a single elemtype
variable.
• aname = starting address of zeroth element = address of allocated block. Const.
• Type of aname:
elemtype *
• Type of aname[i] : elemtype
![Page 17: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/17.jpg)
More examples
• Examples:
int x[10];
char line[80];
float points[150];
char name[35];
• If we are not sure of the exact size of the array, we can define an array of a large size.
int marks[50];
though in a particular run we may only be using, say, 10 elements.
![Page 18: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/18.jpg)
How an array is stored in memory?
• Starting from a given memory location, the successive array elements are allocated space in consecutive memory locations.
• Let
x: starting address of the array in memory
k: number of bytes allocated per array element
– Element a[i] :: allocated memory location at address x + i*k
• First array index assumed to start at zero.
a
x x+k x+2k
Array
![Page 19: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/19.jpg)
Accessing Array Elements
• A particular element of the array can be accessed by specifying two things:– Name of the array.– Index (relative position) of the element in the array.
• In C, the index of an array starts from zero.
• Example:– An array is defined as int x[10];– The first element of the array x can be accessed as x[0], fourth
element as x[3], tenth element as x[9], etc.
• The array index must evaluate to an integer between 0 and n-1 where n is the number of elements in the array.
a[x+2] = 25;b[3*x-y] = a[10-x] + 5;
![Page 20: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/20.jpg)
A Warning
• In C, while accessing array elements, array bounds are not checked.
• Example:int marks[5];
:
:
marks[8] = 75;
– The above assignment would not necessarily cause an error.
– Rather, it MAY result in unpredictable program results.
![Page 21: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/21.jpg)
Initialization of Arrays
• General form:type array_name[size] = { list of values };
• Examples:int marks[5] = {72, 83, 65, 80, 76};
char name[4] = {‘A’, ‘m’, ‘i’, ‘t’};
• Some special cases:– If the number of values in the list is less than the
number of elements, the remaining elements are automatically set to zero.
float total[5] = {24.2, -12.5, 35.1};
total[0]=24.2, total[1]=-12.5, total[2]=35.1, total[3]=0, total[4]=0
![Page 22: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/22.jpg)
Initialization of Arrays
– The size may be omitted. In such cases the compiler automatically allocates enough space for all initialized elements.
int flag[] = {1, 1, 1, 0};
char name[] = {‘A’, ‘m’, ‘i’, ‘t’};
![Page 23: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/23.jpg)
Example 6: Find the minimum of a set of 10 numbers
#include <stdio.h>
int main()
{
int a[10], i, min;
printf(“Give 10 values \n”);
for (i=0; i<10; i++)
scanf (“%d”, &a[i]);
min = 99999;
for (i=0; i<10; i++)
{
if (a[i] < min)
min = a[i];
}
printf (“\n Minimum is %d”, min);
}
Array
declaration
Accessing
Array Element
Reading
Array Element
![Page 24: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/24.jpg)
#include <stdio.h>
#define SIZE 10
int main()
{
int a[SIZE], i, min;
printf(“Give values \n”);
for (i=0; i<SIZE; i++)
scanf (“%d”, &a[i]);
min = 99999;
for (i=0; i<SIZE; i++)
{
if (a[i] < min)
min = a[i];
}
printf (“\n Minimum is %d”, min);
return 0;
}
Alternate
Version 1
Change only one
line to change the
problem size
![Page 25: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/25.jpg)
#include <stdio.h>
int main()
{
int a[100], i, min, n;
printf(“Give number of elements (n) \n”);
scanf (“%d”, &n); /* Number of elements */
printf(“Input all n integers \n”);
for (i=0; i<n; i++)
scanf (“%d”, &a[i]);
min = 99999;
for (i=0; i<n; i++)
{
if (a[i] < min)
min = a[i];
}
printf (“\n Minimum is %d”, min);
return 0;
}
Alternate
Version 2
Define an array of
large size and use
only the required
number of elements
if(n>100) printf(“Array size error!!!”);
![Page 26: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/26.jpg)
Example 7:Computing
GPA
#include <stdio.h>
#define nsub 6
main()
{
int grade_pt[nsub], cred[nsub], i,
gp_sum=0, cred_sum=0, gpa;
printf(“Input gr. points and credits for six subjects \n”);
for (i=0; i<nsub; i++)
scanf (“%d %d”, &grade_pt[i], &cred[i]);
for (i=0; i<nsub; i++)
{
gp_sum += grade_pt[i] * cred[i];
cred_sum += cred[i];
}
gpa = gp_sum / cred_sum;
printf (“\n Grade point average: is %d”, gpa);
}
Handling two arrays
at the same time
![Page 27: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/27.jpg)
Things you cannot do
int a[10], b[10];
You cannot
• use = to assign one array variable to another
a = b; /* a and b are arrays */
• use == to directly compare array variables
if (a = = b) ………..
• directly scanf or printf arrays
printf (“……”, a);
Array in memory
![Page 28: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/28.jpg)
Accessing Array
int a[25],b[25];
• How to copy the elements of one array to another?
– By copying individual elementsfor (j=0; j<25; j++)
a[j] = b[j];
• By reading them one element at a time
for (j=0; j<25; j++)
scanf (“%f”, &a[j]);
– The ampersand (&) is necessary.
– The elements can be entered all in one line or in different lines.
![Page 29: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/29.jpg)
Accessing Array
int a[25];
• Printing Array (array elements)• By printing them one element at a time.
for (j=0; j<25; j++)
printf (“\n %f”, a[j]);
– The elements are printed one per line.printf (“\n”);
for (j=0; j<25; j++)
printf (“ %f”, a[j]);
– The elements are printed all in one line (starting with a new line).
![Page 30: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/30.jpg)
Example 5: computing standard deviation
2
1
1 2( )
N
i
iNx
1
1 N
iiN
x
The Steps1. Read N2. Read Xi
3. Compute Mean 4. Compute Standard Deviation
Do it now!!!
![Page 31: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/31.jpg)
Exercise Problem
• A shop stores n different types of items. Given the number of items of each type sold during a given month, and the corresponding unit prices, compute the total monthly sales.
![Page 32: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/32.jpg)
STRING
![Page 33: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/33.jpg)
STRINGS
• Array of characters
• The size of array must be predefined.
• Usually one extract character is required to store the null character.
• The null character (‘\0’) indicates the end of data/string.
![Page 34: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/34.jpg)
STRINGS Library Functions
• Header file is string.h
• Syntax
– #include <string.h>
• Most frequently used library function:
– strcmp (to compare between two strings)
– strcat (to concatenate one string after another)
– strcpy (to copy one string to another)
– strlen (determines the length of a string)
– ……
![Page 35: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/35.jpg)
strcmp
• #include <string.h>
• int strcmp(const char *s1, const char *s2);
– The strcmp() function compares the two strings s1 and s2. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.
• int strncmp(const char *s1, const char *s2, size_t n);
![Page 36: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/36.jpg)
strcat
• #include <string.h>
• char *strcat(char *dest, const char *src);
– The strcat() function appends the src string to the deststring, overwriting the null byte ('\0') at the end of dest, and then adds a terminating null byte. The strings may not overlap, and the dest string must have enough space for the result.
• char *strncat(char *dest, const char *src, size_t n);
![Page 37: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/37.jpg)
strcpy
• #include <string.h>
• char *strcpy(char *dest, const char *src);
The strcpy() function copies the string pointed to by src, including the terminating null byte ('\0'), to the buffer pointed to by dest. The strings may not overlap, and the destination string dest must be large enough to receive the copy.
• char *strncpy(char *dest, const char *src, size_t n);
![Page 38: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/38.jpg)
Example 8: Check whether a text is a palindrome or not
• WOW
• MOM
• NOON
• LEVEL
• ANNA
• ROTOR
• ROTATOR
R O T ROTA
R R==
R O T ROTA
O O==
R O T ROTA
T T==
R O T ROTA
A
R O T ROTA
![Page 39: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/39.jpg)
Example 8: Check whether a text is a palindrome or not
#include <stdio.h>
#define MAXLEN 100
main()
{
char text[MAXLEN];
int i=0,j,len;
scanf("%s",text);
while(text[i++]!='\0'); /* count the length of the text */
len=i-1; /* length is excluding null character */
printf("Length of the text is %d\n",len);
i=0;
j=len-1;
while(text[i]==text[j]) {
i++;
j--;
if(i>j)
break;
}
(i>j)? printf("%s is a palindrome.\n",text) : printf("%s is NOT a palindrome.\n",text);
}
![Page 40: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/40.jpg)
Example 9: Palindrome testing for case sensitive cases
• Wow
• Mom
• Noon
• Level
• Anna
• Rotor
• Rotator
1. Check is it a lower case character?2. If yes convert to upper case.3. How?
1. If character is between 65 and 90 (including) then it is in upper case.
2. If character is between 97 and 122 (including) then it is in lower case.
3. Subtract 97-65=324. Check for palindrome or execute the
same code.
![Page 41: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/41.jpg)
Exercise 1: Multiple Word Palindromes
• Was it a cat I saw?
• No lemon, no melon
• Borrow or rob?
• ….
![Page 42: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/42.jpg)
Searching an Array:Linear and Binary Search
![Page 43: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/43.jpg)
Searching
• Check if a given element (key) occurs in the array.
![Page 44: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/44.jpg)
Linear Search
• Basic idea:– Start at the beginning of the array.
– Inspect every element to see if it matches the key.
• Time complexity:– A measure of how long an algorithm takes to run.
– If there are n elements in the array:• Best case:
match found in first element (1 search operation)
• Worst case:
no match found, or match found in the last element (n search operations)
• Average:
(n + 1) / 2 search operations
![Page 45: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/45.jpg)
/* If key appears in a[0..size-1], print its location pos, where a[pos] == key. Else print unsuccessful search */
#include <stdio.h>
int main()
{
int size,a[100],key,i,pos;
printf("Enter the number of elements: ");
scanf("%d",&size);
printf("Enter the elements: ");
for(i=0;i<size;i++)
scanf("%d",&a[i]);
printf("Enter the key element: ");
scanf("%d",&key);
for(pos=-1,i=0;i<size;i++) { /* initializing pos as unsuccessful search*/
if(a[i]==key) {
pos=i;
break;
}
}
if(pos==-1)
printf("Unsuccessful search\n");
else
printf("The element is present at %d position\n",pos+1);
return 0;
}
Linear Search
![Page 46: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/46.jpg)
/* If key appears in a[0..size-1], print its location pos, where a[pos] == key. Else print unsuccessful search */
#include <stdio.h>
#include <stdlib.h> /* for exit() function */
#define SIZE 100
void main()
{
int size,a[SIZE],key,i,pos;
printf("Enter the number of elements: ");
scanf("%d",&size);
if(size>SIZE) { /* size is a variable, SIZE is not!! */
printf("Array Size error!!! I am exiting .... \n");
exit(0);
}
printf("Enter the elements: ");
for(i=0;i<size;i++)a
scanf("%d",&a[i]);
printf("Enter the key element: ");
scanf("%d",&key);
for(pos=-1,i=0;i<size;i++) { /* initializing pos as unsuccessful search*/
if(a[i]==key) {
pos=i;
break;
}
}
(pos==-1)? printf("Unsuccessful search\n"):printf("The element is present at %d position\n",pos+1);
}
Linear Search
1. If function type is void thennothing is to be returnedfrom the function.
2. It is always good to assign areturn data type (includingvoid) with each function.
3. exit() will exit from theprogram without executingremaining statements ofthe program. This needsstdlib.h as header file.
![Page 47: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/47.jpg)
Linear Search
int x[ ]= {12,-3,78,67,6,50,19,10} ;
• Trace the following calls :
search 6;
search 5;
Returns 5
Unsuccessful search
![Page 48: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/48.jpg)
Linear Search
• Basic idea:– Start at the beginning of the array.
– Inspect every element to see if it matches the key.
• Time complexity:– A measure of how long an algorithm takes to run.
– If there are n elements in the array:• Best case:
match found in first element (1 search operation)
• Worst case:
no match found, or match found in the last element (n search operations)
• Average:
(n + 1) / 2 search operations
![Page 49: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/49.jpg)
int x[ ]= {-3,6,10,11,12,19,50,67,78} ;
• Trace the following calls :
search 6;
search 5;
Search on Sorted List
int x[ ]= {12,-3,78,67,6,50,19,10,11} ;
![Page 50: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/50.jpg)
Binary Search
• In every step, we reduce the number of elements to search in by half.
• Binary search works if the array is sorted.
– Look for the target in the middle.
– If you don’t find it, you can ignore half of the array, and repeat the process with the other half.
![Page 51: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/51.jpg)
The Basic Strategy
• What do we want?
– Look at [(L+R)/2]. Move L or R to the middle depending on test.
– Repeat search operation in the reduced interval.
0
x:
n-1
L R
x[m]>key
no yes
Elements in Ascending order
m
<=key
L R
>key
RL
![Page 52: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/52.jpg)
Binary Search/* If key appears in x[0..size-1], prints its location pos where
x[pos]==key. If not found, print -1 */
int main ()
{
int x[100],size,key;
int L, R, mid;
_________________;
while ( ____________ )
{
__________________;
}
_________________ ;
}
![Page 53: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/53.jpg)
The basic search iteration/* If key appears in x[0..size-1], prints its location pos where x[pos]==key. If
not found, print -1 */
int main (){
int x[100],size,key;int L, R, mid;_________________;while ( ____________ ){
mid = (L + R) / 2;if (x[mid] > key)
R = mid;else L = mid;
}_________________ ;
}
![Page 54: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/54.jpg)
Loop termination/* If key appears in x[0..size-1], prints its location pos where x[pos]==key. If
not found, print -1 */
int main ()
{
int x[100],size,key;
int L, R, mid;
_________________;
while ( L+1 != R )
{
mid = (L + R) / 2;
if (x[mid] <= key)
L = mid;
else R = mid;
}
_________________ ;
}
![Page 55: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/55.jpg)
Print result/* If key appears in x[0..size-1], prints its location pos where x[pos]==key. If
not found, print -1 */
int main (){
int x[100],size,key;int L, R, mid;_________________;while ( L+1 != R ){
mid = (L + R) / 2;if (x[mid] <= key)
L = mid;else R = mid;
}if (L >= 0 && x[L] = = key) printf(“%d”,L);else printf(“-1”);
}
![Page 56: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/56.jpg)
Initialization/* If key appears in x[0..size-1], prints its location pos where x[pos]==key. If not
found, print -1 */
int main (){
int x[100],size,key;int L, R, mid;
_________________;L = -1; R = size;while ( L+1 != R ){
mid = (L + R) / 2;if (x[mid] <= key)
L = mid;else R = mid;
}if (L >= 0 && x[L] = = key) printf(“%d”,L);else printf(“-1”);
}
![Page 57: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/57.jpg)
Complete C Program/* If key appears in x[0..size-1], prints its location pos where x[pos]==key. If not found, print -1 */
void main ()
{
int x[100],size,key;
int L, R, mid;
printf("Enter the number of elements: ");
scanf("%d",&size);
printf("Enter the elements: ");
for(i=0;i<size;i++)
scanf("%d",&a[i]);
printf("Enter the key element: ");
scanf("%d",&key);
L = -1; R = size;
while ( L+1 != R ) {
mid = (L + R) / 2;
if (x[mid] <= key)
L = mid;
else R = mid;
}
if (L >= 0 && x[L] = = key) printf(“%d”,L);
else printf(“-1”);
}
![Page 58: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/58.jpg)
Binary Search Examples
-17 -5 3 6 12 21 45 63 50
Trace :
binsearch 3;
binsearch 145;
binsearch 45;
Sorted array
L= -1; R= 9; x[4]=12;
L= -1; R=4; x[1]= -5;
L= 1; R=4; x[2]=3;
L=2; R=4; x[3]=6;
L=2; R=3; return L;
![Page 59: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/59.jpg)
Is it worth the trouble ?
• Suppose there are 1000 elements.
• Ordinary search
– If key is a member of x, it would require 500 comparisons on the average.
• Binary search
– after 1st compare, left with 500 elements.
– after 2nd compare, left with 250 elements.
– After at most 10 steps, you are done.
What is best case?What is worst case?
![Page 60: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/60.jpg)
Time Complexity
• If there are n elements in the array.– Number of searches required:
log2n
• For n = 64 (say)– Initially, list size = 64.
– After first compare, list size = 32.
– After second compare, list size = 16.
– After third compare, list size = 8.
– …….
– After sixth compare, list size = 1.
log264 = 6
2k= n,
Where k is the number of steps.
![Page 61: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/61.jpg)
Homework
Modify the algorithm by checking equality with x[mid].
![Page 62: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in/~pds/semester/2017s/SS/array-23-jan-17.pdf · per Euclid’s theorem, and hence GCD(m,n)=GCD(M,N) must be](https://reader036.vdocument.in/reader036/viewer/2022090610/607335b392703d6f8c148614/html5/thumbnails/62.jpg)
Download example programs
Copy paste the following link in the web browse:
http://cse.iitkgp.ac.in/~pralay/teaching/2016a/pds/