Docsity

C programming Pointers,Array, Recursion, search sorting, Study notes of C programming

Toggle sidebar
bg1
Notes Prepared By Ms. Aasiya Khatoon
(One Dimensional Array)
Introduction ARRAY
Array is a collection of similar data items or elements. Or we can say that array is a collection
of homogeneous data elements stored continuously under a single name.
Need for an array
When the number of variables of same type and nature are more than it is difficult to handle
them. So we need an array. Let us understand the use of array with an example:
int i,a;
for(i=1;i<=5;i++)
{
printf(“Enter the no.”);
scanf(“%d”,&a);
}
In the above program as soon as the new value is assigned to the variable a the old value is lost.
Thus, all the variables that we have used so far are not able to hold more than one value at a
time. But sometimes we wish to store more than one value for a variable. For example, if we wish to
arrange the marks of 10 students in ascending order.
There are two ways for it:
(i) Use of 10 different variables so that each variable can store marks of a single student.
(ii) Use a single variable which can store the marks of all students.
Obviously the second option looks better because it is easier to handle them. A single variable
which can store more than one value at a time is called an Array.
It is a collection of homogeneous data elements stored continuously under a single name. Array
declaration:
int a[6];
Here a is the name of the array, int is the data type of values which the array will store, 6 is the
size of array and [] are called as subscript operator. The size of array must be a constant. The most
important thing is that all the values in the array should be of the same type.
pf3
pf4
pf5

Partial preview of the text

Download C programming Pointers,Array, Recursion, search sorting and more Study notes C programming in PDF only on Docsity!

(One Dimensional Array)

Introduction ARRAY

Array is a collection of similar data items or elements. Or we can say that array is a collection of homogeneous data elements stored continuously under a single name. Need for an array When the number of variables of same type and nature are more than it is difficult to handle them. So we need an array. Let us understand the use of array with an example: int i,a; for(i=1;i<=5;i++) { printf(“Enter the no.”); scanf(“%d”,&a); } In the above program as soon as the new value is assigned to the variable a the old value is lost. Thus, all the variables that we have used so far are not able to hold more than one value at a time. But sometimes we wish to store more than one value for a variable. For example, if we wish to arrange the marks of 10 students in ascending order. There are two ways for it: (i) Use of 10 different variables so that each variable can store marks of a single student. (ii) Use a single variable which can store the marks of all students. Obviously the second option looks better because it is easier to handle them. A single variable which can store more than one value at a time is called an Array. It is a collection of homogeneous data elements stored continuously under a single name. Array declaration: int a[6]; Here a is the name of the array, int is the data type of values which the array will store, 6 is the size of array and [] are called as subscript operator. The size of array must be a constant. The most important thing is that all the values in the array should be of the same type.

10, 20, 30, 40, 50 are the elements of the array. To use the elements of the array we refer to the index. The array index starts from 0 i.e. if the size of the array is 6, so the indices of the array will be from 0 to 5. In the definition we also said that the elements of the array are stored continuously, i.e. if the element at 0 index has address 100 then the next elements will have addresses 102, 104, 106, 108, 110 if the array is an integer array because an integer occupies only 2 bytes. But if it was a float array the addresses will have been 100, 104, 108, 112, 116, 120 since a float number occupies 4 bytes.

Limitations of array –

  1. The array formed will be homogeneous. That is in an integer array only integer values can be stored, while in a float array only floating values and character array can have only characters. Thus, no array can have values of two data types.
  2. While declaring the array passing size of the array is compulsory, and the size must be a constant. Thus, there is either shortage or wastage of memory.
  3. Insertion or deletion of elements in an array will require shifting.
  4. The array does not check its boundaries: In C there is no check to see if the values entered in the array are exceeding the size of the array. Data entered with the subscript exceeding the array size will be simply placed outside the array, probably on the top of the data or the program itself. Solved Problems:
    1. Write a program to print the average of n numbers.

printf(“enter numbers :”); for(i=0;i<n; i++) { scanf(“%d”,&a[i]); } min=a[ 0 ]; for(i=0;i<n; i++) { if(min>a[i]) { max=a[i]; } } printf(“minimum element =%d”, max); getch(); }

4. Write a program in C to search a key is found in given list of numbers or not. Or WAP to implement linear search in C. void main() { int a[20], i, key; printf(“enter the number of elements in the array: “); scanf(“%d”,&n); printf(“enter numbers :”); for(i=0;i<n; i++) { scanf(“%d”,&a[i]); } printf(“enter key element: “); scanf(“%d”,&key); for(i=0;i<n; i++) { if(key ==a[i]) { printf(“%d is found at %d position in list”, key, i+1); break; } } if(i==n) { printf(“key is not found in list”); } getch(); }

Exercise – WAP- Write a Program.

**1. WAP to sort a 1-d array using bubble sort technique.

  1. WAP to sort a 1-d array using selection sort.
  2. WAP to sort a 1-d array using insertion sort technique.
  3. WAP to search an element in 1-d array using linear search method.
  4. WAP to search an element in 1-d array using binary search method.
  5. WAP to insert an element in 1-d sorted array.
  6. WAP to delete an element from 1-d array(unsorted) from the given positions.
  7. WAP that will read an array of integers and print even and odd element separately.
  8. WAP to find sum of element appearing at even and odd subscript position of an array of** integers. 11. WAP to find maximum and the minimum values from a set of values stored **in an array, along with their positions in the array.
  9. WAP that will read 2 array, sum their individual element in third array and display the** third array.

(Two-Dimensional Array (Matrix))

Introduction

Two-dimensional array is popularly known as tables or matrix and can be easily visualized as having rows and columns. Matrix can also be thought of as arrays of arrays. To create a two-dimensional array, specifying both dimensions i.e. rows and columns in square brackets. For e.g. the following declaration creates a matrix of 4 rows and 5 columns. int mat [4][5]; or #define MAXROW 4 #define MAXCOL 5 int mat [MAXROW][MAXCOL]; 0 1 2 3 4 0 0,0 0,1 0,2 0,3 0, 1 1,0 1,1 1,2 1,3 1, 2 2,0 2,1 2,2 2,3 2, 3 3,0 3,1 3,2 3,3 3, 4 4,0 4,1 4,2 4,3 4, Array are stored in row order, thus the expression mat [0] represents the first row of 5 values, mat [1] represents second row, mat [2] represents third row, and so on. Similarly, the expression mat [0][0] refers to the upper left value in the matrix, mat [2][3] represents the fourth value in third row.

Matrix initializes:

  1. int mat[4][3];
  2. int mat[4][3]={{10,20,30,40},{50,60,70,80},{90,100,110,120}};