Skip to main content

Posts

Linear and Binary Search in C

Searching is one of the most common problems that arise in computing. Searching is the algorithmic process of finding a particular item in a collection of items. A search typically answers either True or False as to whether the item is present or not. On occasion it may be modified to return where the item is found. Search operations are usually carried out on a key field. Consider searching for a given value k in an array A of size n. There are 2 basic approaches: sequential search and binary search. Linear (Sequential) Search When data items are stored in a collection such as a list or array , we say that they have a linear or sequential relationship. Each data item is stored in a position relative to the others. In C array, these relative positions are the index values of the individual items. Since these index values are ordered, it is possible for us to visit them in sequence. This process gives rise to our first searching technique, the sequential search or linear sear

Command-line Arguments in C

The parameters passed to the program when the program is invoked are known as command line arguments. These parameters are the additional information like filename or other kind of input to the program. By passing command line arguments there is no need for the user to provide the input while executing the program. These command line arguments can be processed by using the arguments available in the main function. The main allows two parameters namely: argc and argv. The argc represents the argument counter which contains the number of arguments passed in the command line. The argv is a character pointer array which points to the arguments passed in the command line. To know the number of command line arguments, we can use the argc parameter of the main function and to access the individual arguments, we can use the argv array. The first element in the argv array is always the program name. So the first argument can be accessed by using argv[1] and so on. The prototype of main f

Files in C

A file is a repository of data that is stored in a permanent storage media, mainly in secondary memory. In order to use the files we should learn how to read information from a file and how to write information into a file. A very important concept in C is the stream.The stream is a common logical interface to the various devices( files).A stream is linked to a file while using an open operation. A stream is disassociated from a file while using a close operation. The current location, also referred to as the current position, is the location in a file where the next file access will occur.There are two types of streams text and binary. The following are some difference between text and binary files Text file is human readable because everything is stored in terms of text. In binary file everything is written in terms of 0 and 1, therefore binary file is not human readable. A newline(\n) character is converted into the carriage return-linefeed combination before being written to the