Page 1 :
GFGC,Malur, Department of Computer Science, Unit 4, _____________________________________________________________________, , Arrays: One Dimensional arrays - Declaration, Initialization and, representation; Two Dimensional arrays - Declaration, Initialization and, representation. Strings: Declaring & Initializing string variables; String, functions -strlen, strcmp, strcpy and strcat; Character handling functions, toupper, tolower, isalpha, isnumeric etc., , Memory, Memory, handling, - toascii,, , Array in C Language, , An array is a collection of similar data type value in a single variable. It is a derived, data type in C, which is constructed from fundamental data type of C language., , Advantage of array, •, , Code Optimization: Less code is required, one variable can store, numbers of value., , •, , Easy to traverse data: By using array easily retrieve the data of array., , •, , Easy to sort data: Easily short the data using swapping technique, , •, , Random Access: With the help of array index you can randomly, access any elements from array., , Dis-Advantage of array, Fixed Size: Whatever size, we define at the time of declaration of array, we can, not change their size, if you need more memory in that time you can not increase, memory size, and if you need less memory in that case also wastage of memory., , Declaring Array, To declare an array in C you need to declare datatype and size of an array., Syntax, , 1
Page 2 :
GFGC,Malur, Department of Computer Science, Unit 4, _____________________________________________________________________, , datatype arrayName[SIZE];, , Example, , int roll_no[10];, , Initializing Array, Initializing is a process to initialize the value in array variable. This is happen in, two ways, initialize array one by one or all elements are initializing once., Initialization of array one by one, , int arr[5];, arr[0]=10;, arr[1]=20;, arr[2]=30;, arr[3]=40;, arr[4]=50;, Initialization of array at once, , int arr[]={10,20,30,40,50};, , Accessing Array Elements, We can access array elements with the help of index value of element., Example, , int arr[]={10,20,30,40,50};, arr[3] // here 3 is index value and it return 40, Example of array, , 2
Page 3 :
GFGC,Malur, Department of Computer Science, Unit 4, _____________________________________________________________________, , #include<stdio.h>, #include<conio.h>, void main(), {, int i, marks[]={80, 62, 70, 90, 98}; //declaration and initialization of, array, , clrscr();, //traversal of array, for(i=0;i<5;i++), {, printf("\n%d",marks[i]);, }, getch();, }, Example, , 80, 62, 70, 90, 98, , EXAMPLE:, /* Program to sort numbers in ascending order & descending order*/, #include<stdio.h>, #include<conio.h>, void main(), {, int i,j,temp,a[7],n;, printf("Enter the no. of elements");, scanf("%d",&n);, printf("Enter the numbers :");, for(i=0;i<n;i++), {, scanf("%d",&a[i]);, }, 3
Page 4 :
GFGC,Malur, Department of Computer Science, Unit 4, _____________________________________________________________________, for (i=0;i<n;i++), for(j=i+1;j<n;j++), {, if(a[i]>a[j]), {, temp=a[j];, a[j]=a[i];, a[i]=temp;, }, }, printf("THE NUMBERS SORTED IN ASCENDING ORDER ARE :\n");, for(i=0;i<n;i++), {, printf("\hn%d",a[i]);, }, printf("\nDescending Order :");, for(i=n-1;i>=0;i--), {, printf("%d\t",a[i]);, }, getch();, }, output:, Enter the no. of elements: 5, Enter the numbers 78 3 45 23 90, Ascending Order: 3, , 23, , 45, , 78, , 90, , Descending Order:90 78, , 45, , 23, , 3, , Program to find the location of an item in an array, #include<stdio.h>, #include<conio.h>, void main(), {, int a[5],i;, intele,temp=0;, clrscr();, printf("enter the 5 array elements\n");, for (i=0; i<5; i++), scanf("%d",&a[i]);, printf("Enter the element to be search\n");, scanf("%d",&ele);, for (i=0; i<5; i++), {, if (a[i]==ele), {, temp=1;, printf("Element found %d , position=%d\n",ele,i+1);, 4
Page 5 :
GFGC,Malur, Department of Computer Science, Unit 4, _____________________________________________________________________, }, }, if (!temp), printf("Element not found\n");, getch();, }, Output:, enter the 5 array elements 89 56 45 32 7, Enter the element to be search: 56, Element found 56 , position=2, , 2-dimentional array, •, •, •, •, •, , In 2-dimentional elements are arranged in row and column format., When we are working with 2-dimentional array we require to refer 2subscript operator which indicates row and column sizes., The main memory of 2-dimentional array is rows and sub-memory is, columns., On 2-dimentional array when we are referring one-subscript operator, then if gives row address, 2-subscript operator will gives element., On 2-dimentional array arrayName always gives main memory that is, 1st row base address, arrayName will gives next row base address., Array declaration, initialization and, accessing, , Example, , Array declaration syntax:, data_type arr_name, [num_of_rows][num_of_column];, , Integer array, example:, int arr[2][2];, int arr[2][2] = {1,2, 3,, 4};, , Array initialization syntax:, data_type arr_name[2][2] =, {{0,0},{0,1},{1,0},{1,1}};, , arr [0] [0] = 1;, arr [0] ]1] = 2;, arr [1][0] = 3;, arr [1] [1] = 4;, , Array accessing syntax:, arr_name[index];, , //Transpose of a Matrix, #include<stdio.h>, void main(), {, int i,j,A[10][10],n;, clrscr();, printf("Enter the order of matrix:");, scanf("%d",&n);, for(i=0;i<n;i++), 5
Page 6 :
GFGC,Malur, Department of Computer Science, Unit 4, _____________________________________________________________________, for(j=0;j<n;j++), {, scanf("%d",&A[i][j]);, }, printf("\ntransposed matrix A: \n");, for(i=0;i<n;i++), {, for(j=0;j<n;j++), printf("%3d",B[j][i]);, printf("\n");, }, getch();, }, OUTPUT, Enter the order of matrix: 2, Enter Elements of Matrix:4 6 7 8, Transposed Matrix:, 4 7, 6 8, , PRODUCT OF TWO M * N MATRICES, #include<stdio.h>, #include<conio.h>, void main(), {, int a[20][20],b[20][20],c[20][20];, inti=0,j=0,k,sum=0,m,n;, clrscr();, printf("\n enter the m\n");, scanf("%d",&m);, printf("\n enter the n\n");, scanf("%d",&n);, printf("\n enter the matrix A elements ");, for(i=0;i<m;i++), {, for(j=0;j<n;j++), {, scanf("%d",&a[i][j]);, }, }, printf("\n enter the matrix B elements ");, 6
Page 7 :
GFGC,Malur, Department of Computer Science, Unit 4, _____________________________________________________________________, , for(i=0;i<m;i++), {, for(j=0;j<n;j++), {, scanf("%d",&b[i][j]);, }, }, for(i=0;i<m;i++), {, for(j=0;j<n;j++), {, sum=0;, for(k=0;k<n;k++), sum=sum+a[i][k]*b[k][j];, c[i][j]=sum;, }, }, printf("\n the product of matrices is as follow \n");, for(i=0;i<m;i++), {, for(j=0;j<n;j++), {, printf("\t%d",c[i][j]);, }, printf("\n");, }, getch();, }, Output:, enter the m 3, enter the n 3, enter the matrix A elements 4 4 4 4 4 4 4 4 4, the matrix A elements, 444, 444, 444, enter the matrix B elements 3 3 3 3 3 3 3 3 3, the matrix B elements, 333, 333, 7
Page 8 :
GFGC,Malur, Department of Computer Science, Unit 4, _____________________________________________________________________, , 333, the product of matrices is as follow, 36, 36, 36, 36, 36, 36, 36, 36, 36, , ADDITION OF TWO MATRIX:, #include<stdio.h>, main(){, int a[3][3],b[3][3],c[3][3],i,j;, printf("Enter the First matrix->");, for(i=0;i<3;i++), for(j=0;j<3;j++), scanf("%d",&a[i][j]);, printf("\nEnter the Second matrix->");, for(i=0;i<3;i++), for(j=0;j<3;j++), scanf("%d",&b[i][j]);, for(i=0;i<3;i++), for(j=0;j<3;j++), c[i][j]=a[i][j]+b[i][j];, printf("\nThe Addition of two matrix is\n");, for(i=0;i<3;i++){, printf("\n");, for(j=0;j<3;j++), printf("%d\t",c[i][j]);, }, }, MULTIDIMENTIONAL ARRAY, C programming language supports multi dimensional Arrays., •, •, •, , Multi dimensional arrays have more than one subscript variables., Multi dimensional array is also called as matrix., Multi dimensional arrays are array of arrays., , Declaration of Multi Dimensional Array, data_type array_name[size1][size2]...[sizeN];, , 8
Page 9 :
GFGC,Malur, Department of Computer Science, Unit 4, _____________________________________________________________________, Above statement will declare an array of N dimensions of name array_name, where, each element of array is of type data_type. The maximum number of elements that, can be stored in a multi dimensional array array_name is size1 X size2 X, size3...sixeN., For Example:, Declaration of three dimensional character array, char cube[50][60][30];, , PASSING ARRAY TO THE FUNCTION:, In C programming, a single array element or an entire array can be passed, to a function., This can be done for both one-dimensional array or a multi-dimensional, array., , #include <stdio.h>, main(), {, int ageArray[] = { 2, 3, 4 };, display(ageArray[2]); //Passing array element ageArray[2] only., }, void display(int age), {, printf("%d", age);, }, output, 4, , String, String is a collection of character or group of character, it is achieve in C language, by using array character. The string in C language is one-dimensional array of, character which is terminated by a null character '\0'. In other words string is a, collection of character which is enclose between double cotes ( " " )., , 9
Page 10 :
GFGC,Malur, Department of Computer Science, Unit 4, _____________________________________________________________________, Note: Strings are always enclosed within double quotes. Whereas, character is, enclosed within single quotes in C., , Declaration of string, Strings are declared in C in similar manner as arrays. Only difference is that,, strings are of char type., Example, , char s[5];, , Initializing Array string, String are initialize into various way in c language;, Example, char str[]="abcd";, OR, char str[5]="abcd";, OR, char str[5]={'a','b','c','d','\0'};, OR, char str[]={'a','b','c','d','\0'};, OR, char str[5]={'a','b','c','d','\0'};, , In c language string can be initialize using pointer., Example, char *c="abcd";, , 10
Page 11 :
GFGC,Malur, Department of Computer Science, Unit 4, _____________________________________________________________________, , Reading String from user, Example, , char str[5];, scanf("%s",&str);, Example, , #include<stdio.h>, #include<conio.h>, void main(), {, char str[10];, printf("Enter name: ");, scanf("%s",name);, printf("Your name is: %s.",name);, getch();, }, Example of reading string, , Enter name: Hitesh kumar, Your name is: Hitesh, , gets(), gets() are used to get input as a string from keyword, using gets() we can input, more than one word at a time., , 11
Page 12 :
GFGC,Malur, Department of Computer Science, Unit 4, _____________________________________________________________________, puts(), puts() are used to print output on screen, generally puts() function are used with, gets() function., Example of String program, #include<stdio.h>, #include<conio.h>, void main(), {, char str[10];, printf("Enter any string: ");, gets(str);, printf("String are: ");, puts(str);, getch();, }, , Explanation: Here gets() function are used for input string and puts() function are, used to show string on console or monitor., Output, Enter String: hello word, String are: hello word, , LENGTH OF THE STRING, #include<stdio.h>, main(), {, char str[100];, int length;, printf("\nEnter the String : ");, gets(str);, length = 0; // Initial Length, while (str[length] != '\0'), length++;, 12
Page 14 :
GFGC,Malur, Department of Computer Science, Unit 4, _____________________________________________________________________, }, i++;, j--;, }, if (flag == 0), printf("\nString is palindrome");, else, printf("\nString is not palindrome");, getch();, }, , C Library String functions, All the library function of String is available in String.h header file., , example:, // strlen() function, #include <stdio.h>, #include <string.h>, main(), {, , 14
Page 17 :
GFGC,Malur, Department of Computer Science, Unit 4, _____________________________________________________________________, Strings are just char arrays. So, they can be passed to a function in a, similar manner as arrays., #include <stdio.h>, void displayString(char str[]);, main(), {, char str[50];, printf("Enter string: ");, gets(str);, displayString(str); // Passing string c to function., }, void displayString(char str[]){, printf("String Output: ");, puts(str);, }, output:, Enter string:roses, String Output:roses, CTYPE.H, The ctype.h header file of the C Standard Library declares several, functions that are useful for testing and mapping characters., Sr.No., , Function & Description, , 1, , int isalnum(int c):This function checks whether the passed character is, alphanumeric., , 2, , int isalpha(int c):, This function checks whether the passed character is alphabetic., , 3, 4, 5, 6, 7, 8, 9, 10, , int iscntrl(int c):This function checks whether the passed character is, control character., int isdigit(int c):This function checks whether the passed character is, decimal digit., int isgraph(int c):This function checks whether the passed character, has graphical representation using locale., int islower(int c):This function checks whether the passed character is, lowercase letter., int isprint(int c):This function checks whether the passed character is, printable., int ispunct(int c):This function checks whether the passed character is, a punctuation character., int isspace(int c):This function checks whether the passed character is, white-space., int isupper(int c):This function checks whether the passed character is, , 17
Page 18 :
GFGC,Malur, Department of Computer Science, Unit 4, _____________________________________________________________________, 11, , an uppercase letter., int isxdigit(int c):This function checks whether the passed character is, a hexadecimal digit., , EXAMPLE, #include <stdio.h>, #include <ctype.h>, main(), {, char c;, printf("Enter a character: ");, scanf("%c", &c);, if (isalpha(c) == 0), printf("%c is not an alphabet.", c);, else, printf("%c is an alphabet.", c);, }, Output, , Enter a character: 5, 5 is not an alphabet., , EXAMPLE, #include <stdio.h>, #include <ctype.h>, main(), {, char c;, c = 'm';, printf("%c -> %c", c, toupper(c));, c = 'D';, printf("\n%c -> %c", c, tolower(c));, }, output, m -> M, D -> d, , 18