
























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Subject: Programming in C (Course Code: 1BEIT105/205) Institution: Vemana Institute of Technology (Dept. of ISE) Affiliation: Visvesvaraya Technological University (VTU) Author: Prof. Chandana D.C This comprehensive study material covers Module 3 of the C Programming curriculum, providing an in-depth exploration of data structures and memory management. It is a vital resource for engineering students preparing for VTU exams. Detailed Syllabus Coverage: Arrays & Strings: Single-dimension, two-dimensional, and multidimensional arrays. Includes array initialization, variable-length arrays, and passing arrays to functions. Pointers: Fundamental pointer concepts, pointer variables, operators, expressions, and the relationship between pointers and arrays. Advanced Memory: Multiple indirection (pointers to pointers), initializing pointers, and an introduction to dynamically allocated arrays using malloc(). Key Features: The document includes theory explanation paired with practical example
Typology: Lecture notes
1 / 32
This page cannot be seen from the preview
Don't miss anything!

























Prof.Chandana D.C,Dept.of ISE. VEMANAIT
Arrays are used to represent multiple data items of the same type using single name.
The array is a collection of homogeneous elements of same data type. The array index starts with 0. Arrays are called as subscripted variables because; it is accessed using subscripts/indexes. Ex: 1. List of employees in an organization.
b) Initialization without size: In the declaration the array size will be set to the total number of initial values specified. The compiler will set the size based on the number of initial values. Syntax: data_type array_name[ ]={list of values}; Examples: int marks[ ]={ 95,35, 67, 87}; float temperature[ ]={29.5, 30.7, 35.6, 45.7, 19.5}; a) Partial initialization: If we not specify the all the elements in the array, the unspecified elementswill be initialized to zero. In such a case elements are initialized in the order from 0th element. The remaining elements will be initialized to zero automatically by the compiler. Example : int marks[5]={10,12,20}; Here, marks[3] and marks[4] will be initialized to zero. a) Initializing all the elements zero: If we want to store zero to all the elements in the array we can do. Examples: int marks[4]={0}; e) String Initialization Sequence of characters enclosed within double quotes is called as string. The string always ends with NULL character (\0) Assigning values to arrays Using assignment operators, we can assign values to individual elements of arrays. For example: int a[3]; a[0]=10;
a[1]=20; a[2]=30;
2. Run time initialization: Run time initialization is storing values in an array when program is running or executing. Example: printf(“Enter 4 marks”); for(i=0; i<4; i++) { scanf(“ %d”, &marks[i]); } Reading and writing single dimensional arrays. To read array elements from keyboard we can use scanf() function as follows: To read 0th element: scanf(“%d”,&a[0]); To read 1st element: scanf(“%d”,&a[1]); To read 2nd element: scanf(“%d”,&a[2]); …… ……. To read nth element : scanf(“%d”,&a[n-1]); In general To read ith element: scanf(“%d”,&a[i]); where i=0; i Write a c program to find largest of n elements stored in an array a. #include void main() { int a[10],i,n, big; printf("Enter size of array: "); scanf("%d",&n); printf("Enter %d elements in the array : ", n); for(i=0;ibig) big=a[i]; } printf(" big %d ",big); } OUTPUT Enter size of array: 5 Enter 5 elements in the array : 12 45 7 1 7 Elements in array are: big 45 Write a C program to generate Fibonacci numbers using arrays. #include void main() { int fib[24]; int i,n; printf("Enter size of array: "); scanf("%d",&n); fib[0] = 0; fib[1] = 1; for(i = 2; i < n; i++) { fib[i] = fib[i-1] + fib[i-2]; } for (i = 0; i < n; i++) { printf("%d \t", fib[i]); } } OUPUT
Write a C program to evaluate the polynomial using Horners method. #include void main() { float a[100],sum=0,x; int n,i; printf("\nEnter degree of the polynomial X :: "); scanf("%d",&n); printf("\nEnter coefficient's of the polynomial X :: \n"); for(i=n;i>=0;i--) { printf("\nEnter Coefficient of [ X^%d ] :: ",i); scanf("%f",&a[i]); } printf("\nEnter the value of X :: "); scanf("%f",&x); for(i=n;i>0;i--) { sum=(sum+a[i])*x; } sum=sum+a[0]; printf("\nValue of the polynomial is = [ %f ]\n",sum); }
Enter degree of the polynomial X :: 3 Enter coefficient's of the polynomial X :: Enter Coefficient of [ X^3 ] :: 1 Enter Coefficient of [ X^2 ] :: 3 Enter Coefficient of [ X^1 ] :: 2 Enter Coefficient of [ X^0 ] :: 5 Enter the value of X :: 4 Value of the polynomial is = [ 125.000000 ]
2. Run time initialization: Run time initialization is storing values in an array when program is running or executing. Following example illustrates run time storing of values using scanf and for loop: Example: printf(“Enter the marks”); for(i=0; i<3; i++) for(j=0;j<3;j++) { scanf(“ %d”, &marks[i][j]); } More Examples: Other way of initialization: int a[ ][3]= { 0, 1, 2, 3,4,5,6,7,8}; int b[ ][4] ={1,2,3,4,5,6,7,8,9,10,11,12}; a Example for Invalid initialization int A[3][ ]={1,2,3}; Note: Never have column size undeclared in two dimension array.
array of 2D arrays. It has row, columns, depth associated with it. NOTE: USING ARRAYS WITH FUNCTIONS: In large programs that use functions we can pass Arrays as parameters. Two ways of passing arrays to functions are:
int main( ) { int num[5], i; num[5] ={1, 2, 3, 4, 5}; for(i=0; i<5; i++) { square(num[i]); } } int square(int n) { int sq; sq= n * n; printf(“%d ”, sq); } int main( ) { int marks[5], i; marks[5] ={10, 20, 30, 40, 50}; sum(marks); } int sum(int n[ ]) { int i, sum=0; for(i=0; i<5; i++) { sum = sum+n[i]; } printf(“Sum = %d ”, sum); } Write a c program to read & print 2d array as a Array. #include void main() { int m, n , i, j , a[3][3]; printf(“Enter number of Rows and Columns\n”); scanf(“%d %d”, &m, &n); printf(“Enter array Elements\n”); for(i=0;i for(j=0;j void main() { int m, n, i, j, a[3][3]; printf(“Enter Number of Rows and Columns\n”); scanf(“%d %d”, &m, &n); printf(“Enter Array Elements\n”); for(i=0; i a[i][j]) big = a[i][j]; } } printf(“Big is %”, big); }
Matrix Multiplication Programs #include #include void main() { int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,q; printf("Enter the order of matrix A\n"); scanf("%d%d",&m,&n); printf("Enter the order of matrix B\n"); scanf("%d%d",&p,&q); if(n!=p) { printf("Matrix multiplication is not possible\n"); exit(0); } printf("Enter the elements of matrix A\n"); for(i = 0 ; i < m; i++) { for(j = 0 ; j < n; j++) { scanf("%d",&a[i][j]); } } printf("Enter the elements of matrix B\n"); for(i = 0 ; i < p; i++) { for(j = 0 ; j < q; j++) { scanf("%d",&b[i][j]); } } for(i=0 ; i < m ; i++) { for(j=0 ; j < q ; j++) { c[i][j] = 0; for(k=0;k if(Flag == 1) { printf("\n The Matrix that you entered is an Identity Matrix "); } else { printf("\n The Matrix that you entered is Not an Identity Matrix "); } } C program to find Transpose of a Matrix #include void main() { int a[10][10], transpose[10][10], r, c; printf("Enter rows and columns: "); scanf("%d %d", &r, &c); // asssigning elements to the matrix printf("\nEnter matrix elements:\n"); for (int i = 0; i < r; ++i) for (int j = 0; j < c; ++j) { printf("Enter element a%d%d: ", i + 1, j + 1); scanf("%d", &a[i][j]); } // printing the matrix a[][] printf("\nEntered matrix: \n"); for (int i = 0; i < r; ++i) for (int j = 0; j < c; ++j) { printf("%d ", a[i][j]); if (j == c - 1) printf("\n"); } // computing the transpose for (int i = 0; i < r; ++i) for (int j = 0; j < c; ++j) { transpose[j][i] = a[i][j]; } // printing the transpose printf("\nTranspose of the matrix:\n"); for (int i = 0; i < c; ++i) for (int j = 0; j < r; ++j) { printf("%d ", transpose[i][j]); if (j == r - 1) printf("\n"); } }
The process of finding a particular item in the large amount of data is called searching. The element to be searched is called key element. There are two methods of searching: 1] Linear search. 2] Binary search
C program to search a key in a list of names using Binary searching technique/* #include #include void main() { int a[20],low,mid,high,i,n,k; printf("Enter the number of elements\n"); scanf("%d",&n); printf("%d\n",n); printf("Enter the elements\n"); for(i = 0 ; i < n ; i++) scanf("%d",&a[i]); printf("Enter the element to be searched\n"); scanf("%d",&k); low = 0; high = n - 1; while(low <= high) { mid = (low + high)/2; if(a[mid] == k) { printf("Successful search, element %d found at %d\n",k,mid+1); exit(0); } else if(a[mid] > k) { high = mid - 1; } else { low = mid + 1; } } printf("Unsuucessful search\n"); }