Category Archives: C Array

C Code of arrays.

Quick Sort on Array

Algorithm: Quick Sort on Array
Complexity : Average case O(n logn) , worst Case O(n^2)

Code:

Quick sort on array.

Heap Sort on array

Algorithm : Heap Sort on Array
Complexity :O(n logn)

 

Heap sort in Array code in C .

insertion sort on array

Algorithm : Insertion Sort on array
Complexity : O(n^2)

Insertion sort on array code in C .

Sort an array in C (Basic Method)

Complexity : O(n2)

Explanation:

Below code picks a cell one by one and compares with each other cells , if value of current cell is greater than other cell then it will swap with other value. Similarly it performs untill picked cell is last cell.

Code :

 

 

C program to find given element in array

C program to find given element in array

Explanation :

So far we have seen how to take input in array and printing the same. We have got knowledge, how to scan an array ? Right…  In this post we are finding an element if it found then we have to print its position otherwise say “Element not found “. We have read the relational operators which are used to compare values.

step 0 : initialize found variable as 0 means notfound

step1: Input the array and value to be searched

Step2 : Scan the array from start position to size of array

— check for equality of array element (arr[i] ) and search value (findval) using (arr[i]==findval)

— above condition is true then initialize fount=1 and break the loop

step3 : if found==1 then print the found value and position too.

 

Code :

 

C program to find given element in array.

 

A C code to print array reverse

A C code to print array reverse (printing array from last position to first)

Explanation : To print element in reverse order we must start loop from last position of array and then print upto it reaches to zeroth position. Code :

A C Code to Input array and print the same

Input array and print the same.

Explanation :

we can access element in array using array name and [ ]. Just specify the position within square bracker [ ] just after the array variable name.

Here we have defined int array of size 100. We have defined size variable which can go max upto 100 (because array size max can reach upto 100). Then we have taken size as input using scanf function then we have initialized for loop from zero because array starts default from zero in C. We will  run the loop upto size-1 which is controlled by (i<size) . In the body part of for loop we have given arr[i] as argument in scanf function, to store value from input into arr[i] untill loop gets terminated.

After inputting array we are printing array using for loop.

Input array and print the same code in C.

C 1D,2D Array

Array is data structure in C . They are defined at compile time. Used to store same kind of information in sequential manner. Contiguous memory is allocated for array.

If we want to store marks of 100 students. Instead of declaring 100 float variables like (float marks1,marks2, …. ,marks100) we can declare one variable marks[100];

Caution: array starts from 0;

so information can be stored in marks[0]. marks[1], .. . . . , marks[99].

How to declare an array ?

data_type   identifier[ size_of_array];

eq.

int a[100];

float b[30];

char c[50];

 

How to initialize an array at the time of declaration ?

int a[5]={1,2,3,4,5};

value of a[0] is 1;

value of a[1] is 2;

similarly value of a[4] is 5;

a[5] is not accessible because we have declare array as int a[5] which consist of 5 variables from a[0] to a[4].

 

Accessing element in array :

float arr[6];

we can access 5 th element in array by arr[4] .

 

Types of Array :

  • 1D Array
  • 2D Array
  • Multidimensional array.

1-D Array :

Declaration :

int arr[10];

Element access :

eq. 2nd element

arr[1]

2-D Array :

Declaration :

int arr[5][5];

the above declaration will create 5 X 5 array with name arr.

Element access:

To access 2nd element in row and 3rd element in column we can access by a[1][2];

 

=>similarly we can apply rule in multidimensional array.

Examples:

1-D Array

To declare an array of size 5 ,input the value from user printing the same.

2-D Array 

Declare a 2-D array of size 3X3 and store below matrix and print.

1 2 3

4 5 6

7 8 9

 

Click here to read more