Module-3: Arrays, Strings and Functions

Size: px
Start display at page:

Download "Module-3: Arrays, Strings and Functions"

Transcription

1 Introduction to Arrays: Module-3: Arrays, Strings and Functions Normally, the programmer makes the use of scalar variable to store and process single value. However, it is necessary for the programmer to store and process large volume of data in computer s memory while developing the programs to solve complex problems and is possible by making the use of data structure or derived data type named array. The examples to store and process large volume of data in computers memory by using an array are as follow. i. To store and process roll numbers of 100 students int rno[100]; ii. To store and process total marks of 100 students int marks[100]; iii. To store and process name of a candidate char name[25]; iv. To store and process names of 100 candidates char name[100][25]; Definition of an array: An array can defined as a collection of related data elements of the same data type stored in computer s memory to store and process large volume of data Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 1 Or An array can defined as a fixed size sequenced collection of data elements of similar data type stored in consecutive computer s memory Examples: 1. To define an array with the name a of type int i.e. to store and process maximum number of 100 integer values instead of defining 100 separate variables. int a[100]; Index/subscript a The machine allocates 100 memories continuously with the common name a. The individual memories can be identified by common array name with the index values i.e. starts with 0 and end with ARRAY SIZE-1. The integer values can be stored in array elements with common array name a with different index values as follow. a[0]= 15, a[1]=25,a[2]=35, a[3]=45,a[5]=55,.,a[99]=125; Index a

2 bytes). The total memory allocated by this array is 200 bytes (100 memories x 2 bytes= To define an array with the name avg of float type to store and process percentage of marks of 100 students instead of defining 100 separate variables. float avg[100]; Index avg The floating point values can be stored in array elements with common array name avg with different index values as follow. bytes). avg[0]= 90.59, avg[1]=80.89,avg[2]=70.88, avg[3]=93.67,avg[5]=87.55,.,avg[99]=69.25; Index a The total memory allocated by this array is 400 bytes (100 memories x 4 bytes= To store and process name of a student in character array or string variable. char name[25]; strcpy(name, John ); /* character array or string variable*/ Index name J o h n \0 The total memory allocated by this array is 25 bytes (25 memories x 1 byte=25 bytes). 4. To store two strings (words) char str1[50], str2[50]; Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 2

3 Types of arrays: The arrays can be classified according to the volume of data to be stored in computer s memory. 1. One dimensional array e.g. int a[100]; 2. Two dimensional array e.g. int a[100][100]; 3. Multi dimensional array e.g. int a[100][100][100]; 1. One dimensional array: One dimensional array can defined as a collection of related data elements of the same data type stored in consecutive computer s memory to store and process linear list of data elements The individual arrays elements can be processed by using common array name with different index that starts with 0 and ends with the ARRAY SIZE 1. Declaration of one-dimensional array: The one dimensional array must be declared in the declaration part of the main() before the array elements are used in the executable part of main() by using following syntax. Syntax: where, data_type array_name[array_size]; data_type the data type of data to be stored and processed in computer s memory like int, float, char, double, long int, etc. array_name valid identifier ARRAY_SIZE the integer constant indicating maximum number of data elements can be stored. Examples: int a[5]; float price[5]; Index a Index price Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 3

4 double result[5]; Index result char name[5]; Index name char city[5]; Index city Note: Computer allocates 5 memory locations with garbage (unknown) values Initialization of one dimensional array: The process of assigning the values to the defined array elements is called initialization of array. The arrays can be initialized with the values in the following two ways. 1. Compile Time Array Initialization 2. Run Time Array Initialization 1. Compile Time Array Initialization: If the values are well known by programmers well in advance, then the programmer makes the use of compile time initialization. The process of assigning the values during the declaration of arrays in the declaration part of main() is called compile time initialization. The syntax is as follow. Syntax: data_type array_name[array_size]=value1,value 2,.,Value n; where, data_type the data type of data to be stored and processed in computer s memory like int, float, char, double, long int, etc. array_name valid identifier ARRAY_SIZE the integer constant indicating maximum number of data elements can be stored. Examples: 1. int a[5]=10,20,30,40,50; Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 4

5 or B.L.D.E.A s Vachana Pitamaha Dr. P. G. Halakatti College of Engineering & Technology, Vijyapur-03. a[0]=10, a[1]=20, a[2]=30, a[3]=40, a[4]=50; Index A float price[5]=55.50,99.50,100.50,75.60,50.60; Index price char name[5]= John ; or char name[5]= J, o, h, n ; or char name[ ]= John Index Name J O H n \0 char name[5]= J ; Index Name J \0 \0 \0 \0 4. int a[5]=0,0,0,0,0; or int a[5]=0; Index a int a[5]=10, 20; Index a int a[3]=1,2,3,4,5; /* invalid i.e. to many initialization*/ 2. Run Time Array Initialization: If the values are not known by programmers in advance then the programmer makes the use of run time initialization. It helps the programmers to read unknown values from the end users of a program by the keyboard by using input functions like scanf() getch() and gets(),etc. Examples: Partial Initialization of Array: If the number of values to be initialized is less than the size of the array then it is called as partial initialization. The remaining locations will be initialized to zero automatically Ex: Consider the array shown below Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 5

6 int p[5]=10,20; Even though we have declared 5 as a array-size but we have initialized only 2 elements in the array. So other locations are initialized to 0 automatically by compiler as shown below. Index p[0] p[1] p[2] p[3] p[5] Here, elements of index 2,3 and 4 are initialized to 0 automatically by compiler Array Initialization without Size: Consider the example shown below int b[ ]=10,15,20,25,30; Size is not specified In this initialization even though we have not specified the size of array b, but array elements are initialized with 5 different values. The array size will be set to total number of values specified in the array. The compiler will calculate the size of array based on the number of initial values. Since number of elements is 5 so totally 5*2bytes = 10 bytes are reserved where 2 is size of integer. Accessing the element of array: Consider an array consist of 7 integer element int p[7]=15,9,8,19,29,36,85; We can access the elements of array using index or subscript of element. Index gives the position of the elements in the array. Index p[0] p[1] p[2] p[3] p[4] p[5] p[6] p[0]=15, p[1]=9, p[2]=8, p[3]=19, p[4]=29, p[5]=36, p[6]=85 Index of first element of array is 0. Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 6

7 1. To read and display 5 integer values from a keyboard by using an array. #include<conio.h> int a[5],i; printf("\n Enter any 5 integer values"); for(i=0;i<5;i++) scanf("%d",&a[i]); for(i=0;i<5;i++) printf("\n%d",a[i]); 2. To read and display n integer values from a keyboard by using an array. #include<conio.h> int a[50],i,n; printf("\n Enter n: "); scanf("%d",&n); printf("\n Enter %d integer values: ",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\n Array Elements "); for(i=0;i<n;i++) printf("\n%d",a[i]); a[0] a[1] a[2] a[3] a[4] Enter any 5 integer values Enter n: 7 Enter 7 integer values Array Elements Assignment Questions: Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 7

8 1. What is one dimensional array? Explain its declaration and initialization with syntax and examples. (2+2+2=6 marks) (Model QP) 2. What is two dimensional array? Explain its declaration and initialization with syntax and examples. (2+2+2=6 marks) 3. Write a c program to implement bubble sort technique. 4. Write a c program to find largest of n numbers. 5. Write a c program to read N integers into an array A and to i. Find the sum of odd numbers ii. Find the sum of even numbers iii. Find the average of all numbers Output the results computed with appropriate headings. (06 marks June/July 2015) 6. Write a program to multiple of two arrays of given order a[m x n] and b[ p x q] (09 marks Jan. 2015) Anyone who stops learning is old, whether at twenty or eighty~ Henry Ford When Henry Ford decided to produce his famous V-8 motor, he chose to build an engine with the entire eight cylinders cast in one block, and instructed his engineers to produce a design for the engine. The design was placed on paper, but the engineers agreed, to a man, that it was simply impossible to cast an eight-cylinder engine-block in one piece. Ford replied,''produce it anyway. ~ Henry Ford What we usually consider as impossible are simply engineering problems ~ Michio Kaku *** Programming Examples on one dimensional arrays /* To find largest and lowest of n numbers*/ #include<conio.h> int a[5],i,n,max,min; printf("\n Enter n: "); scanf("%d",&n); printf("\n Enter %d values: ",n); for(i=0;i<n;i++) scanf("%d",&a[i]); max=min=a[0]; for(i=1;i<n;i++) if (a[i]>max) Enter n: 5 Enter 5 values: Largest number is 67 Lowest number is 2 Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 8

9 max=a[i]; if (a[i]<min) min=a[i]; printf("\n Largest number is %d",max); printf("\n Lowest number is %d",min); Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 9

10 /* To find sum of odd,even,all and avg of N numbers by using array */ #include<conio.h> int a[5],i,n,sum=0,esum=0,osum=0; float avg; printf("\n Enter n: "); scanf("%d",&n); printf("\n Enter %d values: ",n); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) if (a[i]%2==0) esum=sum+i; else osum=osum+i; sum=sum+a[i]; avg=(float)sum/n; printf("\n Sum of even numbers=%d",esum); printf("\n Sum of odd numbers=%d",osum); printf("\n Sum of all numbers=%d",sum); printf("\n Average of all numbers=%f",avg); Enter n: 5 Enter 5 values: Sum of even numbers = 9 Sum of even numbers = 6 Sum of all numbers = 15 Average of all numbers = Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 10

11 Sorting Techniques in C The process of arranging the elements in the ascending order or descending order is the sorting. There are 2 sorting technique 1) Bubble Sort 2) Selection Sort Bubble Sort: Bubble sort technique starts by comparing the first two elements of an array and swap the elements if necessary (Swapping is nothing but interchanging the elements), i.e., if you want to sort the elements of array in ascending order and if the first element is greater than second then, you need to swap the elements. If the first element is smaller than second, you must not swap the element. Then, again second and third elements are compared and swapped if it is necessary and this process go on until last and second last element is compared and swapped. This completes the first step of bubble sort. In the first level of sorting highest elements of the list will settles at bottom position. In the second level of sorting second highest element will settles at second bottom position and so on. Each and every level largest element settles at the bottom and smallest elements bubbles up. The following figure illustrates the working of bubble sort Bubble sort technique Here, there are 5 elements to the sorted. So, there are 4 steps(levels). Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 11

12 /* Program to sort n numbers using Bubble sort technique */ #include<conio.h> int n, a[25], i, j, temp; printf("enter n:"); scanf("%d",&n); printf("\n Enter any %d values",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nbefore sorting\n"); for(i=0;i<n;i++) printf("\t%d",a[i]); for(i=0;i<n;i++) for(j=0; j<n-i-1 ;j++) if (a[j]> a[j+1]) temp=a[j]; a[j]=a[j+1]; a[j+1]= temp; printf("\nafter sorting\n"); for(i=0;i<n;i++) printf("\t%d",a[i]); Enter n: 5 Enter any 5 values: Before Sorting After Sorting Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 12

13 Selection Sort: Selection sort algorithm starts by comparing first two elements of an array and swap the elements if necessary, i.e., if you want to sort the elements of array in ascending order and if the first element is greater than second then, you need to swap the elements but, if the first element is smaller than second, leave the elements as it is. Then, again first element and third element are compared and swapped if necessary. This process goes on until first and last element of an array is compared. This completes the first step of selection sort. If there are n elements to be sorted then, the process mentioned above should be repeated n -1times to get required result. Following figure shows the working of selection sort technique Fig: Selection sort technique In above example the first number 20 is compared with second number 12. Since first number is larger than second number then swap the numbers. Then compare the second number with third number if swapping is necessary then swap the number otherwise compare third element with fourth element and so on up to n. C Program for illustrating Selection sort technique Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 13

14 /* C Program to sort the numbers in ascending order using selection sort technique*/ #include<conio.h> void main( ) int n,i,j,temp,a[20]; printf("enter total elements: "); scanf("%d",&n); printf("enter %d elements: ",n); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) for(j=i+1;j<n;j++) if(a[i]>a[j]) temp=a[i]; a[i]=a[j]; a[j]=temp; printf("after sorting is: "); for(i=0;i<n;i++) printf(" %d",a[i]); getch( ); /*Swapping technique*/ Output Enter total elements:5 Enter 5 elements: After Sorting is: Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 14

15 /* Program to find sum and average of N numbers using array */ #include<conio.h> int a[25],n,i,sum=0; float avg; printf("enter n:"); scanf("%d",&n); printf("\nenter any %d values",n); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) sum=sum+a[i]; avg=(float)sum/n; printf("\n Sum of all numbers=%d",sum); printf("\n Average of all numbers=%f",avg); Enter n: 5 Enter any 5 values: Sum of all numbers = 15 Average of all numbers = Bell Teacher: A new student approached the Zen master and asked how he should prepare himself for his training. "Think of me a bell," the master explained. "Give me a soft tap, and you will get a tiny ping. Strike hard, and you'll receive a loud, resounding peal." People's reactions to this story: "You get out of something what you put into it." "The more you try, the more a good teacher will help." "Be careful what you ask for. The universe may just provide you with what you seek." "Sounds like the master is saying pay me a lot, and I will help you a lot; pay me little, and that's what I'll give you in return." "I think the teacher was warning the student that if he is struck he will strike back with equal force." Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 15

16 Searching Techniques in C Programming Language More often we will be working on large amount of data. It may be necessary to determine whether a particular item is present in that large amount of data. The Process of finding a particular element in the large amount of data is called as Searching. There are 2 types of searching techniques 1) Linear Search 2) Binary Search 1) Linear Search( Sequential Search): A linear search also called as Sequential Search is a simple searching technique. In this technique we search for a given specific element called as key element in the large list of data in sequential order( One after another) from first element to last element. If the key element is present in the list of data then search is Successful. Otherwise, search is Unsuccessful. Ex: int a[5]=10,20,30,40,50; Index a[0] a[1] a[2] a[3] a[4] Consider above example where array a contains 5 integer elements. If user wants to search a key element 30 from given list of elements, then search begins from first element of array i.e. 10(Index is 0). Then it checks this element with given key element. If both element are same then it is called as SUCESSFUL SEARCH and it returns the position of that particular element in the array. If the element is not present in the array then it is called as UNSUCESSFUL SEARCH and it returns -1. So here in above example the search is successful and the key element is present at Position 3(Position=index+1). Following is the C Program for Linear Search Technique Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 16

17 /* Program to implement linear search technique by using an array*/ #include<conio.h> int a[25],n,i,key ; printf("enter n:"); scanf("%d",&n); printf("\n Enter any %d values",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\n Enter item to be search:"); scanf("%d",&key); for(i=0;i<n;i++) if (key==a[i]) printf("\n Item found at %d position",i+1); exit(); printf("\n Item not found!"); Enter n: 5 Enter any 5 values: Enter item to be search: 88 Item found at 4 position Enter n: 5 Enter any 5 values: Enter item to be search: 100 Item not found! Advantages: 1) Very simple approach. 2) Works well for small arrays 3) Used to search when the elements are not sorted. Disadvantages: 1) Less efficient if the array is large 2) If the elements are the already sorted, linear search is not efficient. Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 17

18 Binary Search Technique: A binary search is simple and very efficient searching technique which can be applied if the items to be compared are either in ascending order or descending order. The general idea behind Binary Search technique is finding middle element from the sorted list of element. If given Key element is less than middle element search towards left side of middle element, otherwise search towards right side. This process is repeated until key element is found or key element is not found. If key element is found then return position of that element. If key element is not found then retuen -1. Consider the array shown below int a[10]=10,20,30,40,50,60,70,80,90,100; index: a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] low high low --> Represents the first element of array where index is 0. Hence low=0. High --> Represents the last element of array where last index is 9 i.e. n-1. Hence high=n-1. low=0 high=n-1( n is total number of elements) So position of middle element is obtained by mid=(low+high)/2 Here in above example mid=(0+9)/2=4. Because the mid value is integer. This suggest that the element which is at index 4 is the middle element. So middle element is 50. Case 1) Now key element is compared with middle element. If both elements are same then return mid value. if(key==a[mid]) return mid; Case 2) If key element to be search is less than middle element then search towards left of the middle element. i.e. Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 18

19 if(key<a[mid]) high=mid-1; high=mid-1 searches towards left side of middle element Case 3) If Key element to be search is greater than middle element then search towards right side of middle element. i.e. if(key>a[mid]) low=mid+1; low=mid+1 searches towards right side of middle element. Finally if key element to be searched is not present in the list, then it returns -1. /* Program to implement binary search technique by using an array*/ #include<conio.h> int a[25], n, i, key, low, high, mid; float avg; printf("enter n:"); scanf("%d",&n); printf("\n Enter any %d values in ascending order: ",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\n Enter item to be search:"); scanf("%d",&key); low=0; high=n-1; while(low<=high) mid=(low+high)/2; if (key==a[mid]) printf("\n Item found at %d position",mid+1); Enter n: 5 Enter any 5 values in ascending order: Enter item to be search: 3 Item found at 3 position Enter n: 5 Enter any 5 values in ascending order: Enter item to be search: 8 Item not found! Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 19

20 exit(0); if (key>a[mid]) low=mid+1; if (key<a[mid]) high=mid-1; printf("\n Item not found!"); Advantage: Very efficient searching technique. Disadvantage: Array elements should be in sorted. Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 20

21 Two dimensional arrays: A two dimensional array helps the programmers to store and process table of data elements of similar type in computer s primary memory RAM. It is similar to a matrix containing numbers of rows and columns. The rows and columns of 2D array are indexed by starting from 0(zero). i.e row index starts with 0 and end with ROW_SIZE-1 and column index starts with 0 and end with COLUMN_SIZE-1. The 2D arrays elements can be processed by using common array name with two index representing row and column. Examples: 1. To store and process 6 subject marks of 5 students. int a[5][6]; Col-0 Col-1 Col-2 Col-3 Col-4 Col-5 Row Row-1 Row-2 Row-3 Row In each row, programmer can store 6 subject marks of each student. a[0][0]=65, a[0][1]=75, a[0][2]=90, a[0][3]=80, a[0][4]=66, a[0][5]=68;. a[4][0]=50, a[4][1]=60, a[4][2]=70, a[4][3]=80, a[4][4]=90, a[4][5]=99; 2. To store and process table of integer values containing 2 rows & 3 columns. int a[2][3]; To store and process table of floating point numbers of the size 3 x float b[3][2]; 0 1 Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 21

22 Declaration of two-dimensional array: The two dimensional array must be declared in the declaration part of the main() before the array elements are used in the executable part of main() by giving common name with two indexes representing no. of rows and columns. The syntax is as follow. Syntax: where, data_type array_name[row_size] [COLUMN_SIZE]; data_type the data type of data to be stored and processed in computer s memory like int, float, char, double, long int, etc. array_name valid identifier ROW_SIZE & COLUMN_SIZE the maximum number of row and column elements can be stored ( The values should be integer). Examples: 1. To store the integer values for 5 x 5 matrix. int a[5][5]; Col-0 Col-1 Col-2 Col-3 Col-4 Row-0 Row-1 Row-2 Row-3 Row-4 Total memory occupied by this array is ( 5 x 5 x 2 bytes=50 bytes) 2. To store and process table of integer values containing 2 rows & 3 columns. 0 1 int a[2][3]; To store the names of 100 students char name[100][25]; Total memory occupied by this array is ( 100 x 25 x 1 byte=25000 bytes) Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 22

23 Initialization of two dimensional array: The process of assigning the values to the defined array elements of 2D array is called initialization of 2D array. The array elements can be initialized with different in the following two ways. 1. Compile Time Array Initialization 2. Run Time Array Initialization 1. Compile Time Array Initialization: If the values are well known by programmers in advance, then the programmer makes the use of compile time initialization. i.e the process of assigning the values during the declaration of arrays in the declaration part of main() is called compile time initialization. The syntax is as follow. Syntax: Examples: data_type array_name[row_size] [COLUMN_SIZE]= list of values, list of values, list of values ; int a[2][3]= or int a[2][3]=10,20,30,40,50,60; 10,20,30, 40,50,60 ; int a[3][2]=10,20,30,40,50,60; int a[ ][2]=0,0,0,0; Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 23

24 1. Run Time Array Initialization: If the values are not known by programmers in advance then the programmer makes the use of run time initialization. i.e. programmer reads values from users through the keyboard by using input functions like scanf() getch() and gets(),etc. Examples: 1. To read and display integer values for m x n matrix by using 2D array. /* To read and display values for (m x n) matrix*/ #include<conio.h> int a[25][25],m,n,i,j; printf("enter the order of matrix A:"); scanf("%d%d",&m,&n); printf("\n Enter %d values in row order: ",m*n); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); Enter the order of matrix A: 2 3 Enter 6 values in row order: printf("\n Array Elements \n"); Array Elements for(i=0;i<m;i++) for(j=0;j<n;j++) printf("\t%d",a[i][j]); printf("\n"); Assignments: 1. Write a c program to find addition of two matrices A and B of the same size. 2. Write a c program to find multiplication two matrices A(m,n) and B(p,q). Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 24

25 /* Addition of two matrices*/ #include<conio.h> int a[25][25],b[25][25],c[25][25],m,n,i,j; printf("enter the order of both the matrix A and B:"); scanf("%d%d",&m,&n); printf("\nenter %d values of A matrix: ",m*n); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("\nenter %d values of B matrix: ",m*n); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&b[i][j]); /* code to find addition of matrix*/ for(i=0;i<m;i++) for(j=0;j<n;j++) c[i][j]=a[i][j]+b[i][j]; printf("\n Resultant Matrix \n"); for(i=0;i<m;i++) for(j=0;j<n;j++) printf("\t%d",c[i][j]); printf("\n"); Enter the order of both matrix A & B : 2 3 Enter 6 values of A matrix : Enter 6 values of B matrix : Resultant Matrix Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 25

26 /* Substraction of two matrices*/ #include<conio.h> int a[25][25],b[25][25],c[25][25],m,n,i,j; printf("enter the order of both the matrix A and B:"); scanf("%d%d",&m,&n); printf("\nenter %d values of A matrix: ",m*n); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("\nenter %d values of B matrix: ",m*n); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&b[i][j]); /* code to find substraction of matrix*/ for(i=0;i<m;i++) for(j=0;j<n;j++) c[i][j]=a[i][j]-b[i][j]; printf("\n Resultant Matrix \n"); for(i=0;i<m;i++) for(j=0;j<n;j++) printf("\t%d",c[i][j]); printf("\n"); Enter the order of both matrix A & B : 2 3 Enter 6 values of A matrix : Enter 6 values of B matrix : Resultant Matrix Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 26

27 /* Multiplication of two matrices*/ #include<conio.h> int a[25][25],b[25][25],c[25][25],m,n,p,q,i,j,k; printf("enter the order of matrix A:"); scanf("%d%d",&m,&n); printf("enter the order of matrix B:"); scanf("%d%d",&p,&q); if (n==p) printf("\n Multiplication of A and B is possible\n"); printf("\nenter values of A matrix: "); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("\nenter values of B matrix: "); for(i=0;i<p;i++) for(j=0;j<q;j++) scanf("%d",&b[i][j]); /* code to find multiplication of matrix*/ for(i=0;i<m;i++) for(j=0;j<n;j++) c[i][j]=0; for(k=0;k<n;k++) c[i][j]+=a[i][k]*b[k][j]; printf("\n Resultant Matrix \n"); for(i=0;i<m;i++) for(j=0;j<q;j++) printf("\t%d",c[i][j]); printf("\n"); else printf("\n Multiplication is not possible!"); Enter the order matrix A & B : 2 3 Enter the order matrix A & B : 3 2 Multiplication is possible Enter values of A matrix : Enter values of B matrix : Resultant Matrix Enter the order matrix A & B : 2 3 Enter the order matrix A & B : 2 3 Multiplication is not possible Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 27

28 /* C Program to print transpose of given matrix*/ #include <stdio.h> #include<conio.h> int a[10][10]; int i, j, m, n; printf("enter the order of the matrix \n"); scanf("%d %d", &m, &n); printf("enter the coefiicients of the matrix\n"); for (i = 0; i < m; ++i) for (j = 0; j < n; ++j) scanf("%d", &a[i][j]); Output: Enter the order of the matrix printf("the given matrix is \n"); 3 3 for (i = 0; i < m; ++i) Enter the coefiicients of the for (j = 0; j < n; ++j) matrix printf(" %d", a[i][j]); printf("\n"); The given matrix is printf("transpose of matrix is \n"); for (j = 0; j < n; ++j) for (i = 0; i < m; ++i) Transpose of matrix is printf(" %d", a[i][j]); printf("\n"); Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 28

29 /* C Program to print principal diagonal elements of the matrix*/ #include<conio.h> int a[20][20],i,j,m,n; printf("enter the rows and columns of the matrixs\n"); scanf("%d%d",&m,&n); printf("enter the first matrics\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); //reads the matrics printf("the diagonal element of the matrics are\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) if(i==j) printf("%d,",a[i][j]); //prints the diagonal element of the matrics Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 29

30 Introduction to Strings The use of strings plays very important role, while developing the user friendly software. Hence, it is necessary for the programmer to store and process strings within computers memory. To read and display the strings, the programmer makes the use of I/O functions like scanf(), printf(), gets() and puts(). C supports different string handling functions like strcmp(), strcat(), strcpy() and strupr(),strlwr(), etc. to perform different operations on strings to get desired results. A string constant or literal can be defined as a sequence of characters enclosed in double quotes that will be treated as single data element Technically, a string constant is an array of characters. By default, each string ends with the NULL character (\0) that helps the programmer to identify the end of string. Examples: 1) Brain W. Kernighan and Dennis M. Ritchie 2) Computer Programming in C 3) VTU V T U \0 4) A 5) 2015 and 6) etc. Declaration and Initialization of string variables: The character arrays are also called as string variables. The string variables must be defined and initialized in same way of array declaration and initialization. Syntax: char string_variable[size]; Example: 1) To store name of a student. char name[25]; strcpy(name, Thomas ); Name T h o m a s \0 \0 Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 30

31 2) To store names of 3 students. char name[5][25]; strcpy(name[0], Thomas ); strcpy(name[1], Alice ); strcpy(name[2], Bob ); T h o m a s \0 \0 1 A l i c e \0 \0 2 B o b \0 \0 Reading and Writing Strings: The strings can be read and write by using following I/O functions. 1. scanf() and printf() 2. gets() and puts() The scanf( ) reads a string until white space found from a keyboard, whereas gets( ) reads a string until user press ENTER KEY. The printf( ) helps to write (display) strings on the monitor screen in different formats with appropriate message, whereas puts () helps to display only the string on monitor screen without format. /* To read and siplay strings by using scanf() and printf() */ char name[5][25]; printf("enter your name: "); scanf("%s",name); printf("hello! %s",name); /* To read and siplay strings by using gets() and puts() */ char name[25]; printf("enter your name"); gets(name); printf("hello! "); puts(name); Enter your name: Alice Bob Hello! Alice Bob Enter your name: Alice Bob Hello! Alice Bob Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 31

32 /* Program to find length of a string without using string built-in function*/ #include<conio.h> char str[25]; int i,count=0; printf("enter a string"); gets(str); for(i=0;str[i]!='\0';i++) count++; printf("\n Length of a string is %d",count); Enter a string: Alice Length of a string is 5 /* Program to copy one string1 into string2 without using string built-in function*/ #include<conio.h> char str1[25],str2[25]; int i; printf("enter string1 to be copy: "); gets(str1); for(i=0;str1[i]!='\0';i++) str2[i]=str1[i]; str2[i]='\0'; printf("\nstring Copied\n"); printf("\nstring1=%s String2=%s",str1,str2); Enter string1 to be copy: Alice String Copied String1= Alice String2=Alice /* To reverse a string without using string builtin function*/ #include<string.h> char str[25]; int i,len,temp; printf("enter string to be reverse: "); gets(str); len=strlen(str)-1; for(i=0;i<strlen(str)/2;i++) temp=str[i]; str[i]=str[len]; str[len--]=temp; printf("\n The reversed string is %s",str); Enter string to be reverse: tech The reversed string is hcet Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 32

33 String Manipulating Functions/ String Built-in Function: C supports different string handling functions to perform different operations on strings. All the string handling functions are stored in string.h file. Some of the most common used string functions are as follow. Sl.No. Function Description 1 strlen() Returns number of characters in the given string 2 strcpy() Copies the given source string to another destination string variable 3 strncpy() Copies first n characters from source to destination 4 strcmp() Compares two strings for their similarity 5 strncmp() Compares first n characters from two strings for their similarity 6 strcat() Concatenates (joins) two strings into single string 7 strncat() Concatenates first n characters from source string to destination 8 strupr() Converts characters into uppercase 9 strlwr() Converts characters into lowercase 10 strrev() Reverses a given string 1. strlen(): It returns the number of characters in the given string. Syntax: strlen(string); Examples: Programming Example: int len; len=strlen( vtu ); len=3 /* To find length of a string */ #include<string.h> char str[50]; int len; printf("enter a string"); gets(str); len=strlen(str); int len; char str[]= vtu ; len=strlen(str); len=3 printf("\n Length of the string is %d",len); Enter a string : vtu Length of the string is 3 Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 33

34 2. strcpy(): It copies the contents of source string to another destination string variable. Syntax: strcpy(destination, source string); Examples: char str1[25]= vtu,str2[25]; strcpy(str2,str1); printf( copied string is %s,str2); Programming Example: Copied string is vtu /* To copy a string */ #include<string.h> char str1[50],str2[50]; Enter string1 to be copy : vtu Copied string is vtu printf("enter string1 to be copy: "); gets(str1); strcpy(str2,str1); printf("\n Copied string is %s",str2); 3. strncpy(): It copies the number of characters from source string to another destination string variable. Syntax: strcpy(destination, source string, n); where, n number of characters to be copy Examples: char str1[25]= Technology,str2[25]; strcpy(str2,str1,4); printf( copied string is %s,str2); Copied string is Tech 4. strcmp(): It compares given two strings for their similarity character by character. If, the given strings are same then it returns zero; otherwise, non-zero (ASCII difference of dissimilar characters). Syntax: strcmp(string1,string2); Examples: char str1[25]= VTU,str2[25]= VTU ; int temp; temp=strcmp(str1,str2); temp=0 /* because both are same */ Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 34

35 char str1[25]= Ant,str2[25]= Cat ; int temp; temp=strcmp(str1,str2); temp=-2 /* ASCII of A ASCII of C i.e =-2 */ 5. strncmp(): It compares given two strings for their similarity for the specified number of characters from the beginning. If, the given strings are same then it returns zero; otherwise, non-zero (ASCII difference of dissimilar characters). Syntax: strncmp(string1,string2,n); where, n number of characters to be compare. Examples: char str1[25]= THERE,str2[25]= THEIR ; int temp; temp=strncmp(str1,str2,3); temp=0 /* because first 3 characters are same */ 6. strcat(): It concatenates (joins) two strings into a single string. Syntax: strcat(string1,string2); Example: char str1[25]= Honey, str2[25]= well, str3[25]; strcpy(str3,strcat(str1,str2)); printf( %s,str3); Honeywell 7. strncat(): It concatenates (joins) only specified number of characters from source to destination string. Syntax: strncat(string1,string2,n); where, n number of characters to be joined. Example: char str1[25]= Honey, str2[25]= well, str3[25]; strcpy(str3,strcat(str1,str2,2)); printf( %s,str3); Honeywe 8. strrev(): It reverses a string. Syntax: strrev(string); Example: char str1[25]= computer ; strrev(str1); printf( %s,str1); retupmoc Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 35

36 /* check string for palindrome */ #include<string.h> char str1[25],str2[25]; printf("enter a string:"); gets(str1); strcpy(str2,str1); strrev(str1); if(strcmp(str1,str2)==0) printf("\n Palindrome"); else printf("\n Not palindrome"); Enter a string : madam Palindrome Enter a string : teacher Not Palindrome 9. strupr(): It converts the characters of a given string into uppercase. Syntax: strupr(string); Example: char str1[25]= computer ; strupr(str1); printf( %s,str1); COMPUTER 10. strlwr(): It converts the characters of a given string into lowercase. Syntax: strlwr(string); Example: char str1[25]= COMPUTER ; strlwr(str1); printf( %s,str1); computer *** Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 36

37 Functions or Subprograms Introduction to Functions: As we know, every C program permits the programmer to make use only one main ( ) to solve a problem. Hence, it is very difficult for the programmer to solve a large and complex problem by using single main() with only built-in functions. Hence, programmers write different sub programs to do specific task. These subprograms written by programmers are called subprograms or user defined functions or modules. The programming approach in which the given large and complex problem will be divided into many subprograms and are called by main() to do specific task is called modular programming approach. A function can be defined as a subprogram that contains a group of statements to do specific task Examples: int sum (int x, int y) return(x+y); float area (float x) return(3.142*x*x); A function to find sum of two numbers int sum (int x, int y) or int ans; ans=x+y; return (ans); A function to find area of circle or float area (float x) float ans; ans=3.142*x*x; return(ans); A function to find largest of two numbers int large (int x, int y) if (x>y) return x; or int large (int x, int y) int ans; if (x>y) else return y; ans=x; else ans=y; return (ans); Advantages of writing user defined functions 1. Reduction in the size of main( ). 2. Reusability of the written functions. 3. Program maintenance, testing and debugging is easier. 4. Functions can be shared among many C files. 5. Programmers can create their own functions library like header files. 6. Modular programming approach. Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 37

38 Elements of user defined functions: In order to design and develop user defined functions of sub programs the programmer needs to establish and use the following three elements of user defined functions. i. Function declaration or prototype ii. iii. Function call Function definition The programming example that includes all the three elements is as follows. /* to find sum of two numbers by using function*/ int sum(int x, int y); /* function prototype*/ int n1,n2,ans; printf("\n Enter any two numbers"); scanf("%d%d",&n1,&n2); ans=sum(n1,n2); printf("\n Addition is %d",ans); int sum(int x, int y) return (x+y); /* function call*/ /* function definition*/ Enter any two numbers: 7 3 Addition is 10 (i) Function declaration or prototype: The function must be declared in the global declaration part of the C program i.e. before the main( ). It provide three details of function to be write to C-compiler. The syntax is as follows. Syntax: return_type function_name (arguments list); where, return_type The data type of return value like int,cfloat, char, double etc. The default return type is int. function name valid identifier. arguments list Number of inputs or parameters or arguments. Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 38

39 Examples: int sum (int x, int y); or int sum(int, int) /* to find sum*/ float area (float x); or float area (float) /* to find area of circle*/ void STRCOPY(char str1[],char str2[]); /* to copy str1 into str2*/ long int fact(long int x); /* to find factorial */ int isprime (int x); /* to check of prime number*/ Note: If you write the function before the main() then no need to declare the function. ii. Function call: The user defined function is called by the main() function to do specific task by passing the number of actual parameters. Examples: i) ans1=square(n); ans2=cube(n); 2) r=isprime(i); 3) rightrot(x,n); /* function call*/ /* function call*/ /* function call*/ /* function call*/ iv. Function definition: This is the subprogram, which can be written before or after the main(). To write function, the programmer needs to provide three details of function to C compiler. They are, return type, function name and number of parameters to be received. Syntax: return_type function_name (arguments list) local definitions; statements; return (value); Examples: 1) To find addition of factorial of n number. Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 39

40 int sum (int x, int y) int ans; ans=x+y; return (ans); 2) To find factorial of n number. long int fact(int x) int i, prod=1; for(i=1;i<=x;i++) prod=prod*i; return (prod); 3) To check for prime number. int isprime(int x) int i; for(i=2;i<=x/2;i++) if (x%i==0) return 0; return(1); Programming Examples on user defined functions or subprograms /* to find square and cube of number by using functions*/ int square(int); int cube(int); /* function declaration*/ int n,ans1,ans2; printf("\n Enter a number"); scanf("%d",&n); ans1=square(n); /* function call*/ ans2=cube(n); /* function call*/ printf("\n Square is %d",ans1); printf("\n Cube is %d",ans2); Enter a number: 2 Square is 4 Cube is 8 int square(int x) return (x*x); int cube(int x) return (x*x*x); /* function definition*/ /* function definition*/ Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 40

41 /* to find factorial n by using function*/ long int fact(long int x); /* function declaration*/ long int n,ans; printf("\n Enter a number"); scanf("%ld",&n); ans=fact(n); /* function call*/ printf("\n Factorial is %ld",ans); Enter a number: 4 Factorial is 24 long int fact(long int x) long int prod=1; int i; for (i=1;i<=x;i++) prod=prod*i; return(prod); /* function definition*/ /* to check for prime number by using function*/ int isprime(int x); /* function declaration*/ int n,r; printf("\n Enter a number"); scanf("%d",&n); r=isprime(n); /* function call*/ if(r==1) printf("\n %d is prime number",n); else printf("\n %d is not prime number",n); int isprime(int x) /* function definition*/ int i; for (i=2;i<=x/2;i++) if(x%i==0) return 0; return(1); Enter a number: is prime number Enter a number:24 24 is not prime number Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 41

42 /* to copy string1 into string2 by using user defined function*/ void STRCOPY(char str1[],char str2[]); /* function declaration*/ char str1[50],str2[50]; printf("\n Enter string1 to be copy: "); gets(str1); STRCOPY(str1,str2); /* function call*/ void STRCOPY(char str1[],char str2[]) int i; for (i=0;str1[i]!='\0';i++) str2[i]=str1[i]; str2[i]='\0'; printf("\n String copied..."); printf("\n string1=%s string2=%s",str1,str2); /* function definition*/ Enter a string1 to be copy : Happy String copied String1= Happy String2=Happy Assignments 1. Explain string declaration and initialization with examples. 2. Write a c program to find length of a string without using string handling function. 3. Explain any FIVE string manipulating functions with syntax and examples. 4. What is user defined function? Give example. 5. List advantages of writing subprograms or user defined functions? 6. Explain THREE elements of user defined functions with programming example. 7. Write c program to find factorial of n using user defined function. 8. Write user defined function named isprime(n) that returns 1 if the number is prime; otherwise Write user defined function STRCOPY() to copy string1 into string Solve questions from model and annual question papers. Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 42

43 Parameters (Arguments or Inputs) : The inputs given to the functions to do specific task are called parameters or arguments. The parameters are passed by main() to user defined function to process and to get desired result as an output from it. These parameters are classified into following two types. i) Formal parameters ii) Actual parameters Formal Parameters: The parameters used in the function header of function definition are called formal parameters. These parameters receive input data for processing from the actual parameters from the calling function main(). i,e. formal parameters receive data from actual parameters. The changes made to formal parameters do not affect the contents of actual parameters. Examples: int square(int x); /* Here, x is formal or dummy parameter */ int n,ans; printf( Enter a number: ); ans=square(n); /* Here, n is actual parameter*/ printf( \n square is %s,ans); int square(int x) /* Here, x is formal parameter */ return (x*x); Actual Parameters: The parameters used during the function call in the main() function are called actual parameters. These are actual (original) input data received from users of program and passed to user define function to do specific task. i.e. the contents of actual parameters will be copied into formal parameters. Examples: /* Example for actual parameters */ void test(int x,int y); int x=7,y=3; printf("\n Before calling function: Actual parameters "); printf("\n x=%d y=%d", x,y); test(x,y); /* Here, x & y are actual parameters*/ printf("\n After calling function: Actual parameters "); printf("\n x=%d y=%d", x,y); Before calling function: Actual parameters x = 7 y = 3 Inside function: Formal parameters x = 8 y = 4 After calling function: Actual parameters x = 7 y = 3 void test(int x,int y) /* Here, x & y are formal parameters*/ x++; y++; printf("\n Inside function: Formal parameters"); printf("\n x=%d y=%d",x,y); Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 43

44 Categories of Functions based on number of arguments and return value: The functions can be categorized into following 4 types based on the number of parameters or inputs they receive to do specific task and return value. i. Functions with no arguments and no return value ii. Functions with arguments and no return value iii. Functions with arguments and return value iv. Functions with no arguments and return value i. Functions with no arguments and no return value: These functions do not receive any inputs from the calling function main() and do not return any value back to it. But, these functions do specific task. i.e. there is no data transmission between calling and called function. void line(void); line(); line(); printf("\nvtu,belagavi.\n"); line(); line(); void line(void) int i; for(i=1;i<=80;i++) printf("_"); void sum(void); sum(); void sum(void) int n1,n2,ans; printf("\nenter two numbers:"); scanf("%d%d",&n1,&n2); ans=n1+n2; printf("\naddition is %d",ans); VTU,Belagavi Enter any two numbers: 7 3 Addition is 10 Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 44

45 ii. Functions with arguments and no return value: These functions receive inputs from the calling function main() to do specific task; but, do not return any value back to it. The processed data will be displayed by it. Example: void sum(int x,int y); int n1,n2; printf("\nenter two numbers:"); scanf("%d%d",&n1,&n2); sum(n1,n2); void sum(int x,int y) int ans; ans=x+y; printf("\naddition is %d",ans); iii. Functions with arguments and return value: These functions receive inputs from the calling function main() to process and return a value back to it. i.e. data transmission takes place between calling and called function. Example: int sum(int x, int y); int n1,n2,ans; printf("\n Enter any two numbers"); scanf("%d%d",&n1,&n2); ans=sum(n1,n2); printf("\n Addition is %d",ans); int sum(int x, int y) return (x+y); Enter any two numbers: 7 3 Addition is 10 Enter any two numbers: 7 3 Addition is 10 Department of Computer Science and Engineering Class Notes By Miss Laxmi S Shabadi Page 45

Unit 1 - Arrays. 1 What is an array? Explain with Example. What are the advantages of using an array?

Unit 1 - Arrays. 1 What is an array? Explain with Example. What are the advantages of using an array? 1 What is an array? Explain with Example. What are the advantages of using an array? An array is a fixed-size sequenced collection of elements of the same data type. An array is derived data type. The

More information

UNIT III ARRAYS AND STRINGS

UNIT III ARRAYS AND STRINGS UNIT III ARRAYS AND STRINGS Arrays Initialization Declaration One dimensional and Two dimensional arrays. String- String operations String Arrays. Simple programs- sorting- searching matrix operations.

More information

Columns A[0] A[0][0] = 20 A[0][1] = 30

Columns A[0] A[0][0] = 20 A[0][1] = 30 UNIT Arrays and Strings Part A (mark questions). What is an array? (or) Define array. An array is a collection of same data type elements All elements are stored in continuous locations Array index always

More information

Chapter 8 Character Arrays and Strings

Chapter 8 Character Arrays and Strings Chapter 8 Character Arrays and Strings INTRODUCTION A string is a sequence of characters that is treated as a single data item. String constant: String constant example. \ String constant example.\ \ includes

More information

Write a C program using arrays and structure

Write a C program using arrays and structure 03 Arrays and Structutes 3.1 Arrays Declaration and initialization of one dimensional, two dimensional and character arrays, accessing array elements. (10M) 3.2 Declaration and initialization of string

More information

Classification s of Data Structures

Classification s of Data Structures Linear Data Structures using Sequential organization Classification s of Data Structures Types of Data Structures Arrays Declaration of arrays type arrayname [ arraysize ]; Ex-double balance[10]; Arrays

More information

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities Continuous Internal Evaluation Test 2 Date: 0-10- 2017 Marks: 0 Subject &

More information

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size]; Arrays An array is a collection of two or more adjacent memory cells, called array elements. Array is derived data type that is used to represent collection of data items. C Array is a collection of data

More information

'C' Programming Language

'C' Programming Language F.Y. Diploma : Sem. II [DE/EJ/ET/EN/EX] 'C' Programming Language Time: 3 Hrs.] Prelim Question Paper Solution [Marks : 70 Q.1 Attempt any FIVE of the following : [10] Q.1(a) Define pointer. Write syntax

More information

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays 1 What are Arrays? Arrays are our first example of structured data. Think of a book with pages numbered 1,2,...,400.

More information

Module 6: Array in C

Module 6: Array in C 1 Table of Content 1. Introduction 2. Basics of array 3. Types of Array 4. Declaring Arrays 5. Initializing an array 6. Processing an array 7. Summary Learning objectives 1. To understand the concept of

More information

ONE DIMENSIONAL ARRAYS

ONE DIMENSIONAL ARRAYS LECTURE 14 ONE DIMENSIONAL ARRAYS Array : An array is a fixed sized sequenced collection of related data items of same data type. In its simplest form an array can be used to represent a list of numbers

More information

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters, Strings Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters, decimal digits, special characters and escape

More information

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 10: Arrays Readings: Chapter 9 Introduction Group of same type of variables that have same

More information

Government Polytechnic Muzaffarpur.

Government Polytechnic Muzaffarpur. Government Polytechnic Muzaffarpur. Name of the Lab: COMPUTER PROGRAMMING LAB (MECH. ENGG. GROUP) Subject Code: 1625408 Experiment: 1 Aim: Programming exercise on executing a C program. If you are looking

More information

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows Unti 4: C Arrays Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type An array is used to store a collection of data, but it is often more useful

More information

F.E. Sem. II. Structured Programming Approach

F.E. Sem. II. Structured Programming Approach F.E. Sem. II Structured Programming Approach Time : 3 Hrs.] Mumbai University Examination Paper Solution - May 14 [Marks : 80 Q.1(a) What do you mean by algorithm? Which points you should consider [4]

More information

Arrays. Arrays are of 3 types One dimensional array Two dimensional array Multidimensional array

Arrays. Arrays are of 3 types One dimensional array Two dimensional array Multidimensional array Arrays Array is a collection of similar data types sharing same name or Array is a collection of related data items. Array is a derived data type. Char, float, int etc are fundamental data types used in

More information

Array. Arijit Mondal. Dept. of Computer Science & Engineering Indian Institute of Technology Patna IIT Patna 1

Array. Arijit Mondal. Dept. of Computer Science & Engineering Indian Institute of Technology Patna IIT Patna 1 IIT Patna 1 Array Arijit Mondal Dept. of Computer Science & Engineering Indian Institute of Technology Patna arijit@iitp.ac.in Array IIT Patna 2 Many applications require multiple data items that have

More information

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer. Functions A number of statements grouped into a single logical unit are called a function. The use of function makes programming easier since repeated statements can be grouped into functions. Splitting

More information

String can be represented as a single-dimensional character type array. Declaration of strings

String can be represented as a single-dimensional character type array. Declaration of strings String String is the collection of characters. An array of characters. String can be represented as a single-dimensional character type array. Declaration of strings char string-name[size]; char address[25];

More information

Introduction to string

Introduction to string 1 Introduction to string String is a sequence of characters enclosed in double quotes. Normally, it is used for storing data like name, address, city etc. ASCII code is internally used to represent string

More information

ARRAYS(II Unit Part II)

ARRAYS(II Unit Part II) ARRAYS(II Unit Part II) Array: An array is a collection of two or more adjacent cells of similar type. Each cell in an array is called as array element. Each array should be identified with a meaningful

More information

Q 1. Attempt any TEN of the following:

Q 1. Attempt any TEN of the following: Subject Code: 17212 Model Answer Page No: 1 / 26 Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in the model answer scheme. 2) The

More information

Computers Programming Course 11. Iulian Năstac

Computers Programming Course 11. Iulian Năstac Computers Programming Course 11 Iulian Năstac Recap from previous course Cap. Matrices (Arrays) Matrix representation is a method used by a computer language to store matrices of different dimension in

More information

PDS Class Test 2. Room Sections No of students

PDS Class Test 2. Room Sections No of students PDS Class Test 2 Date: October 27, 2016 Time: 7pm to 8pm Marks: 20 (Weightage 50%) Room Sections No of students V1 Section 8 (All) Section 9 (AE,AG,BT,CE, CH,CS,CY,EC,EE,EX) V2 Section 9 (Rest, if not

More information

Unit III Functions. C functions can be classified into two categories, namely, library functions and user defined functions.

Unit III Functions. C functions can be classified into two categories, namely, library functions and user defined functions. Unit III Functions Functions: Function Definition, Function prototype, types of User Defined Functions, Function calling mechanisms, Built-in string handling and character handling functions, recursion,

More information

Multiple Choice Questions ( 1 mark)

Multiple Choice Questions ( 1 mark) Multiple Choice Questions ( 1 mark) Unit-1 1. is a step by step approach to solve any problem.. a) Process b) Programming Language c) Algorithm d) Compiler 2. The process of walking through a program s

More information

Questions Bank. 14) State any four advantages of using flow-chart

Questions Bank. 14) State any four advantages of using flow-chart Questions Bank Sub:PIC(22228) Course Code:-EJ-2I ----------------------------------------------------------------------------------------------- Chapter:-1 (Overview of C Programming)(10 Marks) 1) State

More information

C does not support string data type. Strings in C are represented by arrays of characters.

C does not support string data type. Strings in C are represented by arrays of characters. Chapter 8 STRINGS LEARNING OBJECTIVES After going this chapter the reader will be able to Use different input/output functions for string Learn the usage of important string manipulation functions available

More information

UNIT 2 STRINGS. Marks - 7

UNIT 2 STRINGS. Marks - 7 1 UNIT 2 STRINGS Marks - 7 SYLLABUS 2.1 String representation : Reading and Writing Strings 2.2 String operations : Finding length of a string, Converting Characters of a string into upper case and lower

More information

Unit 3 Functions. 1 What is user defined function? Explain with example. Define the syntax of function in C.

Unit 3 Functions. 1 What is user defined function? Explain with example. Define the syntax of function in C. 1 What is user defined function? Explain with example. Define the syntax of function in C. A function is a block of code that performs a specific task. The functions which are created by programmer are

More information

Contents ARRAYS. Introduction:

Contents ARRAYS. Introduction: UNIT-III ARRAYS AND STRINGS Contents Single and Multidimensional Arrays: Array Declaration and Initialization of arrays Arrays as function arguments. Strings: Initialization and String handling functions.

More information

SUMMER 13 EXAMINATION Model Answer

SUMMER 13 EXAMINATION Model Answer Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in the model answer scheme. 2) The model answer and the answer written by candidate

More information

// CSE/CE/IT 124 JULY 2015, 2.a. algorithm to inverse the digits of a given integer. Step 1: Start Step 2: Read: NUMBER Step 3: r=0,rev=0

// CSE/CE/IT 124 JULY 2015, 2.a. algorithm to inverse the digits of a given integer. Step 1: Start Step 2: Read: NUMBER Step 3: r=0,rev=0 // CSE/CE/IT 124 JULY 2015, 2a algorithm to inverse the digits of a given integer Step 1: Start Step 2: Read: NUMBER Step 3: r=0,rev=0 Step 4: repeat until NUMBER > 0 r=number%10; rev=rev*10+r; NUMBER=NUMBER/10;

More information

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9 Fundamental of C Programming Unit I: Q1. What will be the value of the following expression? (2017) A + 9 Q2. Write down the C statement to calculate percentage where three subjects English, hindi, maths

More information

Computers Programming Course 10. Iulian Năstac

Computers Programming Course 10. Iulian Năstac Computers Programming Course 10 Iulian Năstac Recap from previous course 5. Values returned by a function A return statement causes execution to leave the current subroutine and resume at the point in

More information

PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE

PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE 1. Write a C program to perform addition, subtraction, multiplication and division of two numbers. # include # include int a, b,sum,

More information

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo 1. (a)what is an algorithm? Draw a flowchart to print N terms of Fibonacci

More information

Chapter4: Data Structures. Data: It is a collection of raw facts that has implicit meaning.

Chapter4: Data Structures. Data: It is a collection of raw facts that has implicit meaning. Chapter4: s Data: It is a collection of raw facts that has implicit meaning. Data may be single valued like ID, or multi valued like address. Information: It is the processed data having explicit meaning.

More information

Array Initialization

Array Initialization Array Initialization Array declarations can specify initializations for the elements of the array: int primes[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ; initializes primes[0] to 2, primes[1] to 3, primes[2]

More information

Computers Programming Course 12. Iulian Năstac

Computers Programming Course 12. Iulian Năstac Computers Programming Course 12 Iulian Năstac Recap from previous course Strings in C The character string is one of the most widely used applications that involves vectors. A string in C is an array of

More information

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in the model answer scheme. 2) The model answer and the answer written by candidate

More information

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) MODEL ANSWER

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) MODEL ANSWER Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in the model answer scheme. 2) The model answer and the answer written by candidate

More information

Functions: call by reference vs. call by value

Functions: call by reference vs. call by value Functions: call by reference vs. call by value void change(int x[ ], int s){ x[0] = 50; s = 7; void main(){ int n = 3; int a[ ] = {60,70,80; change(a, n); printf( %d %d, a[0], n); Prints 50 3 Strings A

More information

/* Area and circumference of a circle */ /*celsius to fahrenheit*/

/* Area and circumference of a circle */ /*celsius to fahrenheit*/ /* Area and circumference of a circle */ #include #include #define pi 3.14 int radius; float area, circum; printf("\nenter radius of the circle : "); scanf("%d",&radius); area = pi

More information

BITG 1113: Array (Part 2) LECTURE 9

BITG 1113: Array (Part 2) LECTURE 9 BITG 1113: Array (Part 2) LECTURE 9 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the fundamentals of C-strings (character arrays) 2. Use C-string functions 3. Use

More information

1) Write a C Program to check whether given year is Leap year or not. AIM: A C Program to check whether given year is Leap year or not.

1) Write a C Program to check whether given year is Leap year or not. AIM: A C Program to check whether given year is Leap year or not. 1) Write a C Program to check whether given year is Leap year or not. AIM: A C Program to check whether given year is Leap year or not. #include int year; printf("enter a year:"); scanf("%d",&year);

More information

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Arrays CS10001: Programming & Data Structures Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Array Many applications require multiple data items that have common

More information

1. Simple if statement. 2. if else statement. 3. Nested if else statement. 4. else if ladder 1. Simple if statement

1. Simple if statement. 2. if else statement. 3. Nested if else statement. 4. else if ladder 1. Simple if statement UNIT- II: Control Flow: Statements and Blocks, if, switch statements, Loops: while, do-while, for, break and continue, go to and Labels. Arrays and Strings: Introduction, One- dimensional arrays, Declaring

More information

4(a) ADDITION OF TWO NUMBERS. Program:

4(a) ADDITION OF TWO NUMBERS. Program: 4(a) ADDITION OF TWO NUMBERS int a,b,c: printf( enter the elements a and b ); scanf( %d%d,&a,&b); c=a+b; printf( sum of the elements is%d,c); Output: Enter the elements a and b: 4 5 Sum of the elements

More information

Test Paper 3 Programming Language Solution Question 1: Briefly describe the structure of a C program. A C program consists of (i) functions and (ii) statements There should be at least one function called

More information

Functions. Introduction :

Functions. Introduction : Functions Introduction : To develop a large program effectively, it is divided into smaller pieces or modules called as functions. A function is defined by one or more statements to perform a task. In

More information

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C Sample Test Paper-I Marks : 25 Time:1 Hrs. Q1. Attempt any THREE 09 Marks a) State four relational operators with meaning. b) State the use of break statement. c) What is constant? Give any two examples.

More information

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath UNIT - I Introduction to C Programming Introduction to C C was originally developed in the year 1970s by Dennis Ritchie at Bell Laboratories, Inc. C is a general-purpose programming language. It has been

More information

Arrays a kind of data structure that can store a fixedsize sequential collection of elements of the same type. An array is used to store a collection

Arrays a kind of data structure that can store a fixedsize sequential collection of elements of the same type. An array is used to store a collection Morteza Noferesti Arrays a kind of data structure that can store a fixedsize sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful

More information

BCSE1002: Computer Programming and Problem Solving LAB MANUAL

BCSE1002: Computer Programming and Problem Solving LAB MANUAL LABMANUAL BCSE1002: Computer Programming and Problem Solving LAB MANUAL L T P Course Type Semester Offered Academic Year 2018-2019 Slot Class Room Faculty Details: Name Website link Designation School

More information

WAP 10. WAP 11. WAP 12. WAP 13. WAP 14. WAP 15. WAP 16. WAP 1. : 17. WAP 18. WAP 19. WAP 20. WAP 21. WAP 22. WAP 23. WAP & 24. WAP

WAP 10. WAP 11. WAP 12. WAP 13. WAP 14. WAP 15. WAP 16. WAP 1. : 17. WAP 18. WAP 19. WAP 20. WAP 21. WAP 22. WAP 23. WAP & 24. WAP Contents 1. WAP to accept the value from the user and exchange the values.... 2 2. WAP to check whether the number is even or odd.... 2 3. WAP to Check Odd or Even Using Conditional Operator... 3 4. WAP

More information

F.Y. Diploma : Sem. II [CO/CD/CM/CW/IF] Programming in C

F.Y. Diploma : Sem. II [CO/CD/CM/CW/IF] Programming in C F.Y. Diploma : Sem. II [CO/CD/CM/CW/IF] Programming in C Time : 3 Hrs.] Prelim Question Paper Solution [Marks : 70 Q.1 Attempt any FIVE of the following : [10] Q.1 (a) List any four relational operators.

More information

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS Programming Year 2017-2018 Industrial Technology Engineering Paula de Toledo Contents 1. Structured data types vs simple data types 2. Arrays (vectors and matrices)

More information

FUNCTIONS OMPAL SINGH

FUNCTIONS OMPAL SINGH FUNCTIONS 1 INTRODUCTION C enables its programmers to break up a program into segments commonly known as functions, each of which can be written more or less independently of the others. Every function

More information

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) Subject Code: 17212 Model Answer Page No: 1/28 Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in themodel answer scheme. 2) The model

More information

MODULE 3: Arrays, Functions and Strings

MODULE 3: Arrays, Functions and Strings MODULE 3: Arrays, Functions and Strings Contents covered in this module I. Using an Array II. Functions in C III. Argument Passing IV. Functions and Program Structure, locations of functions V. Function

More information

Developed By: P.Venkateshwarlu, Alphores Womens Degree College, Karimnagar

Developed By: P.Venkateshwarlu, Alphores Womens Degree College, Karimnagar B.Sc (Computer Science) 1 sem Practical Solutions 1.Program to find biggest in 3 numbers using conditional operators. # include # include int a, b, c, big ; printf("enter three numbers

More information

Functions. Arash Rafiey. September 26, 2017

Functions. Arash Rafiey. September 26, 2017 September 26, 2017 are the basic building blocks of a C program. are the basic building blocks of a C program. A function can be defined as a set of instructions to perform a specific task. are the basic

More information

Sample Paper - II Subject Computer Science

Sample Paper - II Subject Computer Science Sample Paper - II Subject Computer Science Max Marks 70 Duration 3 hrs Note:- All questions are compulsory Q1) a) What is significance of My Computer? 2 b) Explain different types of operating systems.

More information

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type. Data Structures Introduction An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type. Representation of a large number of homogeneous

More information

MODULE 1. Introduction to Data Structures

MODULE 1. Introduction to Data Structures MODULE 1 Introduction to Data Structures Data Structure is a way of collecting and organizing data in such a way that we can perform operations on these data in an effective way. Data Structures is about

More information

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays Maltepe University Computer Engineering Department BİL 133 Algorithms and Programming Chapter 8: Arrays What is an Array? Scalar data types use a single memory cell to store a single value. For many problems

More information

To store the total marks of 100 students an array will be declared as follows,

To store the total marks of 100 students an array will be declared as follows, Chapter 4 ARRAYS LEARNING OBJECTIVES After going through this chapter the reader will be able to declare and use one-dimensional and two-dimensional arrays initialize arrays use subscripts to access individual

More information

Introduction to Arrays

Introduction to Arrays Introduction to Arrays One Dimensional Array. Two Dimensional Array. Inserting Elements in Array. Reading Elements from an Array. Searching in Array. Sorting of an Array. Merging of 2 Arrays. What is an

More information

LABORATORY MANUAL. (CSE-103F) FCPC Lab

LABORATORY MANUAL. (CSE-103F) FCPC Lab LABORATORY MANUAL (CSE-103F) FCPC Lab Department of Computer Science & Engineering BRCM College of Engineering & Technology Bahal, Haryana Aim: Main aim of this course is to understand and solve logical

More information

Lecture 04 FUNCTIONS AND ARRAYS

Lecture 04 FUNCTIONS AND ARRAYS Lecture 04 FUNCTIONS AND ARRAYS 1 Motivations Divide hug tasks to blocks: divide programs up into sets of cooperating functions. Define new functions with function calls and parameter passing. Use functions

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Lecture 6 - Array and String Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Array Generic declaration: typename variablename[size]; typename is

More information

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are

More information

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays) Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays) In this lecture, you will: Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of

More information

Chapter 8: Character & String. In this chapter, you ll learn about;

Chapter 8: Character & String. In this chapter, you ll learn about; Chapter 8: Character & String Principles of Programming In this chapter, you ll learn about; Fundamentals of Strings and Characters The difference between an integer digit and a character digit Character

More information

SRI VIDYA COLLEGE OF ENGINEERING & TECHNOLOGY, VIRUDHUNAGAR Department of CSE & IT Internal Test I

SRI VIDYA COLLEGE OF ENGINEERING & TECHNOLOGY, VIRUDHUNAGAR Department of CSE & IT Internal Test I SRI VIDYA COLLEGE OF ENGINEERING & TECHNOLOGY, VIRUDHUNAGAR Department of CSE & IT Internal Test I Year & Sem: I B.E (CSE) & II Date of Exam: 21/02/2015 Subject Code & Name: CS6202 & Programming & Data

More information

Objectives of This Chapter

Objectives of This Chapter Chapter 6 C Arrays Objectives of This Chapter Array data structures to represent the set of values. Defining and initializing arrays. Defining symbolic constant in a program. Using arrays to store, list,

More information

Strings and Library Functions

Strings and Library Functions Unit 4 String String is an array of character. Strings and Library Functions A string variable is a variable declared as array of character. The general format of declaring string is: char string_name

More information

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Arrays CS10001: Programming & Data Structures Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur 1 Array Many applications require multiple data items that have common

More information

Programming for Engineers Arrays

Programming for Engineers Arrays Programming for Engineers Arrays ICEN 200 Spring 2018 Prof. Dola Saha 1 Array Ø Arrays are data structures consisting of related data items of the same type. Ø A group of contiguous memory locations that

More information

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee C Language Part 2 (Minor modifications by the instructor) 1 Scope Rules A variable declared inside a function is a local variable Each local variable in a function comes into existence when the function

More information

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will: Chapter 8 Arrays and Strings Objectives In this chapter, you will: Learn about arrays Declare and manipulate data into arrays Learn about array index out of bounds Learn about the restrictions on array

More information

9/9/2017. Prof. Vinod Mahajan

9/9/2017. Prof. Vinod Mahajan In the last chapter you learnt how to define arrays of different sizes & sizes& dimensions, how to initialize arrays, how to pass arrays to a functions etc. What are strings:- The way a group of integer

More information

What is recursion. WAP to find sum of n natural numbers using recursion (5)

What is recursion. WAP to find sum of n natural numbers using recursion (5) DEC 2014 Q1 a What is recursion. WAP to find sum of n natural numbers using recursion (5) Recursion is a phenomenon in which a function calls itself. A function which calls itself is called recursive function.

More information

Lecture 10 Arrays (2) and Strings. UniMAP SEM II - 11/12 DKT121 1

Lecture 10 Arrays (2) and Strings. UniMAP SEM II - 11/12 DKT121 1 Lecture 10 Arrays (2) and Strings UniMAP SEM II - 11/12 DKT121 1 Outline 8.1 Passing Arrays to Function 8.2 Displaying Array in a Function 8.3 How Arrays are passed in a function call 8.4 Introduction

More information

20 Dynamic allocation of memory: malloc and calloc

20 Dynamic allocation of memory: malloc and calloc 20 Dynamic allocation of memory: malloc and calloc As noted in the last lecture, several new functions will be used in this section. strlen (string.h), the length of a string. fgets(buffer, max length,

More information

Chapter 10. Arrays and Strings

Chapter 10. Arrays and Strings Christian Jacob Chapter 10 Arrays and Strings 10.1 Arrays 10.2 One-Dimensional Arrays 10.2.1 Accessing Array Elements 10.2.2 Representation of Arrays in Memory 10.2.3 Example: Finding the Maximum 10.2.4

More information

Subject: Fundamental of Computer Programming 2068

Subject: Fundamental of Computer Programming 2068 Subject: Fundamental of Computer Programming 2068 1 Write an algorithm and flowchart to determine whether a given integer is odd or even and explain it. Algorithm Step 1: Start Step 2: Read a Step 3: Find

More information

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17 Grade Distribution Exam 1 Exam 2 Score # of Students Score # of Students 16 4 14 6 12 4 10 2 8 1 Total: 17 Exams 1 & 2 14 2 12 4 10 5 8 5 4 1 Total: 17 Score # of Students 28 2 26 5 24 1 22 4 20 3 18 2

More information

& Technology. G) Functions. void. Argument2, Example: (Argument1, Syllabus for 1. 1 What. has a unique. 2) Function name. passed to.

& Technology. G) Functions. void. Argument2, Example: (Argument1, Syllabus for 1. 1 What. has a unique. 2) Function name. passed to. Computer Programming and Utilization (CPU) 110003 G) Functions 1 What is user defined function? Explain with example. Define the syntax of function in C. A function is a block of code that performs a specific

More information

Saini Technologies ADVANCED C PROGRAMMING WITH SAINI TECHNOLOGIES. Sushil Kumar Saini Mo.

Saini Technologies ADVANCED C PROGRAMMING WITH SAINI TECHNOLOGIES. Sushil Kumar Saini   Mo. Saini Technologies ADVANCED C PROGRAMMING WITH SAINI TECHNOLOGIES Sushil Kumar Saini Email: Sushilsaini04@gmail.com Mo. 9896470047 Integer Array int a[]=12,34,54,45,34,34; printf("%d",a[0]); printf(" %d",a[1]);

More information

int Return the number of characters in string s.

int Return the number of characters in string s. 1a.String handling functions: Function strcmp(const char *s1, const char *s2) strcpy(char *s1, const char *s2) strlen(const char *) strcat(char *s1, Data type returned int Task Compare two strings lexicographically.

More information

PROGRAMMING IN C AND C++:

PROGRAMMING IN C AND C++: PROGRAMMING IN C AND C++: Week 1 1. Introductions 2. Using Dos commands, make a directory: C:\users\YearOfJoining\Sectionx\USERNAME\CS101 3. Getting started with Visual C++. 4. Write a program to print

More information

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos. UNIT 2 ARRAYS Arrays Structure Page Nos. 2.0 Introduction 23 2.1 Objectives 24 2.2 Arrays and Pointers 24 2.3 Sparse Matrices 25 2.4 Polynomials 28 2.5 Representation of Arrays 30 2.5.1 Row Major Representation

More information

How to declare an array in C?

How to declare an array in C? Introduction An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type. Representation of a large number of homogeneous values.

More information

Subject: Computer Science

Subject: Computer Science Subject: Computer Science Topic: Data Types, Variables & Operators 1 Write a program to print HELLO WORLD on screen. 2 Write a program to display output using a single cout statement. 3 Write a program

More information

C Programming Lecture V

C Programming Lecture V C Programming Lecture V Instructor Özgür ZEYDAN http://cevre.beun.edu.tr/ Modular Programming A function in C is a small sub-program that performs a particular task, and supports the concept of modular

More information

C Programming Multiple. Choice

C Programming Multiple. Choice C Programming Multiple Choice Questions 1.) Developer of C language is. a.) Dennis Richie c.) Bill Gates b.) Ken Thompson d.) Peter Norton 2.) C language developed in. a.) 1970 c.) 1976 b.) 1972 d.) 1980

More information