rev='made'/> name='keywords'/> name='author'/> ANURANAN: Reading Input ,identifier, if statement, Arithmetic Operation

Welcome Note

I'm Anisuzzaman Babla . I am a full stack Android Application Developer. I am working with Android for more than two years. I have completed my graduation from IIT, Jahangirnagar University. I have developed a series of commercially successful apps on Android. Moreover, 10+ of my Apps are live at Google play store. .
Thank You

Reading Input ,identifier, if statement, Arithmetic Operation

Programming Fundamentals
Ø                                  Reading Input
Ø                                  identifier,
Ø                                  if statement,
Ø                                  Arithmetic Operation
Reading Input
scanf is the C library’s counterpart to printf.
scanf requires a format string to specify the appearance of the input data.
Example of using scanf to read an int value:
                scanf("%d", &i);
                /* reads an integer; stores into i */
The & symbol is usually (but not always) required when using scanf.
Reading Input
Reading a float value requires a slightly different call of scanf:
                scanf("%f", &x);
"%f" tells scanf to look for an input value in float format (the number may contain a decimal point, but doesn’t have to).
Computing  volume of a Box
dweight.c is a program in which the user enters the dimensions.
Each call of scanf is immediately preceded by a call of printf that displays a prompt.
Computing  volume of a Box
/* Computes the dimensional weight of a box from input provided by the user */
#include <stdio.h>
int main()
  int height, length, width, volume;
  printf("Enter height of box: ");
  scanf("%d", &height);
  printf("Enter length of box: ");
  scanf("%d", &length);
  printf("Enter width of box: ");
  scanf("%d", &width);
  volume = height * length * width;
  printf("Volume (cubic inches): %d\n", volume);
  return 0;
Sample output of program:
                Enter height of box: 8
                Enter length of box: 12
                Enter width of box: 10
                Volume (cubic inches): 960
Note that a prompt shouldn’t end with a new-line character.
Return value of scanf function:
int a, b, c;
printf(“Enter 2 Numbers:”);
c=scanf(“%d %d”,&a, &b);
printf(“returned value of scanf = %d\n”,c);
Sample Input:
Enter 2 Numbers: 10 20                 Output: ??
Enter 2 Numbers: 20 A                                   Output: ??
Enter 2 Numbers: A 20                                   Output: ??
Enter 2 Numbers: A B                                     Output: ??
¨  Names for variables, functions, macros, and other entities are called identifiers.
¨  An identifier may contain
¤  letters,
¤  digits, and
¤  underscores,
¤  but must begin with a letter or underscore:
                times10  get_next_char  _done
                It’s usually best to avoid identifiers that begin with an underscore.
¨  Examples of illegal identifiers:
                10times  get-next-char
C is case-sensitive: it distinguishes between upper-case and lower-case letters in identifiers.
For example, the following identifiers are all different:
                job  joB  jOb  jOB  Job  JoB  JOb  JOB
Many programmers use only lower-case letters in identifiers (other than macros), with underscores inserted for legibility:
                symbol_table  current_page  name_and_address
Other programmers use an upper-case letter to begin each word within an identifier:
                symbolTable  currentPage  nameAndAddress
C places no limit on the maximum length of an identifier.
Conditional Statement
The if Selection Statement
¨  Selection structure:
¤  Used to choose among alternative courses of action
¤  Pseudocode:
If your grade is greater than or equal to 60
Print “Passed”
¨  Pseudocode statement in C:
                if ( grade >= 60 )
   printf( "Passed\n" );
¤  C code corresponds closely to the pseudocode
¨  Diamond symbol (decision symbol)
¤  Indicates decision is to be made
¤  Contains an expression that can be true or false
¤  Test the condition, follow appropriate path
The if Selection Flowchart
if statement is a single-entry/single-exit structure
The if…else Selection Statement
¨  if
¤  Only performs an action if the condition is true
¨  if…else
¤  Specifies an action to be performed both when the condition is true and when it is false
¨  Psuedocode:
If student’s grade is greater than or equal to 60
Print “Passed”
Print “Failed”
¤  Note spacing/indentation conventions
The if…else Selection Statement
Flowchart of the if…else selection statement
The if…else Selection Statement
C code:
if ( grade >= 60 )
   printf( "Passed\n");
   printf( "Failed\n");
Compound Statements
In the if statement template, notice that statement is singular, not plural:
                if ( expression ) statement
To make an if statement control two or more statements, use a compound statement.
A compound statement has the form
                { statements }
Putting braces around a group of statements forces the compiler to treat it as a single statement.
Compound Statements
                { line_num = 0; page_num++; }
A compound statement is usually put on multiple lines, with one statement per line:
                  line_num = 0;
Each inner statement still ends with a semicolon, but the compound statement itself does not.
Compound Statements
Example of a compound statement used inside an if statement:
                if (line_num == 15) {
                  line_num = 0;
Compound statements are also common in loops and other places where the syntax of C requires a single statement.
Relational Operators
C’s relational operators:
                <             less than
                >             greater than
                <=           less than or equal to
                >=           greater than or equal to
These operators produce 0 (false) or 1 (true) when used in expressions.
The relational operators can be used to compare integers and floating-point numbers, with operands of mixed types allowed.
Equality Operators
C provides two equality operators:
                ==           equal to
                !=            not equal to
The equality operators produce either 0 (false) or 1 (true) as their result.

Arithmetic Operators
¨  Addition                               +             sum = num1 + num2;
¨  Subtraction                         -              age = 2007 – my_birth_year;
¨  Multiplication    *             area = side1 * side2;
¨  Division                                /              avg = total / number;
¨  Modulus                              %            lastdigit = num % 10;
¤  Modulus returns remainder of division between two integers
¤  Example 5%2 returns a value of 1
¨  Binary vs. Unary operators
¤  All the above operators are binary (why)
¤  - is an unary operator, e.g., a = -3 * -4
Arithmetic Operators (cont’d)
Note that ‘id = exp‘ means assign the result of exp to id, so
X=X+1 means
first perform X+1 and
Assign the result to X 
Suppose X is 4, and
We execute X=X+1
Integer division vs Real division
Division between two integers results in an integer. 
The result is truncated, not rounded
int A=5/3; à A will have the value of 1
int B=3/6; à B will have the value of 0
To have floating point values:
double A=5.0/3;   à A will have the value of 1.666
double B=3.0/6.0; à B will have the value of 0.5
Precedence of
Arithmetic Operators
Compute the following
Area of trapezoid
area = 1.0/2*base*(height_1+height_2);
Exercise: swap
Write a set of statements that swaps the contents of variables x and y
Exercise: swap
First Attempt
Exercise: swap
temp= x;
Exercise: reverse a number
Suppose you are given a number in the range [100 999]
Write a program to reverse it
For example,
                num is 258
                reverse is 852
Exercise: Arithmetic operations
¨  Show the memory snapshot after the following operations by hand
  int a, b, c=5;
  double x, y;
  a = c * 2.5;
  b = a % c * 2 - 1;
  x = (5 + c) * 2.5;
  y = x – (-3 * a) / 2;
  Write a C program and print out the values of a, b, c, x, y and compare them with the ones that you determined by hand.
Exercise: Arithmetic operations
Show how C will perform the following statements and what will be the final output?
int a =  6, b = -3, c =  2;
c= a - b * (a + c * 2) + a / 2 * b;
printf("Value of c = %d \n", c);
Step-by-step show how C will perform the operations

     c = 6 - -3 * (6 + 2 * 2) + 6 / 2 * -3;
     c = 6 - -3 * (6 + 4) + 3 * -3
     c = 6 - -3 *10 + -9
     c = 6 - -30 + -9
     c = 36 + -9
     c = 27
                                Value of c = 27
Library Functions
Math Functions
Trigonometric Functions
Write an expression to compute velocity using the following equation
Assume that the variables are declared
Write an expression to compute velocity using the following equation
Assume that the variables are declared
Exercise: Compute Volume
Write a program to compute the volume of a cylinder of radius r and height h

No comments:

Post a Comment