Lab ACN : C++ Programming Exercises

Similar documents
n Group of statements that are executed repeatedly while some condition remains true

Dept. of CSE, IIT KGP

Chapter 3 Problem Solving and the Computer

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

Positional notation Ch Conversions between Decimal and Binary. /continued. Binary to Decimal

Loops / Repetition Statements

Add Subtract Multiply Divide

PROGRAMMING IN C AND C++:

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Final Labs and Tutors

Lab 2: Structured Program Development in C

Other Loop Options EXAMPLE

Chapter 3 Structure of a C Program

Introduction to Object Oriented Systems Development. Practical Session (Week 2)

A complement number system is used to represent positive and negative integers. A complement number system is based on a fixed length representation

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

ALIGARH MUSLIM UNIVERSITY Department of Computer Science. JAVA Lab Assignment Course: MCA II nd Semester Academic Session:

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Write a java program to prints the count of odd and even no s entered.

LAB 2.1 INTRODUCTION TO C PROGRAMMING

in this web service Cambridge University Press

Solving Recursive Sequences by Iteration

Outline for Today CSE 142. Programming a Teller Machine. CSE142 Wi03 I-1. ATM Algorithm for Dispensing Money

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

LAB 5: REPETITION STRUCTURE(LOOP)

METHODS EXERCISES GuessNumber and Sample run SumAll Sample Run

Control Statements. If Statement if statement tests a particular condition

1010 2?= ?= CS 64 Lecture 2 Data Representation. Decimal Numbers: Base 10. Reading: FLD Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Condition Controlled Loops. Introduction to Programming - Python

Flow Chart. The diagrammatic representation shows a solution to a given problem.

SHARDA UNIVERSITY SCHOOL OF ENGINEERING & TECHNOLOGY Mid Term Examination, (Odd Term, ) SOLUTION

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

BINARY SYSTEM. Binary system is used in digital systems because it is:

Lecture 8: Addition, Multiplication & Division

3 The L oop Control Structure

LOOPS. 1- Write a program that prompts user to enter an integer N and determines and prints the sum of cubes from 5 to N (i.e. sum of 5 3 to N 3 ).

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Subject: Computer Science

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

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

in normal arithmetic = == = == 5 7 x 2 = 24 7 * 2 == = 3 remainder 1 7 / 2 7 % 2

Programming for Engineers Iteration

NUMBER OPERATIONS. Mahdi Nazm Bojnordi. CS/ECE 3810: Computer Organization. Assistant Professor School of Computing University of Utah

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

Digital Fundamentals

Number Systems. Both numbers are positive

9/10/2016. Time for Some Detailed Examples. ECE 120: Introduction to Computing. Let s See How This Loop Works. One Statement/Step at a Time

LAB 1 Binary Numbers/ Introduction to C. Rajesh Rajamani. ME 4231 Department of Mechanical Engineering University Of Minnesota

UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA C ASSIGNMENTS

Lab 1. largest = num1; // assume first number is largest

Chapter 1 An Introduction to Computer Science. INVITATION TO Computer Science 1

Computer System and programming in C

Mth 60 Module 2 Section Signed Numbers All numbers,, and

Control Statements: Part 1

Programming for Engineers Introduction to C

Introduction to Computer Science Midterm 3 Fall, Points

EE 109 Lab 8a Conversion Experience

CHAPTER 3 Expressions, Functions, Output

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Project 3: RPN Calculator

C: How to Program. Week /Mar/05

ROUNDING ERRORS LAB 1. OBJECTIVE 2. INTRODUCTION

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

Arithmetic type issues

Number System. Introduction. Decimal Numbers

Variables, Data Types, and Arithmetic Expressions Learning Objectives:

Section we will not cover section 2.11 feel free to read it on your own

(Following Paper ID and Roll No. to be filled by the student in the Answer Book)

WARM UP LESSONS BARE BASICS

Control Structure: Loop

The Hyderabad Public School, Begumpet, Hyderabad, A.P

CHW 261: Logic Design

Chapter 2 - Introduction to C Programming

Multiply and Divide Integers

Lesson 9: Decimal Expansions of Fractions, Part 1

1. Let n be a positive number. a. When we divide a decimal number, n, by 10, how are the numeral and the quotient related?

ADD Identifier-1 TO Identifier-2 Literal-1. ADD Identifier-1. GIVING Identifier-2 Literal-1

Number Systems and Conversions UNIT 1 NUMBER SYSTEMS & CONVERSIONS. Number Systems (2/2) Number Systems (1/2) Iris Hui-Ru Jiang Spring 2010

SECTION 5.1. Sequences

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Loops / Repetition Statements

Tutorial for Chapter 3, 4

LAB 5: REPETITION STRUCTURE(LOOP)

CEN 414 Java Programming

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

CONDITION CONTROLLED LOOPS. Introduction to Programming - Python

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

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Math 171 Proficiency Packet on Integers

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

Operators. Lecture 3 COP 3014 Spring January 16, 2018

CS16 Exam #1 7/17/ Minutes 100 Points total

ISA 563 : Fundamentals of Systems Programming

Lecture 2: Number Systems

Chapter 5: Computer Arithmetic. In this chapter you will learn about:

Course Outline Introduction to C-Programming

B. V. Patel Institute of Business Management, Computer & Information Technology, UTU 2013

UNIT- 3 Introduction to C++

Transcription:

Lab ACN : C++ Programming Exercises ------------------------------------------------------------------------------------------------------------------- Exercise 1 Write a temperature conversion program that gives the user the option of converting a value of temperature to Celsius and Fahrenheit scales of temperature. Output: Press 1 to convert Fahrenheit to Celsius Press 2 to convert Celsius to Fahrenheit Enter temperature in Figure Fahrenheit: 1 70 In Celsius that s: 21.111111 Exercise 2 (Automatic Teller Machine) Write a program that takes balance of a user s account as input. It should then ask the user how much amount he wants to withdraw from his account. The program should take this amount as input and deduct from the balance. Similarly it should ask the user how much amount he wants to deposit in his account. It should take this amount as input and add to the balance. The program shall display the new balance after amount has been withdrawn and deposited. Output: Welcome! Enter your balance: 1234 ------------------------------------------------------- Select an option: 1. Withdraw Amount Figure 2 2. Deposit Amount 3. Exit ------------------------------------------------------- How much amount you want to withdraw from your account? Amount: 1233 Your new balance is: 101 How much amount you want to deposit in your account? Amount: 100 Your new balance is: 101 Thank you for using the system!

Note: Your program should have a check on balance and amount being withdrawn. Amounts greater than balance cannot be withdrawn i.e. balance cannot be negative. Exercise 3 Write a program which takes two numbers as input from user and determines which is the larger of the two numbers. The Program should also tell which of the entered numbers is even or odd. Output Welcome Enter number 1: 4 Enter number 2: 7 number 2 is greater than number 1 number 1 is EVEN. number 2 is ODD. Exercise 4 You need to design a calculator that performs four basic arithmetic operations. You will write a program that takes two numbers as input from the user. It then displays a list of arithmetic operations. The user selects one operation and the program displays the result of the corresponding operation. The program should also display which of the two input numbers is greater and which is smaller. Use do-while loop. Interaction with the program might look like the following: Exercise 5 Please enter two numbers 24 8 Please make a selection: 1) Addition 2) Subtraction 3) Multiplication 4) Division 1 Addition:32 24 is greater. Figure 1 Write a program that takes marks of 10 students as input. It calculates the class average and displays it on the screen. Interaction with the program might look like this. Use while loop. Welcome Enter marks: 10 Enter marks: 20 Enter marks: 22.5 Enter marks: 70 Enter marks: 54.75 Enter marks: 87.5 Enter marks: 90.5 Enter marks: 43 Enter marks: 24 Enter marks: 45 Class Average is: 46.725

Exercise 6 Write a program that repeatedly asks the user to enter two money amounts expressed in pounds. The program should then add the two amounts and display the answer in pounds. The program should then convert the amount in Rupees. (Set conversion rate: 1 =120.33 rupees). Display the answer in Rupees. Use a do-while loop that asks the user whether the program should be terminated. Welcome! Enter first amount: 5.10 Enter second amount: 3.2 Total amount: 8.30 Figure 2 Amount in rupees: Rs.998.739/- Do you wish to continue (y or Y/n or N)? Thank you for using the system! Exercise 7 An aircraft system is designed which checks speeds and monitors various functions of the aircraft during the flight. Warnings and errors are generated if any fault develops during the flight. The system is coded in a manner that if 100 is entered as input it indi- cates normal speed range, if 101 is entered it gives over-speed warning indicating that aircraft is overspeeding. If 88 is entered, it indicates Low Oil Pressure and if 187 is entered it indicates Engine Failure. Use switch statement to implement this logic. Exercise 8 Write a program that calculates and prints the sum of the even integers from 2 to user_defined_limit and product of odd integers from 1 to user_defined_limit. Use a do-while loop that asks the user whether the program should be terminated or not. Also maintain a count as to how many times the user ran the do-while loop. Exercise 9 (for loop) Write the following program and execute it. What output does the program display? Expand the shorthand form of expression sum += i-- to show what it actually represents. State the total number of iterations made by the for loop?

Exercise 10 The factorial of an integer n, written n!, is simply the product of all the integers from 1 to n. For example 5! = 1*2*3*4*5 = 120. Write a program to calculate the factorial of integers from 1 to n where n is taken from the user. Your output should look like the sample output given below. Use setw() operator to format your output. What is the maximum value of factorial that you can calculate for int data type? Is there any difference between the maximum value of factorial for unsigned and signed int? Exercise 11 Write a program which asks the user whether he wants to draw a pattern as shown in the screenshot below. If user enters Y or y the program should draw the pattern. If Q or q is opted the program should quit. However, if any other alphanumeric input is given to program the program shall display that the particular input is not recognized. Use nested for loops in your program. Interaction with the program might look like this:.

Exercise 12 A calculator for a supercomputer is to be designed using C language. While the engineers were working on the project they found that the calculator was not giving correct answers for very large calculations. For example, if 150000000 is to be multiplied by 10; the calculator gave faulty result. The bug was detected to be the limited range of integers. Use type casting to design a calculator that performs arithmetic operations of large inputs. (e.g; 150000000 *10) Take input from the user in integer data type. Exercise 13 Write a program which takes PIN number of the user as input and then verifies his pin. If pin is verified the program shall display pin verified and Welcome ; otherwise the program shall give user another chance. After 4 wrong attempts, the program shall display Limit expired and then exit. Use for loops to implement the logic. Note: 5 random PIN numbers can be assumed and fed into the program with which input pin is matched. Exercise 14 The function definition of starline() function is given to you below. Write a C program that uses the following function to generate the output given below. Exercise 15. Write a program which takes two integers from the user using scanf(). Implement a function

void swapints(int, int) that interchanges the two int values passed to it by main() function. Display the interchanged numbers inside the function using printf(). Also display the values of the two integers in main() function. What do you observe from the output? What happens to the output if you change the function prototype as follows: void swapints(int &, int &); Exercise 16 Write a program that takes a set of test scores of a student. It then uses two functions calculatesum() and calculateaverage() to calculate the sum and average of the data set. Your program must use the following functions: 1. A value-returning function, calculatesum(), to determine the sum of the test scores. Use a loop to read and sum the test scores. The function does not output the sum of the test scores. This task must be done in the main function. 2. A void function, calculateaverage(), to determine the average of the test scores. The func- tion does not output the average test score. This task must be done in the main function. Hint: Use pass by reference for average variable. Exercise 17 If P is the population of the first day of the year, B is the birth rate and D is the death rate, the estimated population at the end of the year is given by formula: P n + ( B * P n ) / 100 ( D * P n ) / 100 The population growth rate is given by the formula: B - D Write a program that prompts the user to enter the starting population, birth and death rates and n, the number of years. The program should then calculate and print the estimated population after n years. Your program must contain the following functions: GrowthRate: This function takes as its parameters the birth and death rates and returns the population growth rate. Estimated Population: This function takes as its parameters the current population, population growth rate and n, the number of years. It returns the estimated population after n years. Your program should not accept negative birth rate, negative death rate or population less than 2.

Exercise 18 Write a function that displays a message indicating the number of times it gets called: For instance The function has been called three times. Write a program that calls this function 10 times from main(). You will need to use a global variable to store the count. A global variable is declared outside the main() function. Note: All variables declared inside a function are local variables. Their value is lost once the function ends. Exercise 19 (Converting a Number from Decimal to Binary) This exercise designs a program that uses recursion to convert a non-negative integer in decimal format to equivalent binary number. Let x be an integer. We call the remainder of x after division by 2 the right most bit of x. Thus, the right most bit of 33 is 1 because 33% of 2 is 1 and right most bit of 28 is 0 because 28% is 0. Suppose we want to find the binary representation of 35. First we divide 35 by 2. The quotient is 17 and the remainder, right most bit of 35 is 1. Next we divide 17 by 2. The quotient is 8 and remainder, that is right most bit of 17 is 1. Next we divide 8 by 2. The quotient is 4 and the remainder that is the right most bit of 8 is 0. We continue this process until the quotient is 0. Thus, to convert an integer num in base 10 into the equivalent binary number we first convert quotient num/2 into an equivalent binary number, and then append the rightmost bit of num to the binary representation of num/2. Recursive Algorithm : dectobin is equal to num if num = 0 dectobin is equal to dectobin(num/base, base) followed by cout << remainder if num >0 Write a recursive function void dectobin () having two input parameters num and base and hence calculate the binary number. Exercise 20 (Generating a pattern) Write an iterative function void printpattern(num) that takes as a parameter a nonnegative integer and generates the following pattern of stars to generate the following pat-

tern. Then try and convert it into a recursive function that takes as a parameter a nonnegative integer and generates the following pattern of stars. E.g. if the non-negative integer is 5, then following pattern is generated: * * * * * * * * * * * * * * * * * * * * * * * * * * * *