Priority of Arthematic Operations in C Language Programming

The previous post of the blog about c programming deals with concept of mathematical conversions.

The priority or precedence in which the operations in an arithmetic statement are performed is called the hierarchy of operations. The hierarchy of commonly used operators is shown in Figure .

General rules :

1 . Within parentheses the same hierarchy as mentioned in Figure 1.11 is operative. Also, if there are more than one set of parentheses, the operations within the innermost parentheses would be performed first, followed by the operations within the second innermost pair and so on.

2 . We must always remember to use pairs of parentheses. A careless imbalance of the right and left parentheses is a common error. Best way to avoid this error is to type ( ) and then type an expression inside it.

Examples :

1 . Determine the hierarchy of operations and evaluate the following expression:

i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8

This can be done step by step as shown.

i = 6 / 4 + 4 / 4 + 8 - 2 + 5 / 8 operation: *
i = 1 + 4 / 4 + 8 - 2 + 5 / 8 operation: /
i = 1 + 1+ 8 - 2 + 5 / 8 operation: /
i = 1 + 1 + 8 - 2 + 0 operation: /
i = 2 + 8 - 2 + 0 operation: +
i = 10 - 2 + 0 operation: +
i = 8 + 0 operation : -
i = 8 operation: +

Note that 6 / 4 gives 1 and not 1.5. This so happens because 6 and 4 both are integers and therefore would evaluate to only an integer constant. Similarly 5 / 8 evaluates to zero, since 5 and 8 are integer constants and hence must return an integer value.

2 . Determine the hierarchy of operations and evaluate the following expression:

k = 3 / 2 * 4 + 3 / 8 + 3

Stepwise evaluation of this expression is shown below:

k = 3 / 2 * 4 + 3 / 8 + 3
k = 1 * 4 + 3 / 8 + 3 operation: /
k = 4 + 3 / 8 + 3 operation: *
k = 4 + 0 + 3 operation: /
k = 4 + 3 operation: +
k = 7 operation: +

All operators in C are ranked according to their precedence.

conversion of a general arithmetic statement to a C statement :

Here is the list of some simple examples which will give a outline about the conversion .


Related posts :

IF STATEMENT

MULTIPLE STATEMENTS IN IF

IF AND ELSE

NESTED IF AND ELSE


BREAK

CONTINUE AND DO WHILE IN C LANGUAGE

SWITCH IN C PROGRAMMING

FUNCTIONS IN C PROGRAMMING


Functions and usage in C part two

Coding in C functions

C Program Conversion of constants

We have learned regarding C Program arithmetic instructions in the previous post.Here we are going to discuss regarding Integer and Float Conversions first.

Rules of implicit conversion of floating point and integer values in C :

1 . An arithmetic operation between an integer and integer always yields an integer result.

2 . An operation between a real and real always yields a real result.

3. An operation between an integer and real always yields a real result. In this operation the integer is first promoted to a real and then the operation is performed. Hence the result is real.

Examples :


Type Conversion in Assignments :


It may so happen that the type of the expression and the type of the variable on the left-hand side of the assignment operator may not be same.In such a case the value of the expression is promoted or demoted depending on the type of the variable on left-hand side of = .

Example 1. :

int i ;
float b ;
i = 3.5 ;
b = 30 ;

Here in the first assignment statement though the expression’s value is a float (3.5) it cannot be stored in i since it is an int. In such a case the float is demoted to an int and then its value is stored. Hence what gets stored in i is 3. Exactly opposite happens in the next statement. Here, 30 is promoted to 30.000000 and then stored in b, since b being a float variable cannot hold anything except a float value.

Example 2. :

float a, b, c ; int s ; s = a * b * c / 100 + 32 / 4 - 3 * 1.1 ;

Here, in the assignment statement some operands are ints whereas others are floats. As we know, during evaluation of the expression the ints would be promoted to floats and the result of the expression would be a float. But when this float value is assigned to s it is again demoted to an int and then stored in s.
Example 3. :

Note that though the following statements give the same result, 0, the results are obtained differently.

k = 2 / 9 ; k = 2.0 / 9 ; In the first statement, since both 2 and 9 are integers, the result is an integer, i.e. 0. This 0 is then assigned to k.

In the second statement 9 is promoted to 9.0 and then the division is performed. Division yields 0.222222.

However, this cannot be stored in k, k being an int. Hence it gets demoted to 0 and then stored in k.
48.5

Related posts :

IF STATEMENT

MULTIPLE STATEMENTS IN IF

IF AND ELSE

NESTED IF AND ELSE


BREAK

CONTINUE AND DO WHILE IN C LANGUAGE

SWITCH IN C PROGRAMMING

FUNCTIONS IN C PROGRAMMING


Functions and usage in C part two

Coding in C functions

c program Arithmetic Instruction

This part of lesson deals with c language programming instructions and its first part is published here.Here is the continuation for that.

A arithmetic statement could be of three types. These are as follows:

1 . Integer mode arithmetic statement This is an arithmetic statement in which all operands are either integer variables or integer constants.

Ex: int i, king, issac, noteit ;
i = i + 1 ;
king = issac * 234 + noteit - 7689 ;

2 . Real mode arithmetic statement This is an arithmetic statement in which all operands are either real constants or real variables.

Ex: float qbee, antink, si, prin, anoy, roi ;
qbee = antink + 23.123 / 4.5 * 0.3442 ;
si = prin * anoy * roi / 100.0 ;

3 . Mixed mode arithmetic statement This is an arithmetic statement in which some of the operands are integers and some of the operands are real.

Ex: float si, prin, anoy, roi, avg ;
int a, b, c, num ;
si = prin * anoy * roi / 100.0 ;
avg = ( a + b + c + num ) / 4 ;

Execution of an arithmetic statement : Firstly, the right hand side is evaluated using constants and the numerical values stored in the variable names. This value is then assigned to the variable on the left-hand side.

Note :

1 . C allows only one variable on left-hand side of =. That is, z = k * l is legal, whereas k * l = z is illegal.

2 . In addition to the division operator C also provides a modular division operator. This operator returns the remainder on dividing one integer with another. Thus the expression 10 / 2 yields 5, whereas, 10 % 2 yields 0. Note that the modulus operator (%) cannot be applied on a float. Also note that on using % the sign of the remainder is always same as the sign of the numerator. Thus –5 % 2 yields –1, whereas, 5 % -2 yields 1.

3 . An arithmetic instruction is often used for storing character constants in character variables.

char a, b, d ; a = 'F' ;
b = 'G' ; d = '+' ;

When we do this the ASCII values of the characters are stored in the variables. ASCII values are used to represent any character in memory. The ASCII values of ‘F’ and ‘G’ are 70 and 71.

4 . Arithmetic operations can be performed on ints, floats and chars.

Thus the statements,

char x, y ;
int z ; x = 'a' ;
y = 'b' ;
z = x + y ;

are perfectly valid, since the addition is performed on the ASCII values of the characters and not on characters themselves. The ASCII values of ‘a’ and ‘b’ are 97 and 98, and hence can definitely be added.

5 . No operator is assumed to be present. It must be written explicitly. In the following example, the multiplication operator after b must be explicitly written.

Ex:

a = c.d.b(xy) is the usual arithmetic statement and
b = c * d * b * ( x * y ) is the statement in C .

6. Unlike other high level languages, there is no operator for performing exponentiation operation.
Thus following statements are invalid.

a = 3 ** 2 ;
b = 3 ^ 2 ;

For exponentiation we shall follow the following process.

#include
main( )
{ int a ;
a = pow ( 3, 2 ) ;
printf ( “%d”, a ) ;
}

Here pow( ) function is a standard library function. It is being used to raise 3 to the power of 2. #include is a preprocessor directive. It is being used here to ensure that the pow( ) function works correctly.
IF STATEMENT

MULTIPLE STATEMENTS IN IF

IF AND ELSE

NESTED IF AND ELSE


BREAK

CONTINUE AND DO WHILE IN C LANGUAGE

SWITCH IN C PROGRAMMING

FUNCTIONS IN C PROGRAMMING


Functions and usage in C part two

Coding in C functions

C Program Instructions

The purpose of instructions is

  1. Type declaration instruction : To declare the type of variables used in a C program.
  2. Arithmetic instruction : To perform arithmetic operations between constants and variables.
  3. Control instruction : To control the sequence of execution of various statements in a C program.
Type Declaration Instruction is used to declare the type of variables being used in the program. Any variable used in the program must be declared before using it in any statement. The type declaration statement is written at the beginning of main( ) function.

Ex.:

int bas ;
float rs, grosssal ;
char name, code ;

There are several subtle variations of the type declaration instruction.

1 . While declaring the type of variable we can also initialize it as shown below.

int i = 10, j = 25 ;
float a = 1.5, b = 1.99 + 2.4 * 1.44 ;

2 . The order in which we define the variables is sometimes important sometimes not.

For example,
int i = 10, j = 25 ;
is same as
int j = 25, j = 10 ;

However,
float a = 1.5, b = a + 3.1 ; is alright,
but float b = a + 3.1, a = 1.5 ; is not. This is because here we are trying to use a even before defining it.

3 . The following statements would work .
int a, b, c, d ; a = b = c = 10 ;
However, the following statement would not work
int a = b = c = d = 10 ; Once again we are trying to use b (to assign to a) before defining it.

Arithmetic Instruction

A arithmetic instruction consists of a variable name on the left hand side of = and variable names & constants on the right hand side of =. The variables and constants appearing on the right hand side of = are connected by arithmetic operators like +, -, *, and /.

Ex :

int ad ;
float kot, deta, alpha, beta, gamma ;
ad = 3200 ; kot = 0.0056 ;
deta = alpha * beta / gamma + 3.2 * 2 / 5 ;

Here, *, /, -, + are the arithmetic operators. = is the assignment operator. 2, 5 and 3200 are integer constants. 3.2 and 0.0056 are real constants. ad is an integer variable. kot, deta, alpha, beta, gamma are real variables.

The variables and constants together are called ‘operands’ that are operated upon by the ‘arithmetic operators’ and the result is assigned, using the assignment operator, to the variable on left-hand side.
IF STATEMENT

MULTIPLE STATEMENTS IN IF

IF AND ELSE

NESTED IF AND ELSE


BREAK

CONTINUE AND DO WHILE IN C LANGUAGE

SWITCH IN C PROGRAMMING

FUNCTIONS IN C PROGRAMMING


Functions and usage in C part two

Coding in C functions

C Program Compilation and execution

The sample program you are compiling and executing here is the one we have discussed in the previous post under the title c programming rules.
Once you have written the program you need to type it and instruct the machine to execute it. To type your C program you need another program called Editor.

Once the program has been typed it needs to be converted to machine language (0s and 1s) before the machine can execute it. To carry out this conversion we need another program called Compiler.

Compiler vendors provide an Integrated Development Environment (IDE) which consists of an Editor as well as the Compiler. There are several such IDEs available in the market targeted towards different operating systems.

For example, Turbo C, Turbo C++ and Microsoft C are some of the popular compilers that work under MS-DOS; Visual C++ and Borland C++ are the compilers that work under Windows, whereas gcc compiler works under Linux. Note that Turbo C++, Microsoft C++ and Borland C++ software also contain a C compiler bundled with them.

If you are a beginner you would be better off using a simple compiler like Turbo C or Turbo C++.
With Turbo C or Turbo C++ compiler here are the steps that you need to follow to compile and execute your first C program…
  1. Select New from the File menu.
  2. Type the program.
  3. Save the program using F2 under a proper name (say Program1.c).
  4. Use Ctrl + F9 to compile and execute the program.
  5. Use Alt + F5 to view the output.
On compiling the program its machine language equivalent is stored as an EXE file (Program1.EXE) on the disk. This file is called an executable file. If we copy this file to another machine we can execute it there without being required to recompile it. In fact the other machine need not even have a compiler to be able to execute the file.

For Turbo C++ compiler, you may get an error — The function printf should have a prototype. To get rid of this error, perform the following steps and then recompile the program.
  1. Select ‘Options’ menu and then select ‘Compiler | C++ Options’. In the dialog box that pops up, select ‘CPP always’ in the ‘Use C++ Compiler’ options.
  2. Again select ‘Options’ menu and then select ‘Environment | Editor’. Make sure that the default extension is ‘C’ rather than ‘CPP’.
To make the program general the program itself should ask the user to supply the values of p, n and r through the keyboard during execution.

This can be achieved using a function called scanf( ).

This function is a counter-part of the printf( ) function.

printf( ) outputs the values to the screen whereas scanf( ) receives them from the keyboard. This is illustrated in the program shown below.

/* Calculation of simple interest */

main( )

{

int p, n ;

float r, si ;

printf ( "Enter values of p, n, r" ) ;

scanf ( "%d %d %f", &p, &n, &r ) ;

si = p * n * r / 100 ;

printf ( "%f" , si ) ;

}

The first printf( ) outputs the message ‘Enter values of p, n, r’ on the screen.
Here we have not used any expression in printf( ) which means that using expressions in printf( ) is optional.

Note that the ampersand (&) before the variables in the scanf( ) function is a must. & is an ‘Address of’ operator. It gives the location number used by the variable in memory. When we say &a, we are telling scanf( ) at which memory location should it store the value supplied by the user from the keyboard.

Note that a blank, a tab or a new line must separate the values supplied to scanf( ). Note that a blank is creating using a spacebar, tab using the Tab key and new line using the Enter key.

IF STATEMENT

MULTIPLE STATEMENTS IN IF

IF AND ELSE

NESTED IF AND ELSE


BREAK

CONTINUE AND DO WHILE IN C LANGUAGE

SWITCH IN C PROGRAMMING

FUNCTIONS IN C PROGRAMMING


Functions and usage in C part two

Coding in C functions

C Programming rules part two

This post is in continuation with programming rules in c and better going through that post first for the sake of completeness.

main( ) is a collective name given to a set of statements. This name has to be main( ), it cannot be anything else. All statements that belong to main( ) are enclosed within a pair of braces { } as shown below.

main( )

{

statement 1 ;

statement 2 ;

statement 3 ;

}

Technically main( ) is a function. Every function has a pair of parentheses ( ) associated with it.

Any variable used in the program must be declared before using it.

For example, int p, n ; float r, si ;

Any C statement always ends with a ;

For example, float r, si ; r = 8.5 ;

In the statement,

si = p * n * r / 100 ;

* and / are the arithmetic operators.

The arithmetic operators available in C are +, -, * and /. C is very rich in operators. There are about 45 operators available in C. Surprisingly there is no operator for exponentiation...

Once the value of si is calculated it needs to be displayed on the screen. Unlike other languages, C does not contain any instruction to display output on the screen. All output to screen is achieved using readymade library functions. One such function is printf( ).

We have used it display on the screen the value contained in si. The general form of printf( ) function is,

printf ( "", ) ;

can contain,

%f for printing real values
%d for printing integer values
%c for printing character values

In addition to format specifiers like %f, %d and %c the format string may also contain any other characters.

These characters are printed as they are when the printf( ) is executed. Following are some examples of usage of printf( ) function:

printf ( "%f", si ) ;
printf ( "%d %d %f %f", p, n, r, si ) ;
printf ( "Simple interest = Rs. %f", si ) ;
printf ( "Prin = %d \nRate = %f", p, r ) ;

The output of the last statement would look like this...

Prin = 1000
Rate = 8.5

Here ‘\n’ is called newline and it takes the cursor to the next line. Therefore, you get the output split over two lines. ‘\n’ is one of the several Escape Sequences available in C.

printf( ) can not only print values of variables, it can also print the result of an expression. An expression is nothing but a valid combination of constants, variables and operators. Thus, 3, 3 + 2, c and a + b * c – d all are valid expressions. The results of these expressions can be printed as shown below:

printf ( "%d %d %d %d", 3, 3 + 2, c, a + b * c – d ) ;

Note that 3 and c also represent valid expressions.

IF STATEMENT

MULTIPLE STATEMENTS IN IF

IF AND ELSE

NESTED IF AND ELSE


BREAK

CONTINUE AND DO WHILE IN C LANGUAGE

SWITCH IN C PROGRAMMING

FUNCTIONS IN C PROGRAMMING


Functions and usage in C part two

Coding in C functions

Test Process

A process can be defined as a set of activities that represent the way work is performed.

The outcome from a process is usually a product or service. Both software development and software testing are processes.

............................................................


There are two ways to visually portray a process. One is the Plan Do Check Act (PDCA) cycle. The other is a workbench. The PDCA cycle is a conceptual view of a process, while the workbench is a more practical illustration of a process.

The PDCA View of a Process:

It has four components. They are :

P – Plan - Devise Your Plan

Define your objective and determine the conditions and methods required to achieve your objective. Describe clearly the goals and policies needed to achieve the objective at this stage. Express a specific objective numerically. Determine the procedures and conditions for the means and methods you will use to achieve the objective.

D – Do (or Execute) the Plan

Create the conditions and perform the necessary teaching and training to execute the plan. Make sure everyone thoroughly understands the objectives and the plan. Teach workers the procedures and skills they need to fulfill the plan and thoroughly understand the job. Then perform the work according to these procedures.
C – Check the Results

Check to determine whether work is progressing according to the plan and whether the expected results are obtained. Check for performance of the set procedures, changes in conditions, or abnormalities that may appear. As often as possible, compare the results of the work with the objectives.

A – Act - Take the Necessary Action

If your checkup reveals that the work is not being performed according to plan or that results are not as anticipated, devise measures for appropriate action. If a check detects an abnormality that is, if the actual value differs from the target value – search for the cause of the abnormality and eliminate the cause. This will prevent the recurrence of the defect. Usually you will need to retrain workers and revise procedures to eliminate the cause of a defect.

The Workbench View of a Process

A process can be viewed as one or more workbenches. Each workbench is built on the following two components:

1• Objective : States why the process exists, or its purpose.

Example: A JAD session is conducted to uncover the majority of customer requirements early and efficiently, and to ensure that all involved parties interpret these requirements consistently.

2• People Skills : The roles, responsibilities, and associated skill sets needed to execute a process. Major roles include suppliers, owners, and customers.

Each workbench has the following components:

1 • Inputs :– The entrance criteria or deliverables needed to perform testing.

2 • Procedures : Describe how work must be done; how methods, tools, techniques, and people are applied to perform a process. There are Do procedures and Check procedures. Procedures indicate the “best way” to meet standards.

Example:

1. Scribe: Use the XYZ tool to enter the requirements. Generate a list of any open issues using the XX template.

2. Leader: Walk through the requirements, paraphrasing each item. Address each open issue when its REF column contains the item being covered.

The previous post is regarding software independentdent testing.
111.2
ShareThis

To Do Next: Thank you for visiting PROGRAMMING BLOG. If you liked the post, please subscribe to my blog via email or RSS FEED.You can contact me here for any specific feed back .

INDEPENDENT SOFTWARE TESTING

The primary responsibility of individuals accountable for testing activities is to ensure that uality is measured accurately.

Often, just knowing that the organization is measuring quality is enough to cause improvements in the applications being developed. In the loosest definition of independence, just having a tester or someone in the organization devoted to test activities is a form of independence.

The roles and reporting structure of test resources differs across and within organizations.

These resources may be business or systems analysts assigned to perform testing activities, or may be testers who report to the project manager. Ideally, the test resources will have a reporting structure independent from the group designing or developing the application in order to assure that the quality of the application is given as much consideration as the project budget and time line.

Misconceptions abound regarding the skill set required to perform testing, including:
  1. • Testing is easy
  2. • Anyone can perform testing
  3. • No training or prior experience is necessary
In truth, to test effectively, an individual must:
  1. Thoroughly understand the system
  2. Thoroughly understand the technology the system is being deployed upon (e.g., client/server or Internet technologies introduce their own challenges)
  3. Possess creativity, insight, and business knowledge
  4. Understand the development methodology used and the resulting artifacts
Often, successful development teams will have a peer perform the unit testing on a program or class. Once a portion of the application is ready for integration testing, the same benefits can be achieved by having an independent person plan and coordinate the integration testing.

Where an independent test team exists, they are usually responsible for system testing, the oversight of acceptance testing, and providing an unbiased assessment of the quality of an application. The team may also support or participate in other phases of testing as well as executing special test types such as performance and load testing.

An independent test team is usually comprised of a test manager or team leader and a team of testers. The test manager should join the team no later than the start of the requirements definition stage.

Key testers may also join the team at this stage on large projects to assist with test planning activities. Other testers can join later to assist with the creation of test cases and scripts, and right before system testing is scheduled to begin.

The test manager ensures that testing is performed, that it is documented, and that testing techniques are established and developed. They are responsible for ensuring that tests are designed and executed in a timely and productive manner, as well as:
  1. Test planning and estimation
  2. Designing the test strategy
  3. Reviewing analysis and design artifacts
  4. Chairing the Test Readiness Review
  5. Managing the test effort
  6. Overseeing acceptance tests
Testers are usually responsible for:
  1. Developing test cases and procedures
  2. Test data planning, capture, and conditioning
  3. Reviewing analysis and design artifacts
  4. Testing execution
  5. Utilizing automated test tools for regression testing
  6. Preparing test documentation
  7. Defect tracking and reporting
Other testers joining the team will primarily focus on test execution, defect reporting, and regression testing. These testers may be junior members of the test team, users, marketing or
product representatives, and so on.

The test team should be represented in all key requirements and design meetings including:
  1. JAD or requirements definition sessions
  2. Risk analysis sessions
  3. Prototype review sessions

They should also participate in all inspections or walkthroughs for requirements and design artifacts.

The previous post is regarding software testing matrices .



To Do Next: Thank you for visiting PROGRAMMING BLOG. If you liked the post, please subscribe to my blog via email or RSS FEED.You can contact me here for any specific feed back .