0% found this document useful (0 votes)
17 views

Lab1 Introduction I

The document provides instructions for a lab assignment on introductory C++ programming. The assignment involves writing simple C++ programs to display text, calculate averages, and find square roots. It also requires analyzing errors, developing algorithms using pseudocode, and implementing programs with loops and conditional logic. Proper software development processes like problem analysis, algorithm design, coding, and testing are emphasized. The goal is for students to gain familiarity with basic C++ syntax and programming techniques.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lab1 Introduction I

The document provides instructions for a lab assignment on introductory C++ programming. The assignment involves writing simple C++ programs to display text, calculate averages, and find square roots. It also requires analyzing errors, developing algorithms using pseudocode, and implementing programs with loops and conditional logic. Proper software development processes like problem analysis, algorithm design, coding, and testing are emphasized. The goal is for students to gain familiarity with basic C++ syntax and programming techniques.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

The University of Jaffna

Faculty of Engineering
Computer Programming (EC 2010)

Introduction to Programming using C++


Aim: To familiar with the procedure of writing a simple C++ program, such as writing
pseudo code, writing c++ code, compiling, understanding compiler error messages and
executing.

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.

Section I: Writing Simple program


1) Write a program to display “Hello World” on the screen.
2) Compile the program and execute it.
3) Explain what you mean by compiling a program and what you mean by
executing a program.
Section II: Understanding compiler error messages in C++ program
1) A program to evaluate the average value of two numbers is given below. There
PRE LAB are about 6 mistakes in the code. Identify the mistakes without using the c++
compiler.

#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 ;

i) Write down the mistakes.


ii) Then correct all the mistakes and run the program. Now introduce only one of
those mistakes, compile the erroneous program and try to understand the
compiler error message. Repeat the above process for each of the 6 mistakes
one by one.

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;
}

Section III: Problem analysis and programming


You are required to write a program to get a single number as an input and
display its square root. Analyse the problem by considering all the following
possibilities

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)

You might also like