Tuesday 31 January 2012

Algorithm and Flow charts


                                                                    Algorithm

Introduction:

In order to develop any problem in computer science generally we write programs before writing programming languages . It is always better to write we write a informal discretional for the solution, Which is also called as algorithm.

An algorithm is just like a cooking recipes for example:

If you want to prepare any tea:
Ingredients:
1.       2 cups of water
2.       Tea powder
3.       2 table spoon sugar
4.       Match box to light the gas stove
5.       Bowl
6.       Cups are required to serve
7.       1 cup of Milk
Instructions:
    Step 1: switch on the gas stove
    Step 2: Bowl should be kept on the gas
    Step 3: pour the water into the bowl    
    Step 4: add two tea spoon of tea powder
    Step 5: Wait until it is boiled
    Step 6:  take 2 tea spoons of sugar and add into the boiled water
    Step 7:  Wait for 2 minus for boiling the water
    Step 8:  add milk to the boiled water
    Step 9:  Let it be boiled
    Step 10: Now Tea is ready
    Step 11: Switch off the gas stove.

In the above instructions are fairly precised these instruction are not required for Expert person. These instructions are useful for beginners, in the same way algorithms are very much important to the beginners.

Now we see we have some problem.

Problem-> programs like c

Before writing the program if u write a blue print it is informal language ie. English like language.
To describe what we want to write so that it will be easier for us to communicate before implementation.
So that is called algorithm.
So before going for any problem to solve the problem for writing the program we need the algorithm.
In general if there is a problem p1, there might be many solutions. Let me call these solutions are algorithms. So there may be many algorithms(solutions) like A1,A2,A3.,,,,
Now before implementing any algorithm as a program we have to find out which algorithms are good in terms of time and memory. So we should analyze every algorithm like time here time means how much time is taking to execute. And Second is Memory which one will take less memory. So less time and less memory. So this code design analysis of algorithm .
Design Analysis of Algorithm: here design talk about how to design various algorithms like for a given problem (example p1), how many algorithms are there and how to analyze them so what will be time taken this algorithm, what will be the memory by each algorithm.
Once we selected the best algorithm example A2 is the best algorithm, then I implement that algorithm. So that is the main process of designing the algorithms.


What is Algorithm?




Definetion:

Algorithm is used to solution of a problem step by step procedure using simple english language. It is the part of software designing. An algorithm defines as the step by step procedure or method that can be carried out for solvingprogramming problems. An algorithm tells computer that how to solve problem systematically to get desired output. Mainly we follow following steps to design an algorithm.

Step 1 - START
It represents beginning of the algorithm.

Step 2 - DECLARE
The variables used in algorithm are declared in this step.

Step 3 - INPUT
Here we input the values

Step 4 - FORMULA
The required result is generated in this step.

Step 5 - OUTPUT
It displays the output or result.

Step 6 - STOP
It is an end of Algorithm




                                                  
              FLOW CHART
Flow char is a graphical(pictorial) representation of algorithm. It is used to represent algorithm steps into graphical format. It also gives us an idea to organize the sequence of steps or events necessary to solve a problem with the computer. In other words flow charts are symbolic diagrams of operations and the sequence, data flow, control flow and processing logic in information processing. These charts are used to understand any working sequence of the program.

What are the Advantages(uses) of FLOW CHART?
Ans:
Advantages of flowchart:-

1. It provides an easy way of communication because any other person besides the programmer can understand the way they are represented.

2. It represents the data flow.

3. It provides a clear overview of the entire program and problem and solution.

4. It checks the accuracy in logic flow.

5. It documents the steps followed in an algorithm.

6. It provides the facility for coding.

7. It provides the way of modification of running program.

8. They shows all major elements and their relationship.

erere


Terminator
This symbol represents the beginning and end point in a program. We use start and stop option in it.

Input/Output Symbol
This symbol is used to take any input or output in the algorithm.

Process Symbol
A rectangle indicates the processing, calculation and arithmetic operations

Decision Symbol
It is used when we want to take any decision in the program.

Connector Symbol
This symbol is used to connect the various portion of a flow chart. This is normally used when the flow chart is split between two pages

Data Flow Symbol
This symbol is used to display the flow of the program. It shows the path of logic flow in a program.

Monday 23 January 2012

Statements in C



Definition:


Collection of Tokens are called statements and ends with semicolon(;). (or) It is a instruction to the computer to perform specific action.


Types of Statements:


1. Simple Statement or Expression Statement

2. Compound Statement or Block Statement

3. Control statement or Control structures or Branching Statements


1. Simple Statement or Expression Statement:


It follows any c valid expression and followed by semicolon.


Ex:
 int a;
 a=a+1;
 printf("Hello World\n");
 ;


2. Compound Statement or Block statement:


It is collection of statements enclosed in braces ({,}).


Example:


{

 printf(" I'm Rajendra\n");

 getch();

}


3. Branching Statements (Selective Structures)

Branching: the statement which transfers control from one statement to another statement breaking the normal sequence of execution is known as control statement.


Types of Control Structures
 
1. Conditional statements

     if statement
      if..else statement
      if...else if....else Statement
      switch statement

2. Unconditional statements or Jump Statements

       break
     continue
     go to


3. looping statements or repetative statements

   for loop
   while loop
   do-while

Review Questions in Statements

Short answers type
4) Differentiate for do while loops.
7) List any four important features of C language.
8) What is a null statement?
9) List the various conditional statements available in C.
10) What do you meant by conditional expressions?
11) Write a C program to find the whether the given year leap year or not.
12) Write a C program to print the following output: 
                      1
                      22
                      333
13) Explain the switch statement with syntax.
14) Write a C program to find the factorial of a given number.
15) Write the difference between it and else if statements.

                                                    Essay type questions
1) With the help of a flow chart explain the working of while loop.
2) With the help of a flow chart explain the working of do while loop.
3) Explain the working of for loop with the help of flow chart.
4) Explain how continue statements effect the looping.
5) Explain the comma operator with the help of a program.
6) List out the various conditional statements available in C and explain about them with the help of flow chart and a program.
7) Explain multiday conditional statements with the help of a program.
8) Explain the switch statements with the help of a floe chart.
9) With a C program to print whether inputted character is a vowel or not using switch statement.
10) Explain the following statements used in C
(a) if statement
(b)if….else statement
(c)while
(d)do….while.
11) Write a C program to print between 1 and 100.
12) (a)What is a conditional operator ? explain.
     (b)Write the syntax of Do…..while loop with sequence of operations.
13) Explain the working of nested for loop.
14) Write a program in C to calculate loop
    1+2+3…………100 using while loop




---------------------------------------------------------------------------------------------------------
(review questions)
1.       State the need for branching statements?
2.       What are the branching statements used in c?
3.       Write the syntax of various conditional statements available in ‘C’
4.       (i) define conditional statement and list the conditional statements used in ‘C’ language (ii) Explain the different branching statements available in C with examples. (or) List the various conditional statements in c and explain briefly with suitable examples. (or) Explain in detail different conditional statements with their flowcharts and examples
5.       Explain if..else statement with syntax and example
6.       (i) explain the following statements used in C. (a) if statement (b) if..else statement (ii) explain about syntax of (a) if—statement (b)if-else statement (c) nested-if-statement in C.
7.       (i) explain if..else statement with a flowchart. (ii) write the syntax for  if—else statement in C with an example.
8.       Describe different forms of if statement in c
9.       Explain nested if--.else statement in c.
10.    Write the differences between if and else ..if statements
11.    Give the general form of a switch statement and describe how the switch statement is executed (0r) explain the switch statement in c with syntax
12.    Explain in what way does a switch statement differs from an if statement.
13.    Give the syntax of the switch statement with an example.
14.    Explain the switch statement with the help of a flowchart and a suitable c program
15.    write a program to find the average, grade of the 3 subjects using switch case



You may like the following posts:

C Tokens

                                   

Sunday 22 January 2012

C Programming Basics Questions and Answers


Chapter 2 C Programming Basics
Questions: 
/*Note: I gave the answers some of the questions, some of the questions i could not type here, i will post later */
1.        List any four applications of ‘c’ language.
2.        Write any four important features of ‘c’ language.
Ans: 

Features of c-language
1. Modularity(Structure Oriented or Function oriented)
2. Extendability
3. Portability
4. Robustness
5. Flexibility
6. Fast execution (speed)
7. Reusability
For more information click on C Features

3.        Write the character set of c programming language.
Ans: To see the answer click on Character set
4.        List C Tokens.
5.        Describe the different types of tokens used in ‘c’.
6.        List the various types of data in c and give examples.
7.        What are the constants? What are the different types of constants in’c’?
Ans: Constants:If we declare any variable as a constant the value of the variable will not be changed or     modified
       Syntax: Data type const variable=value;
8.        (1) List and explain the four basic types of constants in ‘c’. (2) List the basic data types available in c language and give example for each.
Ans:Data type

9.        What is the character constant? How do characters constant differ from numeric constants? Do character constants represent numeric values?
10.     What is a string constant? How do string constants differ from character constants? Do string constants represent numeric values?
11.     What is a key word? List at least four key words in c.
Ans:
Keywords: At the time of designing the c-language some words are used to do a specific task called keywords. There are 32 keywords are available in c-language.
EX:  int,float,char,double,unsigned,auto,const,while,if,do,else,static,long, short, register signed, default, staticfor,break,continue,goto, volatile , extern etc…..,
     
12.     What is a variable? What are the rules for naming a variable? (Or) what are the rules to be observed in naming an identifier in c?
Ans: 1. Variables:An variable is an identifier(user defined word) It is used to store the data the variable can stores one value
Syntax: datatype variable;
Ex: 
     int a;
     a=10;

Rules for the Identifier :
1.It should starts with alphabetical letters only
2.It should not starts with numerical values, special symbols.
3.It should not exit more than 32 characters.
4. Keywords are not used as variables.

14.     What are the different types of variable? What are the rules for naming a variable?
rules for the variable are

1.It should starts with alphabetical letters only
2.It should not starts with numerical values, special values.
3.It should not exit more than 32 characters.
4. Keywords are not used as variables.

15.     Explain the declaration of variables in detail.(Or) what is the purpose of type declaration?
16.     (1) List and explain the four basic (primary) data types in ‘c’.(Or) name the different fundamental data types that C supports and explain them in detail.(2) explain different data types in c-language with examples. How are constants declared in c- language?

Ans: (a): Basic Data types or Fundamental Data types or Primitive Data types are:
    Name of datatype                            
      Size
      Range
      int
      2                                      
      32, 767 to +32,767
      float
     4
      7.8390 with 6decimal
      char
     1
     -128 to +127
     double
      8
     3.123456789123…
      long int
      4
     -2147483648 to +21147483648
     long double
     10
     3.4E-4932 to 1.1E+4932

  The Constants are declare as below:
   Syntax: const datatype variable=value;
               const int a=10;
17.     Classify data kinds in c and explain the primary data types in c with examples. (Or) discuss in detail about various kinds of data manipulated by a computer program.
18.     List the arithmetic operators in c.
Ans:

     operators
     meanings
             +
             -
             *
            %
            ÷
     Addition
       Subtraction
       Multiplication
      Percentage
      division

19 operators and Expressions
19.     What is an Operators? Describe several different types of operators included in ‘c’.
20.     List the arithmetic operators in c (Or) Explain different arithmetic operations in C programming language
21.     What is meant by operator precedence and associativity of an operator?
Ans:
    For operators, associativity means that when the same operator appears in a row, then to which direction the evaluation binds to. In the following, 

Usually + and - have the same precedence. Consider the expression 7 − 4 + 2. The result could be either (7 − 4) + 2 = 5 or 7 − (4 + 2) = 1. The former result corresponds to the case when + and  are left-associative, the latter to when + and - are right-associative.

22.     What is meant by operator precedence and state the hierarchy of arithmetic operators?
23.     What is the precedence of relational operators in C language?
24.     Differentiate between ‘=’ and’==’ in C language.

               =    
              ==
      1. It is a assignment operator                   
     1.it is a relational operator
      2. It is is used to assign the value         
       to the variable
     Eg: int a=10;
     2. It is used to compare the value to the left hand side variable.
     Eg : int a=5;
            a=10;
     3.The value 10 is string into the   variable a
     3.The value 10 is compare to the left hand side variable.

25.     (1) List and state the hierarchy of logical operators.(2) explain the logical operators in c language and their precedence.
26.     What is the modulus operator and how does it work in ‘C’?
27.     What is meant by assosiativity? What is the assosiativity of the arithmetic, relational and logical operators?
28.     Define an expression and show how to evaluate an expression with an example.
29.     With the help of an example explain how a logical expression is evaluated.
30.     Name and describe relational, logical and assignment operators.
31.     Summarize the standard escape sequences in C.describe them.(or) what is an  escape sequence? What is its purpose? List the standard escape sequences in C.
Ans:  Escape sequences is consider as character motion . it is also known as back slash character

      Escape sequences
     Meaning
       /n                                         
     Next line
        /t                                     
      Tabular space
      /b                                      
      Back space
      /a                                      
     alarm (beap sound)
     /0
     Null character

32.     List assignment or compound operators in C.
33.     Explain any four assignment operators with examples.
34.     What is a unary operator? List the various unary operators in C.
35.     What are the commonly used input/output functions in C? How are they accessed? (Or) explain input and output in C language with examples.

(or)
     List and give the syntax of I/O statements used in C.
Ans:Input output statements in c

Input and output functions are divided into two parts
1.     Formatted functions
2.     Unformatted functions
Again formatted and unformatted functions are divided into few types
They are:
Formatted functions:
printf()
scanf()
Unformatted functions:
putchar()
getchar()
puts()
gets()
37.     Describe the formatted input and output functions.
Ans:
printf(): It is used to output function. It is used to display the message or output on the monitor

It is a call by value function. It is predefined in <stdio.h>
Syntax:    printf(“control string”, variable);
               printf(“message”);
scanf(): it is used to a input function. It is used to the data from the keyboard at run time.
                It is also predefined in <stdio.h>
   
Syntax:
                scanf (“control”,&var1,&var2);
Eg:
                scanf (“%d%d”,&a,&b);
It is call by address function.
The value or data read from the keyboard and converts by using the format specifiers and stores into variable address.

38.     List the commonly used math library functions in C.
39.     Explain the usage of comma operator. (Or) what is comma operator? Explain its syntax with suitable example.
40.     Explain the pre increment and post increment operators’ in’c’ with examples.
41.     (1) Explain increment and decrement operators in C language with examples. (Or) illustrate the usage of increment and decrement operators in detail. (2) Differentiate between pre increment/decrement operators and post increment/decrement operators in’c.

Ans:increment and decrement operators
42.     (1) State the importance of (a) increment and decrement operators (b) conditional operators.
43.     List the various type conversion techniques used in C and explain them in brief.
44.     Distinguish between getch (), and getchar () functions with suitable examples.
45.     Explain the following (1) increment and decrement operators (2) assignment operators (3) arithmetic operators (4) relational operators.
46.     What is conditional (ternary) operator? Explain with an example.
47.     precedence of operators in C
Ans:
48.     Explain the importance of a C Preprocessor Directives
49.     Explain the pre processor features(1)#include(2)#define
50.     Describe the use of the conditional operator to form conditional expressions.
51.     (1) Explain the functions printf() and scanf(). (Or) explain the syntax of scanf() () and printf() functions. (2) Explain the role of scanf() and printf() functions.
52.     How comments can be included within a C program?
53.     Why is the main () function special in a C program?
54.     List any four standard headers that can be included in c.
55.     Explain the Structure of a C Program in detail.
56.     What is a null statement in C language?
57.     Explain the null statement and coma operator.
58.     Which of the following are invalid variable names and why?
(1) Profit                                 (14) counter-3
(2) Last name                          (15) annual report
(3) 2002INCOME  (16) TOTAL-REVENUE
(4) dist.                                    (17) engg.college
(5) Float                  (18) last-named
(6) #tax                                    (19) -count
(7) Stud add                            (20) $balance
(8) 2nd_strret                            (21) new-word
(9) 123
(10) Short
(11) Hyderabad
(12) Count
(13) Int_rate

59.     Find errors, if any, in the following declaration statements
Int fact
Float=x, y,z;
Long int, a;
Char ch;d;
Large area;
Long float root1,root2;
60.     Convert the following equations into corresponding C statements

                                                   
Ans:
      (v):     -b+sqrt b*b-4*a*c/2*a
                -b-sqrt b*b-4*a*c/2*a

61.     Evaluate the expression (A/B-C+A%B) for A=20, B=4, C=2 by following operator hierarchy.
62.     Write a program to ready any two floating-point numbers from the keyboard and to display their addition, subtraction, multiplication and division.
63.     (i) Write a C program to accept five real numbers from the keyboard and to find out their sum and average. (ii) Write a C program to calculate average of three numbers.
64.     Write a C program to calculate the total marks and the percentage marks obtained by the student in five subjects. Assume that the maximum marks that can be obtained by the student in each subject is100.
65.     Write a program to calculate the perimeter and area of a rectangle.
66.     Write program that calculate area of the triangle given the base and the height.
67.     Two numbers are input through the keyboard into two locations Cand D .write a program to interchange the contents of C and D.
68.     Write a c program to convert temperature degrees into centigrade to degrees Fahrenheit.
69.     Write a program that calculates the volume and surface area of the sphere, given the radius.
70.     Write a C program to read an integer number of days and convert it into months and days.
71.     Write a program to calculate the simple interest and compound interest for a given principle (p), rate of interest(r), time (t) and compounding per year (n) using the formulas



72.     Write a program that demonstrate the difference between predecrementing and post decrementing using the decrement operator.
73.     Write a C program to find the largest of two numbers using conditional operator.
74.     Write a C program  to print the following.
        *
      ***
    *****
  *******
*********
75.     Write a program to print the word “C PROGRAM” in the following manner.
                                          C
                                          CP
                                          CPR
                                          CPRO
                                          CPROG
                                          CPROGR
                                          CPROGRA
                                          CPROGRAM
--------------------------------------------------------------------------------------------------------