Lab2 Introduction II

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

The University of Jaffna

Faculty of Engineering
Computer Programming (EC 2010)

Introduction to Programming using C++ (lab 2)


Aim: To familiar with the procedure of writing a simple C++ program, such as writing pseudo code, writing
C++ modular based code, compiling, understanding runtime and logical error messages and handling arrays.

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 the following picture
a) '__' b) /########\
(oo) / # \
+========\/ / # \
/ || %%% || #
* ||-----|| #
"" "" ###########

Section II: Understanding and handling runtime errors and logical errors in C++ program
1. 1 ) Identify the error in the following programs with your explanation and propose a solution to fix
PRE LAB the error in the program
a.
#include <iostream>
using namespace std;
int main() {
int number;
cout << number << endl;
return 0;
}
b.
#include <iostream>
using namespace std;
int main() {
float number,ans;
number = 2.1;
ans = number*(2110/1000);
cout << ans << endl;
return 0;
}
c.
#include <iostream>
using namespace std;
int main() {
unsigned int number;
number = -9;
cout << number << endl;
return 0;
}

2. Define underflow and overflow in C++ programming. Write a simple program to explain the result
of underflow and overflow.

3. Write a simple program using two variables to demonstrate incremental operation using prefix and
postfix notation. Select a data type and use a simple program to demonstrate that the prefix or postfix
caused underflow and overflow problem.

//Sample code for postfix


#include <iostream>
using namespace std;
int main() {
int number1,number2;
number1 = 10;
number2 = number1++;
cout << "Number1: " << number1 << endl;
cout << "Number2: " << number2 << endl;
return 0;
}

4. The program given below is developed to accept ten alphabets as user input and display the
alphabets. Find the problem in the C++ code and fix it. Please list down all of your modification in
the source code and the reason for your modification.

#include <iostream>
using namespace std;
int main ()
{
char letter[10];
int n = 0;
do {
cout << "Enter letter (! to end): ";
cin >> letter[n];
cout << "You entered: " << letter[n] << endl;
n = n+1;
} while (letter[n-1] != '!');
return 0;
}
Section III: Write modular program and handling array
1. What are the practical challenges you may encounter when you use arrays in your program? Write a
simple program to store five of your friends’ name using multidimensional array and write a function
to display all the names.
Note: you are not allowed to use string to store your friends’ name
PRE LAP Note: Please come with your pseudo code for all of the above requirement.

http://w
2. Define buffer overflow in C++. Write a simple program to explain buffer overflow in C++.

3. Create a menu-driven application that accepts a given student’s marks for all subjects for GCE
ordinary level (assume 8 subject) and displays the following information. One of your input must be
78.
a. Display the highest marks of the student
b. Display the lowest marks of the student
c. Search the marks ‘78’ and display a message if found.
d. Sort and display the marks in descending order.
e. Display total marks of the student
f. Display average marks of the student
PRE LAP Note: Please come with your pseudo code for all the above requirement.
http://w
Section V: Simple Software Development Process (Problem analysis, algorithm development, coding
and testing)
Consider you are writing a program to buy pizzas for birthday parties in your class. Assume that you
can only buy the pizza in two different sizes (either large or small) and you can buy only full size
pizza and not partial sizes such as half or quarter. The sizes of each category are fixed but the prices
may vary. The diameter of a large pizza is 12 inch and that of a small pizza is 10 inch. For a party
your requirement for the pizza can be given as total area (e.g. such as 500 square inches). You are
required to write a program that should take the requirement as total area and the prices of each sizes
of pizza and your program has to calculate the number of large pizzas and the number of small pizzas
so that the total cost is minimized.
Note: The large size of an item is not always an economically better solution than the smaller size.
i. Analysis of the problem (inputs, computing and output)
PRE LAP ii. Algorithm design
http://w
iii. Coding
iv. Program testing (all kind of input or boundary values)

You might also like