GE6151 Computer Programming THE CONDITIONAL EXPRESSION OPERATOR or TERNARY OPERATOR

THE CONDITIONAL EXPRESSION OPERATOR or TERNARY OPERATOR

This conditional expression operator takes THREE operators. The two symbols used to denote this operator are the ? and the :. The first operand is placed before the ?, the second operand between the ? and the :, and the third after the :. The general format is,

condition ? expression1 : expression2

If the result of condition is TRUE ( non-zero ), expression1 is evaluated and the result of the evaluation becomes the result of the operation. If the condition is FALSE (zero), then expression2 is evaluated and its result becomes the result of the operation. An example will help,

s = ( x < 0 ) ? -1 : x * x;

If x is less than zero then s = -1

If x is greater than zero then s = x * x

Example:

#include <stdio.h>

main()

{

int input;

printf("I will tell you if the number is positive, negative or zero!"\n");

printf("please enter your number now--->");

scanf("%d", &input );

(input < 0) ? printf("negative\n") : ((input > 0) ? printf("positive\n") : printf("zero\n"));

}

BIT OPERATIONS

C has the advantage of direct bit manipulation and the operations available are,

AND

&

sum = sum & 2;

4

0

OR

|

sum = sum | 2;

4

6

Exclusive OR

^

sum = sum ^ 2;

4

6

1's Complement

~

sum = ~sum;

4

-5

Left Shift

<<

sum = sum << 2;

4

16

Operation Operator Comment Value of Sum before Value of sum after

Right Shift >> sum = sum >> 2; 4 0

Example:

/* Example program illustrating << and >> */

#include <stdio.h>

main()

{

int n1 = 10, n2 = 20, i = 0;

i = n2 << 4; /* n2 shifted left four times */

printf("%d\n", i);

i = n1 >> 5; /* n1 shifted right five times */

printf("%d\n", i);

}

Example:

/* Example program using EOR operator */

#include <stdio.h>

main()

{

int value1 = 2, value2 = 4;

value1 ^= value2; value2 ^= value1; value1 ^= value2;

printf("Value1 = %d, Value2 = %d\n", value1, value2);

}

Example:

/* Example program using AND operator */

#include <stdio.h>

main()

{

int loop;

for( loop = 'A'; loop <= 'Z'; loop++ )

printf("Loop = %c, AND 0xdf = %c\n", loop, loop & 0xdf);

}

MANAGING INPUT AND OUTPUT OPERATORS

Printf ():

printf() is actually a function (procedure) in C that is used for printing variables and text. Where

text appears in double quotes "", it is printed without modification. There are some exceptions however. This has to do with the \ and % characters. These characters are modifier's, and for the present the \ followed by the n character represents a newline character.

Example:

#include <stdio.h>

main()

{

printf("Programming in C is easy.\n");

printf("And so is Pascal.\n");

}

@ Programming in C is easy.

And so is Pascal. FORMATTERS for printf are,

Cursor Control Formatters

\n newline

\t tab

\r carriage return

\f form feed

\v vertical tab

Scanf ():

Scanf () is a function in C which allows the programmer to accept input from a keyboard.

Example:

#include <stdio.h>

main() /* program which introduces keyboard input */

{

int number;

printf("Type in a number \n");

scanf("%d", &number);

printf("The number you typed was %d\n", number);

}

FORMATTERS FOR scanf()

The following characters, after the % character, in a scanf argument, have the following effect. d read a decimal integer

o read an octal value

x read a hexadecimal value h read a short integer

l read a long integer f read a float value

e read a double value

c read a single character

s read a sequence of characters

[...] Read a character string. The characters inside the brackets

Accepting Single Characters From The Keyboard

Getchar, Putchar

getchar() gets a single character from the keyboard, and putchar() writes a single character

from the keyboard. Example:

The following program illustrates this,

#include <stdio.h>

main()

{

int i;

int ch;

for( i = 1; i<= 5; ++i )

{

ch = getchar(); putchar(ch);

}

}

The program reads five characters (one for each iteration of the for loop) from the keyboard. Note that getchar() gets a single character from the keyboard, and putchar() writes a single character (in this case, ch) to the console screen.

GE6151 Computer Programming HEADER FILES

HEADER FILES

Header files contain definitions of functions and variables which can be incorporated into any C program by using the pre-processor #include statement. Standard header files are provided with each compiler, and cover a range of areas, string handling, mathematical, data conversion, printing and reading of variables.

To use any of the standard functions, the appropriate header file should be included. This is done at the beginning of the C source file. For example, to use the function printf() in a program, the line

#include <stdio.h>

should be at the beginning of the source file, because the definition for printf() is found in the file stdio.h All header files have the extension .h and generally reside in the /include subdirectory.

#include <stdio.h>

#include "mydecls.h"

The use of angle brackets <> informs the compiler to search the compilers include directory for the specified file. The use of the double quotes "" around the filename inform the compiler to search in the current directory for the specified file.

OPERATORS AND EXPRESSIONS

An ex pr e s s i o n is a sequence of operators and operands that specifies computation of a value, or that designates an object or a function, or that generates side effects, or that performs a combination thereof.

ARITHMETIC OPERATORS:

The symbols of the arithmetic operators are:-

Multiply

*

sum = sum * 2;

4

8

Divide

/

sum = sum / 2;

4

2

Addition

+

sum = sum + 2;

4

6

Subtraction

-

sum = sum -2;

4

2

Increment

++

++sum;

4

5

Decrement

--

--sum;

4

3

Modulus

%

sum = sum % 3;

4

1

Operation Operator Comment Value of Sum before Value of sum after

Example:

#include <stdio.h>

main()

{

int sum = 50; float modulus;

modulus = sum % 10;

printf("The %% of %d by 10 is %f\n", sum, modulus);

}

PRE/POST INCREMENT/DECREMENT OPERATORS

PRE means do the operation first followed by any assignment operation. POST means do the operation after any assignment operation. Consider the following statements

++count; /* PRE Increment, means add one to count */

count++; /* POST Increment, means add one to count */

Example:

#include <stdio.h>

main()

{

int count = 0, loop;

loop = ++count; /* same as count = count + 1; loop = count; */

printf("loop = %d, count = %d\n", loop, count);

loop = count++; /* same as loop = count; count = count + 1; */

printf("loop = %d, count = %d\n", loop, count);

}

If the operator precedes (is on the left hand side) of the variable, the operation is performed first, so the statement loop = ++count;

really means increment count first, then assign the new value of count to loop.

THE RELATIONAL OPERATORS

These allow the comparison of two or more variables.

== equal to

!= not equal

< less than

<= less than or equal to

> greater than

>= greater than or equal to

Example:

#include <stdio.h>

main() /* Program introduces the for statement, counts to ten */

{

int count;

for( count = 1; count <= 10; count = count + 1 )

printf("%d ", count );

printf("\n");

}

RELATIONALS (AND, NOT, OR, EOR)

Combining more than one condition

These allow the testing of more than one condition as part of selection statements. The symbols are

LOGICAL AND &&

Logical and requires all conditions to evaluate as TRUE (non-zero).

LOGICAL OR ||

Logical or will be executed if any ONE of the conditions is TRUE (non-zero).

LOGICAL NOT !

logical not negates (changes from TRUE to FALSE, vsvs) a condition.

LOGICAL EOR ^

Logical eor will be excuted if either condition is TRUE, but NOT if they are all true.

Example:

The following program uses an if statement with logical AND to validate the users input to be in the range 1-10.

#include <stdio.h>

main()

{

int number;

int valid = 0;

while( valid == 0 )

{

printf("Enter a number between 1 and 10 -->"); scanf("%d", &number);

if( (number < 1 ) || (number > 10) )

{

printf("Number is outside range 1-10. Please re-enter\n"); valid = 0;

}

else

valid = 1;

}

printf("The number is %d\n", number );

}

Example: NEGATION

#include <stdio.h>

main()

{

int flag = 0;

if( ! flag )

{

printf("The flag is not set.\n");

flag = ! flag;

}

printf("The value of flag is %d\n", flag);

}

Example:

Consider where a value is to be inputted from the user, and checked for validity to be within a certain range, lets say between the integer values 1 and 100.

#include <stdio.h>

main()

{

int number;

int valid = 0;

while( valid == 0 ) {

printf("Enter a number between 1 and 100"); scanf("%d", &number );

if( (number < 1) || (number > 100) )

printf("Number is outside legal range\n");

else

valid = 1;

}

 

printf("Number is %d\n", number );

}

 

GE6151 Computer Programming DATA TYPES

DATA TYPES

The four basic data types are

INTEGER

These are whole numbers, both positive and negative. Unsigned integers (positive values only) are supported. In addition, there are short and long integers.

The keyword used to define integers is, int

An example of an integer value is 32. An example of declaring an integer variable called sum is, int sum;

sum = 20;

FLOATING POINT

These are numbers which contain fractional parts, both positive and negative. The keyword used to define float variables is,

float

An example of a float value is 34.12. An example of declaring a float variable called money is,

float money;

money = 0.12;

DOUBLE

These are exponetional numbers, both positive and negative. The keyword used to define double variables

is,

double

An example of a double value is 3.0E2. An example of declaring a double variable called big is, double big;

big = 312E+7;

CHARACTER

These are single characters. The keyword used to define character variables is, char

An example of a character value is the letter A. An example of declaring a character variable called letter

is,

Char letter;

letter = 'A';

Sample program illustrating each data type

Example:

#include < stdio.h >

main()

{

int sum; float money; char letter; double pi;

sum = 10; /* assign integer value */

money = 2.21; /* assign float value */ letter = 'A'; /* assign character value */ pi = 2.01E6; /* assign a double value */ printf("value of sum = %d\n", sum );

printf("value of money = %f\n", money ); printf("value of letter = %c\n", letter ); printf("value of pi = %e\n", pi );

}

Sample program output value of sum = 10

value of money = 2.210000 value of letter = A

value of pi = 2.010000e+06

INITIALISING DATA VARIABLES AT DECLARATION TIME

In C variables may be initialised with a value when they are declared. Consider the following declaration, which declares an integer variable count which is initialised to 10.

int count = 10;

SIMPLE ASSIGNMENT OF VALUES TO VARIABLES

The = operator is used to assign values to data variables. Consider the following statement, which assigns the value 32 an integer variable count, and the letter A to the character variable letter

count = 32;

letter = 'A'

Variable Formatters

%d decimal integer

%c character

%s string or character array

%f float

%e double

GE6151 Computer Programming VARIABLES

VARIABLES

User defined variables must be declared before they can be used in a program. Variables must begin with a character or underscore, and may be followed by any combination of characters, underscores, or the digits 0 – 9

LOCAL AND GLOBAL VARIABLES Local

These variables only exist inside the specific function that creates them. They are unknown to other

functions and to the main program. As such, they are normally implemented using a stack. Local variables cease to exist once the function that created them is completed. They are recreated each time a function is executed or called.

Global

These variables can be accessed (ie known) by any function comprising the program. They are

implemented by associating memory locations with variable names. They do not get recreated if the function is recalled.

Defining Global Variables

/* Demonstrating Global variables */

Example:

#include <stdio.h>

int add_numbers( void ); /* ANSI function prototype */

/* These are global variables and can be accessed by functions from this point on */

int value1, value2, value3;

int add_numbers( void )

{

auto int result;

result = value1 + value2 + value3;

return result;

}

main()

{

auto int result;

result = add_numbers();

printf("The sum of %d + %d + %d is %d\n", value1, value2, value3, final_result);

}

The scope of global variables can be restricted by carefully placing the declaration. They are visible from the declaration until the end of the current source file.

Example:

#include <stdio.h>

void no_access( void ); /* ANSI function prototype */ void all_access( void );

static int n2;/* n2 is known from this point onwards */

void no_access( void )

{

n1 = 10; /* illegal, n1 not yet known */

n2 = 5; /* valid */

}

static int n1; /* n1 is known from this point onwards */

void all_access( void )

{

n1 = 10; /* valid */

n2 = 3; /* valid */

}

AUTOMATIC AND STATIC VARIABLES

C programs have a number of segments (or areas) where data is located. These segments are

typically, _DATA Static data _BSS Uninitialized static data, zeroed out before call to main()

_STACK Automatic data, resides on stack frame, thus local to functions _CONST Constant data, using the ANSI C keyword const

The use of the appropriate keyword allows correct placement of the variable onto the desired data segment.

Example:

/* example program illustrates difference between static and automatic variables */

#include <stdio.h>

void demo( void ); /* ANSI function prototypes */

void demo( void )

{

auto int avar = 0; static int svar = 0;

printf("auto = %d, static = %d\n", avar, svar);

++avar; ++svar;

}

main()

{

int i;

while( i < 3 ) {

demo();

i++;

}

}

Automatic and Static Variables

Example:

/* example program illustrates difference between static and automatic variables */

#include <stdio.h>

void demo( void ); /* ANSI function prototypes */

void demo( void ) {

auto int avar = 0; static int svar = 0;

printf("auto = %d, static = %d\n", avar, svar);

++avar; ++svar;

}

main()

{

int i;

while( i < 3 ) {

demo();

i++;

}

}

Program output

auto

=

0,

static

=

0

auto

auto = 0, static = 2

=

0,

static

=

1

The basic format for declaring variables is

data_type var, var, ... ;

where data_type is one of the four basic types, an integer, character, float, or double type.

Static variables are created and initialized once, on the first call to the function. Subsequent calls to the function do not recreate or re-initialize the static variable. When the function terminates, the variable still exists on the _DATA segment, but cannot be accessed by outside functions. Automatic variables are the opposite. They are created and re-initialized on each entry to the function. They disappear (are de-allocated) when the function terminates. They are created on the

_STACK segment.

GE6151 Computer Programming CONSTANTS

CONSTANTS

A constant is an entity that doesn’t change whereas a variable is an entity that may change.

Types of C Constants

C constants can be divided into two major categories: (a) Primary Constants

(b) Secondary Constants

Rules for Constructing Integer Constants

(a) An integer constant must have at least one digit. (b) It must not have a decimal point.

(c) It can be either positive or negative.

(d) If no sign precedes an integer constant it is assumed to be positive. (e) No commas or blanks are allowed within an integer constant.

(f) The allowable range for integer constants is -32768 to 32767.

Truly speaking the range of an Integer constant depends upon the compiler. For a 16-bit compiler like Turbo C or Turbo C++ the range is –32768 to 32767. For a 32-bit compiler the range would be even greater. Question like what exactly do you mean by a 16-bit or a 32-bit compiler, what range of an Integer constant has to do with the type of compiler and such questions are discussed in detail in Chapter 16. Till that time it would be assumed that we are working with a 16-bit compiler.

Ex.: 426

+782

-8000

-7605

Rules for Constructing Real Constants

Real constants are often called Floating Point constants. The real constants could be written in two forms—Fractional form and Exponential form.

Following rules must be observed while constructing real constants expressed in fractional form: (a)A real constant must have at least one digit.

(b)It must have a decimal point.

(c)It could be either positive or negative. (d)Default sign is positive.

(e)No commas or blanks are allowed within a real constant. Ex.: +325.34

426.0

-32.76

-48.5792

The exponential form of representation of real constants is usually used if the value of the constant is either too small or too large. It however doesn’t restrict us in any way from using exponential form of representation for other real constants.

Rules for Constructing Character Constants

A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas. Both the inverted commas should point to the left. For example, ’A’ is a valid character constant whereas ‘A’ is not.

The maximum length of a character constant can be 1 character. Ex.: 'A'

'I'

'5'

'='

GE6151 Computer Programming BASIC STRUCTURE OF C PROGRAMS

BASIC STRUCTURE OF C PROGRAMS

C programs are essentially constructed in the following manner, as a number of well defined sections.

/* HEADER SECTION */

/* Contains name, author, revision number*/

/* INCLUDE SECTION */

/* contains #include statements */

/* CONSTANTS AND TYPES SECTION */

/* contains types and #defines

*/

/* GLOBAL VARIABLES SECTION */

/* any global variables declared here

*/

/* FUNCTIONS SECTION */

/* user defined functions

*/

/* main() SECTION */

intmain()

{

}

GE6151 Computer Programming C PROGRAMMING BASICS

C PROGRAMMING BASICS

Problem formulation – Problem Solving - Introduction to ‘ C’ programming –fundamentals – structureof a ‘C’ program – compilation and linking processes – Constants, Variables – Data Types –Expressions using operators in ‘C’ – Managing Input and Output operations – Decision Making and Branching – Looping statements – solving simple scientific and statistical problems.

INTRODUCTION TO C

As a programming language, C is rather like Pascal or Fortran.. Values are stored in variables. Programs are structured by defining and calling functions. Program flow is controlled using loops, if statements and function calls. Input and output can be directed to the terminal or to files. Related data can be stored together in arrays or structures.

Of the three languages, C allows the most precise control of input and output. C is also rather more terse than Fortran or Pascal. This can result in short efficient programs, where the programmer has made wise use of C's range of powerful operators. It also allows the programmer to produce programs which are impossible to understand. Programmers who are familiar with the use of pointers (or indirect addressing, to use the correct term) will welcome the ease of use compared with some other languages. Undisciplined use of pointers can lead to errors which are very hard to trace. This course only deals with the simplest applications of pointers.

A Simple Program

The following program is written in the C programming language.

#include <stdio.h>

main()

{

printf("Programming in C is easy.\n");

}

GE6151 Computer Programming Flowchart Types

TYPES:

High-Level Flowchart

A high-level (also called first-level or top-down) flowchart shows the major steps in a process. It illustrates a "birds-eye view" of a process, such as the example in the figure entitled High-Level Flowchart of Prenatal Care. It can also include the intermediate outputs of each step (the product or service produced), and the sub-steps involved. Such a flowchart offers a basic picture of the process and identifies the changes taking place within the process. It is significantly useful for identifying appropriate team members (those who are involved in the process) and for developing indicators for monitoring the process because of its focus on intermediate outputs.

Most processes can be adequately portrayed in four or five boxes that represent the major steps or activities of the process. In fact, it is a good idea to use only a few boxes, because doing so forces one to consider the most important steps. Other steps are usually sub-steps of the more important ones.

Detailed Flowchart

The detailed flowchart provides a detailed picture of a process by mapping all of the steps and activities that occur in the process. This type of flowchart indicates the steps or activities of a process and includes such things as decision points, waiting periods, tasks that frequently must be redone (rework), and feedback loops. This type of flowchart is useful for examining areas of the process in detail and for looking for problems or areas of inefficiency. For example, the Detailed Flowchart of Patient Registration reveals the delays that result when the record clerk and clinical officer are not available to assist clients.

Deployment or Matrix Flowchart

A deployment flowchart maps out the process in terms of who is doing the steps. It is in the form of a matrix, showing the various participants and the flow of steps among these participants. It is chiefly useful in identifying who is providing inputs or services to whom, as well as areas where different people may be needlessly doing the same task. See the Deployment of Matrix Flowchart.

ADVANTAGES OF USING FLOWCHARTS

The benefits of flowcharts are as follows:

1. Communication: Flowcharts are better way of communicating the logic of a system to all concerned.

2. Effective analysis: With the help of flowchart, problem can be analysed in more effective way.

3. Proper documentation: Program flowcharts serve as a good program documentation, which is needed for various purposes.

4. Efficient Coding: The flowcharts act as a guide or blueprint during the systems analysis and program development phase.

5. Proper Debugging: The flowchart helps in debugging process.

6. Efficient Program Maintenance: The maintenance of operating program becomes easy with the help of flowchart. It helps the programmer to put efforts more efficiently on that part

Advantages:

· Logic Flowcharts are easy to understand.They provide a graphical representation of actions to be taken.

· Logic Flowcharts are well suited for representing logic where there is intermingling among many actions.

Disadvantages:

· Logic Flowcharts may encourage the use of GoTo statements leadingsoftware design that is unstructured with logic that is difficult to decipher.

· Without an automated tool, it is time-consuming to maintain Logic Flowcharts.

· Logic Flowcharts may be used during detailed logic design to specify a module.

· However, the presence of decision boxes may encourage the use of GoTo statements, resulting in software that is not structured. For this reason, Logic Flowcharts may be better used during Structural Design

LIMITATIONS OF USING FLOWCHARTS

1. Complex logic: Sometimes, the program logic is quite complicated. In that case, flowchart becomes complex and clumsy.

2. Alterations and Modifications: If alterations are required the flowchart may require re- drawing completely.

3. Reproduction: As the flowchart symbols cannot be typed, reproduction of flowchart becomes a problem.

4. The essentials of what is done can easily be lost in the technical details of how it is done.

GUIDELINES FOR DRAWING A FLOWCHART

Flowcharts are usually drawn using some standard symbols; however, some special symbols can also be developed when required. Some standard symbols, which are frequently required for flowcharting many computer programs.

clip_image001Start or end of the program

clip_image002Computational steps or processing function of a program

clip_image003Input or output operation

clip_image004Decision making and branching

clip_image005Connector or joining of two parts of program

clip_image006Magnetic Tape

clip_image007Magnetic Disk

clip_image008Off-page connector

¬ ® ­ ¯ Flow line

clip_image009clip_image010clip_image011Annotation

clip_image012Display

The following are some guidelines in flowcharting:

(a) In drawing a proper flowchart, all necessary requirements should be listed out in logical order.

(b) The flowchart should be clear, neat and easy to follow. There should not be any room for ambiguity in understanding the flowchart.

(c) The usual direction of the flow of a procedure or system is from left to right or top to bottom.

(d) Only one flow line should come out from a process symbol.

clip_image013¯

clip_image014® ® or

¯

(e) Only one flow line should enter a decision symbol, but two or three flow lines, one for each possible answer, should leave the decision symbol.

clip_image015clip_image016clip_image017clip_image018Yes < 0 > 0

clip_image019clip_image020clip_image021No = 0

clip_image022clip_image023clip_image024(f) Only one flow line is used in conjunction with terminal symbol.

(g) Write within standard symbols briefly. As necessary, you can use the annotation symbol to describe data or computational steps more clearly.

clip_image025clip_image026This is top secret data

(h) If the flowchart becomes complex, it is better to use connector symbols to reduce the number of flow lines. Avoid the intersection of flow lines if you want to make it more effective and better way of communication.

(i) Ensure that the flowchart has a logical start and finish.

(j) It is useful to test the validity of the flowchart by passing through it with a simple test data.

Examples

Sample flowchart

A flowchart for computing factorial N (N!) Where N! = 1 * 2 * 3 *...* N. This flowchart represents a "loop and a half" — a situation discussed in introductory programming textbooks that requires either a duplication of a component (to be both inside and outside the loop) or the component to be put inside a branch in the loop

Sample Pseudocode

ALGORITHM Sample

GET Data

WHILE There Is Data

DO Math Operation

GET Data

END WHILE

END ALGORITHM

clip_image028

GE6151 Computer programming OCTAL NUMBERS

OCTAL NUMBERS

Like the hexadecimal system, the octal system provides a convenient way to express binary numbers and codes. However, it is used less frequently than hexadecimal in conjunction with computers and microprocessors to express binary quantities for input and output purposes.

The octal system is composed of eight digits, which are: 0, 1, 2, 3, 4, 5, 6, 7

To count above 7, begin another column and start over: 10, 11, 12, 13, 14, 15, 16, 17, 20, 21 and so on. Counting in octal is similar to counting in decimal, except that the digits 8 and 9 are not used.

OCTAL-TO-DECIMAL CONVERSION

Since the octal number system has a base of eight, each successive digit position is an increasing power of eight, beginning in the right-most column with 8º. The evaluation of an octal number in terms of its decimal equivalent is accomplished by multiplying each digit by its weight and summing the products.

Let‘s convert octal number 2374 in decimal number.

Weight 8³ 8² 81 80

Octal number 2 3 7 4

2374 = (2 x 8³) + (3 x 8²) + (7 x 81) + (4 x 8º) =1276

DECIMAL-TO-OCTAL CONVERSION

A method of converting a decimal number to an octal number is the repeated division-by-8

method, which is similar to the method used in the conversion of decimal numbers to binary or to hexadecimal.

Let‘s convert the decimal number 359 to octal. Each successive division by 8 yields a remainder that becomes a digit in the equivalent octal number. The first remainder generated is the least significant digit (LSD).

359/8 = 44.875 0.875 x 8 = 7 (LSD)

44 /8 = 5.5 0.5 x 8 = 4

5/8= 0.625 0.625 x 8 = 5 (MSD) The number is 547.

OCTAL-TO-BINARY CONVERSION

Because each octal digit can be represented by a 3-bit binary number, it is very easy to convert from octal to binary.

Octal/Binary Conversion

Octal Digit

0 1 2

3

4 5 6

7

Binary

000 001 010

011

100 101 110

111

Let‘s convert the octal numbers 25 and 140.

Octal Digit

2

5

1 4 0

Binary

010

101

001 100 000

BINARY-TO-OCTAL CONVERSION

Conversion of a binary number to an octal number is the reverse of the octal-to-binary conversion.

Let‘s convert the following binary numbers to octal:

1 1 0 1 0 1 1 0 1 1 1 1 0 0 1

6 5 = 65 5 7 1 = 571

ALGORITHM

Algorithm

· Set of step-by-step instructions that perform a specific task or operation

· ―Natural‖ language NOT programming language

Pseudocode

· Set of instructions that mimic programming language instructions

Flowchart

· Visual program design tool

· ―Semantic‖ symbols describe operations to be performed

FLOWCHARTS

Definitions:

A flowchart is a schematic representation of an algorithm or a stepwise process, showing the steps

as boxes of various kinds, and their order by connecting these with arrows. Flowcharts are used in designing or documenting a process or program.

A flow chart, or flow diagram, is a graphical representation of a process or system that details the sequencing of steps required to create output.

A flowchart is a picture of the separate steps of a process in sequential order.

GE6151 Computer Programming HEXADECIMAL NUMBERS

HEXADECIMAL NUMBERS

The hexadecimal number system has sixteen digits and is used primarily as a compact way of displaying or writing binary numbers because it is very easy to convert between binary and hexadecimal.Long binary numbers are difficult to read and write because it is easy to drop or transpose a bit. Hexadecimal is widely used in computer and microprocessor applications. The hexadecimal system has a base of sixteen; it is composed of 16 digits and alphabetic characters. The maximum 3-digits hexadecimal number is FFF or decimal 4095 and maximum 4-digit hexadecimal number is FFFF or decimal 65.535.

BINARY-TO-HEXADECIMAL CONVERSION

Simply break the binary number into 4-bit groups, starting at the right-most bit and replace each 4-bit group with the equivalent hexadecimal symbol as in the following example

Convert the binary number to hexadecimal: 1100101001010111

Solution:

1100 1010 0101 0111

C A 5 7 = CA57

HEXADECIMAL-TO-DECIMAL CONVERSION

One way to find the decimal equivalent of a hexadecimal number is to first convert the

hexadecimal number to binary and then convert from binary to decimal.

Convert the hexadecimal number 1C to decimal:

1 C

0001 1100= 24+ 2³ + 2² = 16 +8+4 = 28

DECIMAL-TO-HEXADECIMAL CONVERSION

Repeated division of a decimal number by 16 will produce the equivalent hexadecimal number,

formed by the remainders of the divisions. The first remainder produced is the least significant digit (LSD). Each successive division by 16 yields a remainder that becomes a digit in the equivalent hexadecimal number. When a quotient has a fractional part, the fractional part is multiplied by the divisor to get the remainder.

Convert the decimal number 650 to hexadecimal by repeated division by 16

40 /16= 2.5

0.5 x 16 = 8 =

8

2/16= 0.125

0.125 x 16 = 2 =

2 (MSD)

The hexadecimal number is 28A

   
650 /16 = 40.625