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.

Input integer array in C

String is also an array of char but we have functions to take input string . For inputting the int array ,float array we dont have functions like string. Here is the code for inputting integer array , similarly we can apply it for float too.

Example :

 

 

String input in C using gets

The problem with scanf function is it doesnot recognises space in Name. Exmple if you entered string as “Coding street” it will store only coding in string variable so it is recommended by us to use gets over string input. Because it recognizes space and takes space also as input.

Example :

 

Input basic Data types (int, float, char)

Below program takes input of integer, float, string value from keyboard and prints it on screen.

 

C struct

In C programming struct is a structured (record type) that creates a collection of different type of variable under a single name. If we want to store record of student. We can store name , rollno, phone no , address etc. using one variable, then struct makes it possible.

Syntax of struct in c :

struct  tag_name {

type attribute;

type attribute;

/ * ……. */

};

For example to store info of student :

Declaration of stu_info :

Accessing attributes:

Accessing attribute rollno;

rollno can be accessed by s1.rollno;

similiary other varibles also can be accessed.

Pointer to the struct:

 

 

Array of structure :

We have seen array of int , float , char .Similarly array of struct can also be defined. This is used to store students info , customer info etc..

 

 

 

C File Handling

File handling in c means we can create,open , delete files and modify contents in file.For this operations we will use some file handling functions. fopen, fclose, fscanf, fprintf ,fgets, fputs, feof etc.

File operations follow this procedure :

name of file

open or create file

read or write on file.

close file


File Handling Modes :

File Mode

Description

r

Opens a file in read only mode.

w

Opens a file in write mode , it creates a new file with provided file name if it doesnot exist.If file exists then file will be overwritten.

a

Opens a file in append mode ( new info will be added at end of file), If file doesnot exist then it will create a new file.

r+

Opens a file for read and write mode.

w+

Opens a file for read and write mode.It creates a new file if it doesnot exist.

a+

Opens a file for read and write mode. It create a new file if it doesnot exist.

rb

Opens a binary file for reading.

wb

Opens a binary file for writing.If file exist it will be over written.

ab

Opens a binary file in append mode.Info is added at end of file.

r+b or rb+

Opens binary file for reading and writing

w+b or wb+

Opens a binary file for read and write mode.It creates a new file if it doesnot exist.

a+b or ab+

Opens a binary file for read and write mode. It create a new file if it doesnot exist.

File handling functions :

fopen : To open or create file

fclose : To close a open file, we need file pointer of opened file.

Where fp is  file pointer.

fscanf : To read from file according to format provided.

fprintf : To write into file according to format provided.

feof : To check end of file.

 fgets : Reads a line from file and stores in string.

fputs: Writes a string into file.

 fgetc: Reads a single character from file into char variable.

 fputc: Writes a single character to file.

 Example :

Program to copy content of one file into other file.

Given:

Source filename :src.txt

Destination filename: dst.txt

Explanation :

Above code copies content of one txt file into other txt file.

sfp  & dfp are file pointers.

src file is opened in read mode and dst file is is opened in write mode.

We will stop copying content when source file(src.txt) file pointer reaches to EOF (EOF is end of file which indicates that we have reached at end of file ).

we are using fgetc function to get char from src file. We are storing fetched content from source file into character variable c, then we are storing back the value of character variable c into the destination file using fputc function.

After copying the content we are closing the opened files.