Session 4
-
Upload
shailendra-mathur -
Category
Technology
-
view
135 -
download
0
description
Transcript of Session 4
![Page 1: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/1.jpg)
ARRAY in C • Array is a collection or group of similar data type
elements stored in contiguous memory.
• The individual data items can be characters, integers, floating points numbers and so on .
• Here contiguous memory allocation means array occupies contiguous bytes as needed in the memory.
![Page 2: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/2.jpg)
Declaring single dimension Arrays
• We can declare an array by specify its data type, name and the number of elements the array holds between square brackets immediately following the array name.
• Here is the syntax:data_type array_name[size];
![Page 3: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/3.jpg)
Declaring single dimension Array• For example, to declare an integer array which
contains 10 elements we can do as follows:
int values[10];
![Page 4: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/4.jpg)
Accessing Single Dimension Array• We can access array elements via
indexes array_name[index]. Indexes of array starts from 0
• With each subscript enclosed in square brackets.
• For example,values[0]=5;values[1]=10;printf(“%d”,values[1]);
![Page 5: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/5.jpg)
Initializing Arrays• It is like a variable, an array can be initialized.
• To initialize an array, you provide initializing values which are enclosed within curly braces in the declaration and placed following an equals sign after the array name.
• Here is an example of initializing an integer array.
int list[5] = {2,1,3,7,8};
![Page 6: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/6.jpg)
Multi dimensional Arrays:• Often there is a need to store and manipulate two
dimensional data structure such as matrices & tables.
• Here the array has two subscripts. One subscript denotes the row & the other the column.
• The declaration of two dimension arrays is as follows:data_type array_name[row_size][column_size]; int m[10][20];
![Page 7: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/7.jpg)
Declaring Multi Dimensional Arrayfloat table [50][50]; char page[50][50];
int num[3][3]; 0 1 2012
![Page 8: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/8.jpg)
Accessing two dimensional arraysint values[2][2];values[0][0]=1; values[0][1]=2;values[1][0]=3; values[1][1]=4;
int num[2][2]={1,2,3,4};int matrix[3][3] ={{11,12,13}, {21,22,23}, {32,31,33}};
![Page 9: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/9.jpg)
ARRAY AND STRINGS • The gets and puts functions:
• The gets and puts functions facilitate the transfer of strings between the computer and the standard input/output devices.
• Each of these functions accepts a single argument. The argument must be a data item that represents a string(e.g. character array).
![Page 10: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/10.jpg)
Example of using gets and puts#include<stdio.h> void main() {
char inne[80]; gets(line); puts(line);
} The gets and puts functions offer simple alternatives to the use of scanf and printf for reading and displaying strings
![Page 11: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/11.jpg)
String library functions• strlen() • This function counts a number of characters
present in a string while giving a call to the function.
char msg[] = “Lord krishna”; int n; n=strlen(msg); printf(“length of string =%d”, n);
![Page 12: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/12.jpg)
• strcpy() • This function copies the contents of one string
to another. • The base address of source and target strings
are supplied to the function.
char source[] = “Lord Krishna”; char target[15]; strcpy(target, source); printf(“Source string is : %s”, source); printf(“target string is :%s”,target);
String library functions
![Page 13: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/13.jpg)
• strcat() • This function concatenates the source string
at the end of target sting. char s[] = “Lord”; char t[] = “Krishna”; strcat(t,s); printf(“source string is %s\n”, s); printf(“target stirng is %s\n”, t);
String library functions
![Page 14: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/14.jpg)
String library functions• strcmp() • This function compares two strings and returns some
integer value. • If both the strings are equal then it returns 0 otherwise
it returns some other integer value.void main() {
char s1[]=”Jerry”; char s2[]=”Ferry”; int j= strcmp(s1, s2); printf(“%d”, j);
}
![Page 15: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/15.jpg)
Understanding Functions• What is Function?• A function in C language is a block of code that
performs a specific task.
• It is reusable i.e. it can be executed from as many different parts in a C Program as required.
• It also optionally returns a value to the calling program
![Page 16: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/16.jpg)
Structure of a Function<return type> FunctionName (Argument1, Argument2, Argument3……){
Statement1;Statement2;
}int sum (int x, int y){
int result;result = x + y;return (result);
}
![Page 17: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/17.jpg)
Types of functions:A function may belong to any one of the following categories:
• Functions with no arguments and no return values.
• Functions with arguments and no return values.• Functions with arguments and return values.• Functions that return multiple values.• Functions with no arguments and return values.
![Page 18: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/18.jpg)
Advantages of using functions:• It makes possible top down modular
programming.
• The length of the source program can be reduced.
• It becomes uncomplicated to locate and separate a faulty function for further study.
• c programmer can use function written by others, instead of starting over from scratch.
![Page 19: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/19.jpg)
Function Example#include <stdio.h>void printMessage (){
printf ("Programming is fun.\n");}void main (){
printMessage ();}
![Page 20: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/20.jpg)
![Page 21: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/21.jpg)
Function Which Return Values#include <stdio.h>int add (int x,int y){
int result; result=x+y;return(result);
}void main (){
int total;total=add(10,12);
}
![Page 22: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/22.jpg)
Recursive Function• Recursive function is a function which
contains a call to itself.
• Recursive function allows you to divide your complex problem into identical single simple cases which can handle easily.
• This is also a well-known computer programming technique: divide and conquer.
![Page 23: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/23.jpg)
Example of recursive function# include<stdio.h>int factorial(int number){
if(number <= 1) return 1; return number * factorial(number - 1);}void main(){ int x = 5; printf("factorial of %d is %d",x,factorial(x));}
![Page 24: Session 4](https://reader035.fdocuments.in/reader035/viewer/2022070315/5550f580b4c90501448b46ed/html5/thumbnails/24.jpg)
Scope Rule of Functions void main( ) {
int i = 20 ; display ( i ) ;
} void display ( int j ) {
int k = 35 ; printf ( "\n%d", j ) ; printf ( "\n%d", k ) ;
}