Computer Science 1511
Computer Science I

Laboratory Assignment 5
Fun with Operations (5 points)
Due at the end of Lab

Introduction

This lab will explore the use of 'operators' in expressions. An expression is a sequence of operands and operators that reduces to a single value. Expressions are often used to perform mathematical operations and so are a vital part of any program. The lab will introduce the operators and operations that can be performed using these operators. Also we will have a look at precedence and associativity of the various operators.

Types of expressions:

  1. Primary Expressions:

    Most elementary type of expression. A primary expression has only one operand and no operators. An operand can be a name, constant or a parenthetical expression.

    For example:

  2. Binary Expressions:

    Formed by an operand-operator-operand combination. Perhaps the most widely used type of expression and is used to calculate mathematical expressions.

    For example:

  3. Assignment Expressions:

    Actually, assignment expressions are a special type of binary expression. The assignment expression evaluates the operand on the right hand side of the operator (=) and places its value in the variable on the left. On the left side of the assignment operator, an expression is not allowed.

    For example:

  4. Unary Expressions:

    These expressions consist of one operand and one operator. In these type of expressions the result of the operation in the operand itself.

    For example:

Precedence and Associativity:

Precedence is used to determine the order in which different operators in a complex expression are evaluated.

For example:

Associativity is used to determine the order in which operators with the same precedence are evaluated in a complex expression.

For example:

Parenthesizing:

Parenthesizing can be used to explicitly state the precedence for an expression. In this way, the programmer can control the order in which operators are evaluated, overriding C's precedence and associativity rules.

For example:

Calculating intermediate expressions:

An alternative to using parentheses is to calculate a complex expression as a sequence of simple expressions. In this way, a complex expression is built out of easier to understand pieces. As an added bonus, a printf statement can be added after each intermediate step to show the result(s) calculate so far. The main drawback of this approach is that one needs extra variables to hold intermediate results.

For example:

The Program


/*
* Your name
* CS 1511 Section ?
* Today's date
* Lab #5
*/

#include <stdio.h>

int main(void)
{
        int a,b,c;
        int answer;

        printf("\n Enter value for a and b: ");
        scanf("%d %d",&a,&b);

/* Change 1 : replace simple assignment operators by short-hand assignment */

        printf ("\n Expressions evaluated for simple assignments \n");
        a = a + b;
        printf("\t a = a + b , a = %d\n",a);
        a = a - b;
        printf("\t a = a - b , a = %d\n",a);
        a = a * b;
        printf("\t a = a * b , a = %d\n",a);
        a = a / b;
        printf("\t a = a / b , a = %d\n",a);
        a = a % b;
        printf("\t a = a %% b , a = %d\n",a);

/* Change 2 : use parentheses to change the expression so that one + and one - */
/* operator occur before the * and / operators.  Hint:  the answer produced */
/* should be 19. */

        c = 9 * 3 - 5 + 6 / 2 + 1;
        
        printf("\n what is the value of c = 9 * 3 - 5 + 6 / 2 + 1: ");
        scanf("%d",&answer);

        if(answer==c)
                printf("\n You guessed it right ! \n");
        else
        {
                printf("\n Sorry!! The correct answer is: %d",c);
                printf("\n The expression is evaluated as c = ((9 * (((3 - 5) + 6) / 2)) + 1)\n\n");
        }
        
/* Change 3 : Now, rewrite the original expression c = 9 * 3 - 5 + 6 / 2 + 1 */
/* as a sequence of intermediate steps.  Also add a printf after each */
/* intermediate value indicating what part of the expression is captured by */
/* each intermediate piece. The result of the expression after breaking */
/* it down into intermediate steps should be 19*/

       c = 9 * 3 - 5 + 6 / 2 + 1;




/* Change 4 : Use unary operators on a and b to assign the value 54 to c.  */
/* For example, c = ++a + b++ would result in 16 being assigned to c. */

        a = 10;
        b = 5;

        c = ++a + b++;

        if(c == 54)
                printf("You've got the value of C after the new expression to be %d which is right\n",c);

        else
        {
                printf("You've got the value of C after the new expression to be %d which is incorrect\n",c);
                printf("Try again to get C to be 54\n");
        }

        fflush(stdin);
        getchar();
        return 0;
}

Changes in the Program

Look for the words Change 1, Change 2, etc. and do the specified modifications in the program.

Change 1: Replace simple assignment by short-hand assignment operators. Do that for all 5 operations and check the results.

Change 2: Use parentheses to change the precedence in the given expression. The resulting value of c should be 19.

Change 3: Break the new parenthesized expression down into intermediate parts. Produce the same answer (c is 19) without using any parentheses.

Change 4: Try different unary operations on a and b until you find a combination that can produce a result 54 to store into c.

What To Turn In

Turn in hard copies of the source code after you have made all the changes. You will also need to turn in the output of the program after you have made the four changes. Be sure to include a cover sheet specifying your name, course number (CS1511), section number and lab number when you submit the lab assignment.

REFERENCE:


Operator Precedence and Associativity:
-------------------------------------
Note: Table starts from highest precedence.


----------------------------------------------------------------------------------
	Operators 					Associativity	Type
----------------------------------------------------------------------------------

 	()						left to right	parenthesis


	++	--	+	-	!  (type)	right to left	unary

	*	/	%				left to right	binary	

	+	-					left to right	binary

	<	<=	>	>=			left to right	relational
	
	==	!=					left to right 	equality

	&&						left to right	logical AND

	||						left to right	logical OR

	?:						right ot left	conditional

	=	+=	-=	*=	/=	%=	right to left	assignment

	,						left to right	comma

____________________________________________________________________________________