Structured programming. Exercises 3

Similar documents
Structured programming

Fundamental of Programming (C)

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

Operators and Expressions:

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Lecture 3. More About C

Operators & Expressions

Programming Language A

Fundamentals of Programming

Chapter 3 Structure of a C Program

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

Introduction to Programming

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Operators. Lecture 3 COP 3014 Spring January 16, 2018

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Structured programming

Programming in C++ 5. Integral data types

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

C Programming

Programming and Data Structures

For questions 4 through 7, select the value assigned to the relevant variable, given the declarations: 3) ) This is not allowed

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Introduction to C Programming (Part A) Copyright 2008 W. W. Norton & Company. All rights Reserved

Operators in java Operator operands.

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

COP 3275: Chapter 04. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

LECTURE 3 C++ Basics Part 2

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Introduction to Programming

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Operators in C. Staff Incharge: S.Sasirekha

Basic Assignment and Arithmetic Operators

Expressions and Precedence. Last updated 12/10/18

COMP Primitive and Class Types. Yi Hong May 14, 2015

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Fundamentals of Programming Session 20

Operators And Expressions

Programming for Engineers Iteration

Operators and expressions. (precedence and associability of operators, type conversions).

Programming Language A

Unit 3. Operators. School of Science and Technology INTRODUCTION

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

UNIT- 3 Introduction to C++

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Chapter 2. Introduction to C language. Together Towards A Green Environment

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen. Introduction

Chapter 2: Data Types and Arithmetic Expressions TRUE/FALSE

A flow chart is a graphical or symbolic representation of a process.

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Operators. Java operators are classified into three categories:

Introduction to C. Systems Programming Concepts

Data types, variables, constants

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Computer System and programming in C

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Lecture 5: C programming

3. Types of Algorithmic and Program Instructions

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

EECE.2160: ECE Application Programming Spring 2018

Lecture 3 Operators MIT AITI

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Expression and Operator

Arithmetic Operators. Portability: Printing Numbers

printf("%c\n", character); printf("%s\n", "This is a string"); printf("%s\n", string); printf("%s\n",stringptr); return 0;

Chapter 3 Structured Program Development in C Part II

Array1.c. all arrays start at 0 sometimes known as a list

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

CHAPTER 3 Expressions, Functions, Output

3. Java - Language Constructs I

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

The Design of C: A Rational Reconstruction: Part 2

Continued from previous lecture

Course Outline Introduction to C-Programming

Learning to Program with Haiku

COSC121: Computer Systems: Runtime Stack

UNIT 3 OPERATORS. [Marks- 12]

Basics of Programming

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Chapter 2: Using Data

Computer Programing. for Physicists [SCPY204] Class 02: 25 Jan 2018

Dr. R. Z. Khan, Associate Professor, Department of Computer Science

PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE

Chapter 3. Section 3.10 Type of Expressions and Automatic Conversion. CS 50 Hathairat Rattanasook

Part I Part 1 Expressions

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Chapter 12 Variables and Operators

Programming for Electrical and Computer Engineers. Loops

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II)

WARM UP LESSONS BARE BASICS

Week 2 Introduction to Computer and Algorithm (Part 2)

Transcription:

Exercises 3

Table of Contents 1. Reminder from lectures...................................................... 1 1.1. Relational operators..................................................... 1 1.2. Logical operators........................................................ 1 1.3. Additional operators..................................................... 2 1.4. Assignment operator =................................................... 2 1.5. Increment and decrement operators...................................... 2 1.6. Compound operators.................................................... 3 2. s................................................................... 4 2.1. Variables and assignment................................................ 4 2.2. Casting cast operation................................................. 4 2.3. Casting in another type................................................... 4 2.4. Casting................................................................. 5 3. Problems.................................................................... 5 3.1. Problem 1............................................................... 5 3.2. Problem 2............................................................... 6 3.3. Problem 3............................................................... 6 3.4. Problem 4............................................................... 7 3.5. Problem 5............................................................... 8 3.6. Problem 6............................................................... 8 3.7. Problem 7............................................................... 9 3.8. Problem 8............................................................... 9 4. Source code of the examples and problems................................... 11

1. Reminder from lectures Operators Arithmetic Relational Logical Casting values cast operation 1.1. Relational operators Are used on all comparable types, and the result is an integer 0 (false) or 1 (true). Operator Meaning < Less then <= Less then or equal > Greater then >= Greater than or equal == Equality!= Non equality (different) 1.2. Logical operators Used in combination with the relational operators to form complex logical expressions, that again return result 0 or 1. Operator && Operation Logical AND Logical OR! Negation int a = 5 && 0; // a = 0; a = 2 && 5; // a = 1; a = 0 5; // a = 1; a =!0; // a = 1; a =!5; // a = 0; 1. Reminder from lectures 1

1.3. Additional operators Assignment operator = Incrementation and decrementation operators (++, --) ++ incrementing (increasing the value for 1) -- decrementing (decreasing the value for 1) Using the operators + and in unary manner X = + Y; X = - Y; Compound operators Combination of assignment operator and other operator (+=, -=, *=, /=, %=) 1.4. Assignment operator = All expressions have values, even the ones that contain = The value of that expression is taken from the right hand side So an assignment of this form can be applied: x = (y = 10) * (z = 5); x = y = z = 20; 1.5. Increment and decrement operators Increment operator ++ (increasing the value of the operand for 1) Decrement operator --(decreasing the value of the operand for 1) Can be applied in prefix or postfix notation: 1.5.1. Prefix The value of the variable is incremented before the evaluation of the expression it is part of 2 1.3. Additional operators

a = ++b; 1.5.2. Postfix The value of the variable is incremented after the evaluation of the expression it is part of a = b++; 1.6. Compound operators Operator += a += 5; // a = a + 5; a += b * c; // a = a + b * c; Operator -= a -= 3; // a = a 3; Operator *= a *= 3; // a = a * 3; Operator /= a /= 3; // a = a / 3; Operator %= a %= 3; // a = a % 3; 1.6. Compound operators 3

2. s 2.1. Variables and assignment int main () { int a; float p; p = 1.0 / 2.0; /* p = 0.5 */ a = 5 / 2; /* a = 2 */ p = 1 / 2 + 1 / 8; /* p = 0; */ p = 3.5 / 2.8; /* p = 1.25 */ a = p; /* a = 1 */ a = a + 1; /* a = 2; */ 2.2. Casting cast operation Format (data_type) value int i; double d = 7.28; i = (int) d; (int) 5.56 // 5.56 in 5 (long) 8.28 // 8.28 in 8L (double) 2 // 2 in 2.0 (char) 70 // 70 in char with ASCII 70 ('F') (unsigned short) 3.14 //3.14 in 3 (unsigned short) 2.3. Casting in another type Manipulating the format specification %f and division operation 4 2. s

int integer1; /* first number entered by the user */ int integer2; /* second number entered by the user */ int sum; /* variable for storing the sum */ float quotient; /* the resut from the division */ printf("enter the first integer\n"); scanf("%d", &integer1); printf("enter the second integer\n"); scanf("%d", &integer2); /* read integer */ sum = integer1 + integer2; /* assign the sum with the result from the addition */ quotient = (float) integer1 / integer2; /* assign the quotient with the result from the division */ printf("the sum is %d\n", sum); /* prints the sum */ printf("their quotient is %.2f\n", kol); /* prints the quotient */ 2.4. Casting In the previous example we used cast operator (converting one type to another): qutient = (float) integer1 / integer2; Because integer1 and integer2 are integers, the result from the integer division would not be the expected one. To get a float result one of the values integer1 or integer2 should be casted to float or double. The same effect of casting int to double can be achieved by multiplying the variable with const double with value 1.0 kol = 1.0 * integer1 / integer2; 3. Problems 3.1. Problem 1 Write a program that reads character from SI and depending if it is lowercase or uppercase will print 1 or 0 accordingly. Use logical and relational operators for testing the ASCII value of the character. 2.4. Casting 5

Extra: Check if the character is digit Solution 1 char ch; int rez; printf("enter char: "); scanf("%c", &ch); rez = (ch >= 'a') && (ch <= 'z'); printf("%d\n", rez); Solution of extra rez = (ch >= '0') && (ch <= '9'); 3.2. Problem 2 Write a program that reads two integers (x, y) and will print the result of (z) the following expression z = x++ + --y + (x<y) What is the value of z For x = 1, y = 2? Solution 2 int x, y, z; printf("enter x and y: "); scanf("%d%d", &x, &y); z = x++ + --y + (x < y); printf("z = %d\n", z); What will hapen in this situation: z = x++ + --y + x<y; 3.3. Problem 3 Given 6 3.2. Problem 2

r = (x<y y<z++) What will be the value of r for x=1, y=2, z=3? What will be the value of z? Given: r = (x>y && y<z++) What will be the value of r for x=1, y=2, z=3? What will be the value of z? Solution 3 int x = 1, y = 2, z = 3, r; r = (x < y y < z++); printf("r = %d, z = %d\n", r, z); r = (x > y && y < z++); printf("r = %d, z = %d\n", r, z); Output r=1, z=3 r=0, z=3 3.4. Problem 4 Given: int x, y; y = scanf("%d", &x); printf("y = %d\n", y); What will be the value of y for x=5? Output y=1 3.4. Problem 4 7

Given: int x, y, z; z = scanf("%d%d", &x, &y); printf("z = %d\n", z); What will be the value z for x=5, y=6? Output z=2 3.5. Problem 5 Write a program where you read from SI price of product, and then will print it s price with calculated with taxes. The tax is 18% of the price. Solution 5 float price; printf("enter the product price: "); scanf("%f", &price); printf("the total price of the product is: %.2f\n", price * 1.18); 3.6. Problem 6 Write a program where you read from SI price of product, number of installments and interest rate (percents from 0 to 100). The program should output the amount of the installment and total price including the interest. First compute the total sum, then the installment amount. 8 3.5. Problem 5

Solution 6 float price, interest, amount; int installments; printf("enter the product price: "); scanf("%f", &price); printf("enter number of installments: "); scanf("%d", &installments); printf("enter interest rate: "); scanf("%f", &interest); float total = price * (1 + interest / 100); printf("installment amount: %.3f\n", total / installments); printf("total payed amount: %.3f\n", total); 3.7. Problem 7 Read a three digit integer from SI. Then print the most significant and least significant digit. : For the number 795, the program should print: Most significant digit is 7, and least significant digit is 5. Use integer division and modulo operation. Solution 7 int number; printf("enter number:\n"); scanf("%d", &number); printf("most significant digit is "); printf("%d, and least significant digit is %d.\n", (number / 100), (number % 10)); 3.8. Problem 8 Write a program where from the birth date read from SI (in format ddmmyyyy) would print the month and day of birth. : For the following input 18091992, the program should print: 18.09 Use integer division and modulo operation. 3.7. Problem 7 9

Try to solve using only the scanf function. Solution 8 long int date; printf("enter birth date in format (ddmmyyyy):\n"); scanf("%ld", &date); int day = date / 1000000; int month = (date / 10000) % 100; printf("the day and the month of birth are %02d.%02d\n", day, month); 10 3.7. Problem 7

4. Source code of the examples and problems https://github.com/finki-mk/sp/ Source code ZIP 4. Source code of the examples and problems 11