Lab1 Introduction I
Lab1 Introduction I
Faculty of Engineering
Computer Programming (EC 2010)
Objectives:
To write simple programs in C++
To understand error messages in C++
To analyse a problem to write it as a code.
Note: A Lab Report is required to be submitted together with pre-lab preparation.
#include <iostream>
using namespace <std>;
int main {
int num1;
int num2;
int avgg;
num1=7;
avgg=(num1+num2)/2;
cout << "first number = "<<num1<<"\n";
cout << "second number = "num2<<"\n";
cout << "average = "<<avgg<<"\n";
return ;
PRE LAB 2) Understand the following program and try to identify the mistakes. Explain the
mistakes clearly.
#include <iostream>
using namespace std;
int main (){
const unsigned short int TOTALCOST=70000;
short int quantity;
float price;
price=TOTALCOST/quantity;
cout << "If the quantity is "<<quantity<<" the unit price is = "<<price<<"\n";
cout<<"You can continue to enter quantity and display the price and if you”
<<”want to leave the program you may type a negative number as “
<<”the quantity \n";
cout<<"Enter the value of quantity : ";
cin>>quantity;
while (quantity>=0){
price=TOTALCOST/quantity;
cout << "The quantity is "<<quantity<<
"and the price is = "<<price<<"\n";
cout<<"Enter the value of quantity : ";
cin>>quantity;
}
return 0;
}
PRE LAB i) If you are required to have 2 separate variables to store input and
calculated square root, how would you declare the input variable and the
variable for square root. (e.g. if you declare „int‟ data type for input
variable what are the possible options of data types for the variable to
store the square root?, explain why each of other data types (int, long,
float, double,….) may or may not be suitable.
PRE LAB ii) Write a pseudo code for the above program.
iii) Write the above task in c++ and execute the program. Try different
formats of displaying the output.
Section IV: Program with loops and conditions
1) You are required to write a program to get a number as an input and display
both the number and its square root, then it should be repeated (It should ask
input and display output repeatedly) for 10 other numbers.
PRE LAB i) Write a program to achieve the above task in pseudo code using only while
loop (without using do-while or for loops).
ii) Write another program to achieve the above task in pseudo code using only
do-while loop (without using while or for loops)
iii) Write another program to achieve the above task in pseudo code using only
for loop (without using while or do-while loops).
iv) Explain the advantages and disadvantages in each of the above three
implementations
v) Convert all three programs in to C++ codes and execute them one by one.
vi) Modify one of the program that you have written in part (i), (ii) or (iii) to
accommodate the following requirements. First write the program in pseudo
code and then convert it to c++ code. The requirements are:
a) the program has to continuously run until the user wants to end, when
the user want to end the program the user can input a negative number,
b) the program should initially display a text that should say that if the user
wants to end the program the user has to input a negative number.
Section V: Simple Software Development Process (Problem analysis, algorithm
development, coding and testing)
1) You are commissioned by the ALPHA supermarket to write a program that will
determine the retail price of an item given suitable input. Their pricing policy is
that any item that is expected to sell in one week or less is marked up 5%, and
any item that is expected to stay on the shelf for more than one week is marked
up 10% over the wholesale price. Be sure to notice that the low markup of 5% is
used for up to 7 days and that at 8 days the markup changes to 10%.
PRE LAB i. Analysis of the problem (inputs, computing and output)
PRE LAB ii. Algorithm design
iii. Coding
iv. Program testing (all kind of input or boundary values)
2) .During the development of the point of sale application, you identified that you
have to implement a special module to calculate total sales in your point of sales
application. Assume that you are continually run the program until you close
your application.
PRE LAB i. Analysis of the problem (workflow, inputs, computing and output)
PRE LAB ii. Algorithm design
iii. Coding
iv. Program testing (all kind of input or boundary values)