Thursday, 21 February 2019

Linear Search in C without using function

In Searching, you are given a set of elements and a key, you have to find the location of given key in that set.
These algorithms are generally classified into two categories:
  1. Sequential Search: In this, the set of elements is traversed sequentially and every element is checked against key. For example: Linear Search.
  2. Interval Search: These algorithms are used when elements in the given set are sorted. These algorithms are more efficient than Linear Search.For Example: Binary Search.
(1).Linear Search

  1. Traverse given array starting from first element.
  2. Compare element at current index with given key.
  3. If key matches with an element, return the index.
  4. If key doesn’t match with any of elements, return -1.
 
  Problem: Given an array array[] of size n, write a program in C to search a given element x in array[].
   Examples :
     Input : array[] = { 10, 15, 9, 12, 50,32 };
     key = 15;
     Output : 2
     Element key is present at index 2.
   
     Input : array[] = { 10, 15, 9, 12, 50,32 };
     key = 65;
     Output : -1
     Element key does not exist in given set of elements.
  
Solution:
#include <stdio.h>
     int main()
      {
       int array[] = { 10, 15, 9, 12, 50,32 };
       int key = 9,arraySize=0,i,index=-1;
      
     //finding the size(no of elements) of array
       arraySize = sizeof(array)/ sizeof(int);
      
       for (i = 0; i < arraySize; i++){             
         if (array[i] == key){
            index=i;  //key found at index i
         }          
       }
      
       if(index!=-1)
         printf("Element %d found at position %d.\n",key,index+1);
       else
         printf("Element %d not exist in given set of elements.\n",key);
              
       return 0;
      }    

Please comment if you find anything incorrect, or you want to improve the topic discussed above. 

Wednesday, 20 February 2019

Keywords in C

Keywords
  • Keywords are reserved words whose meaning is predefined by the programming language specification(compiler). 
  • They convey some special meaning in programming and we must not use them for other purposes. 
  • Keywords are part of the syntax and they cannot be used as an identifier.
  • An identifier cannot have the same spelling and case as a C keyword.
For example – float is a C keyword that defines real numbers. You can’t use float for other purposes.
As C is a case sensitive language, all keywords must be written in lowercase.
  1. Keywords are those words whose meaning is already defined by Compiler
  2. Cannot be used as Variable Name
  3. There are 32 Keywords in C
  4. C Keywords are also called as Reserved words .

Following table shows the lost of keywords(list taken from https://en.cppreference.com/w/c/keyword):
Please comment if you find anything incorrect, or you want to improve the topic discussed above.

Monday, 18 February 2019

Constants in C

What are constants?
Constants in C are the fixed values that are used in a program, and its value remains the same during the entire execution of the program.
Example: 9,3.14,'T' etc.
Points to remember:
· Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are enumeration constants as well.
· Constants are also called literals.
· It is considered best practice to define constants using only upper-case names.
How to define constants?
In C program we can define constants in two ways as shown below:
  1. Using #define preprocessor directive
  2. Using const keyword

1).Using #define preprocessor directive

This directive used to declare an alias name for existing variable or any value. We can use this to declare a constant as shown below:

Syntax:
#define IDENTIFIERNAME value
where IDENTIFIERNAME is the name given to constant and value is any value assigned to it.
Below is the C program to explain how to use #define to declare constants:
#include<stdio.h>
#define PI 3.14

int main(){
            int radius;
            float area;    
            //Ask user for radius of the cicle
            printf("\nEnter radius of circle:");
            scanf("%d",&radius);        
            //calculate areaof the circle
            area=PI*radius*radius;  
            printf("\nArea of the circle having radius %d is %f.\n",radius,area);      
            return 0;
}
Output:














2).Using const keyword 
Using const  keyword to define constants is as simple as defining variables, the difference is you will have to precede the definition with a const keyword.

Syntax:
const dataType IDENTIFIERNAME =value;
or
dataType const  IDENTIFIERNAME =value;

Example:
    const float rate=12.5;

Below program shows how to use const to declare constants of different data types:
#include<stdio.h>

int main(){
            const float PI=3.14;
            int radius;
            float area;
            //Ask user for radius of the cicle
            printf("\nEnter radius of circle:");
            scanf("%d",&radius);        
            //calculate areaof the circle
            area=2*PI*radius;  
            printf("\nArea of the circle having radius %d is %f.\n",radius,area);      
            return 0;
}
Output:






Types of constants
1).Integer Constants
An integer is a numeric constant (associated with number) without any fractional or exponential part. There are three types of integer constants in C programming:
·                     decimal constant(base 10)
·                     octal constant(base 8)
·                     hexadecimal constant(base 16)

2. Floating-point constants

A floating point constant is a numeric constant that has either a fractional form or an exponent form.

3. Character constants

A character constant is created by enclosing a single character inside single quotation marks.Character constants have a specific set of integer values known as ASCII values (American Standard Code for Information Interchange).
Example: '9', 'T', ';'

 

4. String Literals

A string literal is a sequence of characters enclosed in double-quote marks. For example:
Example: "Techabaya", "2019".

Please comment if you find anything incorrect, or you want to improve the topic discussed above..

Sunday, 17 February 2019

Placement Papers -TCS (Aptitude) 1


 1. Two Pencils costs 8 cents. 5 Pencils will cost ,how much ?

 2.A work is done by two people in 24 minutes. One of them alone can do it in 40 minutes. How much time will the other person will take to complete it?

 3. A car is filled with 4.5 gallons of fuel for a round trip . Car is taken 1/4 more than in going than coming up. What is the fuel consumed in coming up ?


4. Low temperature at the night in a city is more than 1/2 high as higher temperature are 100. Then What is low temperature?

5. Person who dicided to go to weekend trip should not exceed  8 hrs driving in a day. Average speed of forward journey is 40 miles/hour . Due to traffic in sundays ,in return journey average speed is 30mph .How far he can select a picnic spot ?


6. A sales person multiplied by a number and get the answer 3. Instead of that number divided by 3. What is the answer she actually has to get?

7. A ship started from port and moving with 'I' miles/hour and other ship started From L and moving with 'H' miles/hour . At the place where these two ships will meet. 

8. A building with height D shadows up to G. A neighbour building with what height shadows C feet?

9. A person was fined for exceeding the speed limit by 10mph. Another person was also fined for exceeding the speed limit by twice the same. If the second person was traveling at speed of 35mph . Find the speed limit.

10. A bus started from the bus stand at 8 Am and after staying 30 minutes at a destination return back to the bus stand. The destination is 27 miles from the bus stand . The Speed of the bus is 18mph . In the return journey the bus travels with 50%  fast speed.At what time it is return to the bus stand?

11. In a mixture R is two parts, S is one part . In order to mixture how much R is to be added?

Variables in C

What is variable
  • A variable in a program is a specific piece of memory that consists of one or more contiguous bytes, typically 1, 2, 4, 8 or 16 bytes.
  • Every variable has a type that specifies the kind of data the variable can store.
  • The type of a variable determines how many bytes are allocated for it.
  • Every variable in a program has a name, which will correspond to the memory address for the variable.
  • You use the variable name to store a data value in memory or retrieve the data that the memory contains.
  • The value of a variable isn’t fixed, and you can change it whenever you need to throughout a program.
Rules for naming variables
  • A variable name is a sequence of one or more uppercase or lowercase letters, digits, and underscore characters (_) that begin with a letter (incidentally, the underscore character counts as a letter).
  • A variable name must not begin with a digit.
  • A variable name must not include characters other than letters, underscores, and digits.
  • Variables starting with one or two underscore characters are often used in the header files, so don’t use the underscore as the first character in your variable names.
  • Variable names is that they are case sensitive. 
Notes:
  • The maximum number of characters that you can have in a variable name will depend on your compiler.
  • A minimum of 31 characters must be supported by a compiler that conforms to the C standard.
Types of variables
The type of a variable is determined by the its data type while declaring the variable. Variables can be of several type from simple type to complex type.

(a).Integer type variables
·         Used to store integer values.
·         Type is int.
·         Example : int marks=98;

(b).Real type variables
·         Are used to store real numbers.
·         Type is float, double etc.
·         Example : float rate=12.5;

(c).Char type variables
·         Used to character values.
·         Type is char.
·         Example : char grade=’A’;

(d).Complex type variables
·         Like struct,union type;

Variable declaration
Variable declaration specifies the variable name and type of data that variable will store.
Syntax for declaring a variable:
dataType variableName;
example : int salary;  // variable declaration

This statement is called a variable declaration because it declares the name of the variable.
Note that a variable declaration ends with a semicolon. If you omit the semicolon, your program will generate an error when you try to compile it.

Assigning value to a variable
The assignment operator(=) is used to assign value to a variable;

Example
int marks=98;
char grade=’A’;
float rate=12.5;


//Program to ask user their marks and then display the marks.
#include<stdio.h>
int main(){
  int marks;
  printf(Enter marks:“”);
  scanf(“%d”,marks);
  printf(“Your marks are %d.”,marks);
 return 0;
}

Please comment if you find anything incorrect, or you want to improve the topic discussed above..

Saturday, 16 February 2019

How to set timer in Google


Google is best known for its searching capabilities.But there are other things, you can do with Google.
In series of Google Tricks, I will explain you some simple but useful things which comes handy in day-to-day life.
In this first post of Google Tricks series, I will explain how you can use Google as a timer.
Suppose you are working on your computer and want to be alerted after a period of time for another work. But you don't have mobile or clock to set time. Don't worry when Google is here.

Here is the simple procedure you can use Google as a timer.

Simply  type "set timer to 1 minute 30 seconds"is Google search box. Change time as per your requirement.


You will get sound as time expires.

Timer will start automatically. You can stop it anytime. Also you can reset it at any point of time.

If you liked this article, please share it...