Sie sind auf Seite 1von 6

Lab 1 Introduction to Variables & Data Types in C++

Exercise 1.1: Identifying Valid Identifiers


Objective: To be able to differentiate between invalid and valid identifiers. Estimated Time: 10 min Identify whether each of the following are valid identifiers or not. (first two entries done for you) Token bool Salary _box23 Name List Name_List float X X3 2N 2p Int Integer integer2 Date&Time b4 X*Y Double XYZ XYZ 44 * 7UP Valid/Invalid Identifier Invalid Valid Reason
C++ keyword cant be used as identifier

Exercise 1.2: Identifying data types

Objective: To be able to identify the values of the type integer, float, char and String Estimated Time: 10 min Identify each value for which simple data type(s) could be used to represent the values. Some values can be represented by more than one data type. Value 1 43.73 0 A $ ? Hello 123 1 1 66 7.8 + int int +36 0.0 127.0 -21 int Yes Float Yes char bool Yes string Invalid

Exercise 1.3: Identifying I/P & O/P variables


Objective: To be able to identify input and output variables used in a program. Estimated Time: 15-20 min Following is an algorithm for a program to be used at chocolate shop by the shopkeeper. When a customer buys chocolates, the shopkeeper has to know the total weight of the chocolates sold, either in ounces, or in pounds. The program can calculate the weight both in terms of ounces and pounds depending on the shopkeepers choice. To calculate the total weight of the chocolates being sold, the program takes two values as input i.e. the number of chocolates and the weight of one chocolate in ounces. (Assume that all the chocolates being sold are of same weight). The program then asks the user to enter the choice to calculate the weight in terms of ounces or pounds. The user enters O to calculate weight in ounces and P for pounds. Depending on the choice entered, the program then calculates the total weight of chocolates. Remember that one pound is equal to 16 ounces. Write How many chocolates? Read number Write What is the weight of one chocolate in ounces? Read weight Write Your choice? Enter O to display weight in ounces and P to display weight in pounds If (choice = O) Total_Weight = number * weight Else If (choice = P) Total_Weight = number * weight /16 Display Total_Weight From the pseudocode given above, identify the input and output variables and their data type and list them in the following table.

Variables

Data type

Exercise 1.4: Identifying I/P & O/P Variables


Objective: To be able to identify input and output variables needed for a program. Estimated Time: 20-25 min
Instructors often assign different weights to exam scores when determining a students class grade. Typically, the final exam is assigned a higher weight than the midterm exam, even though the maximum possible score on the exams may be the same. For example, the midterm might be worth 40 % of the course grade, and the final worth 60%. The formula for the weighted course average in this case is Weighted course average = 40% of ( midterm scores / midterm maximum ) + 60% of (final score / final maximum) To create a program that computes a students weighted course average, follo wing input and output is needed. Input: Midterm score Maximum possible points on midterm Mid term weight Final Score Maximum final points on final Final Weight Course average

Output:

1. Identify the input and output variables needed in the program along with their data types and list them in the following table.

Variables

Data type

2. Write pseudocode for this program.

Exercise 1.5: Problem Solving


Objectives: To be able to understand a problem statement and to devise a possible solution. To be able to identify input and output variables needed for a program. Estimated Time: 20-25 min
You need to write a program that determines whether a meeting room is in violation of fire law regulations regarding the maximum room capacity. The program will read in the maximum room capacity and the number of people to attend the meeting. If the number of people is less than or equal to the maximum room capacity, the program announces that it is legal to hold the meeting and tells how many additional people may legally attend. If the number of people exceeds the maximum room capacity, the program announces that the meeting cannot be held as planned due to fire regulations and tell how many people must be excluded in order to meet the fire regulations. 1. Identify the required input and output variables and their data types and list them in following table. Variables Data type

2. Write pseudocode for this program.

Exercise 1.6: Problem Solving


Objectives: To be able to understand the problem statement and to devise a possible solution. To be able to identify input and output variables needed for a program. Estimated Time: 25 30 min
You need to buy new pencils in every semester during your studies at the university. So you need to have a budget plan for your pencils. But it is difficult to estimate the cost of a pencil after a year because the prices of pencil rise every year. You cannot simply use this years price as the price of pencil two years from now. The cost is likely to be higher than it is today. You need to write a program to calculate the expected cost of a pencil in a specified numbers of years. The program asks for the current cost of the pencil, the numbers of years from now that the pencil will be purchased, and the rate of increase in prices. The program then outputs the estimated cost of the item after the specified period. For example, if the cost of a pencil is 2 rupees and the rate of increase in prices is 5% then the cost of the pencil in next years will be 2.1 where 0.1 is the added cost i.e. 5% of 2 rupees. Again, in the next year the cost will be 2.205. To design the program, perform following steps. 1. Identify the required input and output variables and their data types and list them in following table. Variables Data type

2. Devise formulae needed to calculate the cost.

3. Write pseudocode for the program.

Das könnte Ihnen auch gefallen