Lab Introduction
Lab Introduction
Lab Introduction
Semester 3
Lab 2
Analysis of Algorithms with respect to Time complexity
Variable Declaration:
Variable Initialization:
Operators in C++
Operators are used to perform operations on variables and values.
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
C++ Strings
Strings are used for storing text set of characters.
We must include an additional header file, the <string> library:
Example
// Include the string library
#include <string>
Syntax:
if(condition)
{
CS212L-Data Structure and algorithms
Data Structure and algorithms CS Lab Manual
//code to be executed
}
B. if-else statement
C. nested if statement
D. if-else-if
E. Switch statement
Loops in C++
There may be a situation, when you need to execute a block of code several
number of times. In general, statements are executed sequentially: The first
statement in a function is executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more
complicated execution paths.
CS212L-Data Structure and algorithms
Data Structure and algorithms CS Lab Manual
1 while loop
2 for loop
Execute a sequence of statements multiple times and abbreviates the
code that manages the loop variable.
3 do...while loop
Like a ‘while’ statement, except that it tests the condition at the end
of the loop body.
CS212L-Data Structure and algorithms
Data Structure and algorithms CS Lab Manual
4 nested loops
You can use one or more loop inside any another ‘while’, ‘for’ or
‘do.while’ loop.
Suppose a class has 27 students, and we need to store the grades of all of them.
Instead of creating 27 separate variables, we can simply create an array:
double grade[27];
dataType arrayName[arraySize];
For example,
int x[6];
Here,
Syntax
void myFunction() {
// code to be executed
}
Example Explained
void myFunction() { // declaration
// the body of the function (definition)
}
Example
int main ()
{
myFunction();
return 0;
}
void myFunction()
{
cout << "I just got executed!";
}
// Error
Class activity
CS212L-Data Structure and algorithms
Data Structure and algorithms CS Lab Manual
1. Write a program to find out the area of triangle when three sides a, b and c of the triangle are
given. Use appropriate statements to input the values of a, b and c from the keyboard.
Formula for the area of triangle is area = (s(s-a)(s-b)(s-c))1/2 where s=(a+b+c)/2
2. Write a program to print the ASCII value of a character.
3. Write a program that inputs two numbers in main() function, passes these numbers to a
function. The function will display the maximum number.
a. Paula and Danny want to plant evergreen trees along the back side of their yard.
They do not want to have an excessive number of trees. Write a program that
prompts the user to input the following:
i. The length of the yard
ii. The radius of the fully-grown tree
iii. The required space between fully grown tree
The program outputs the number of trees that can be planted in the yard and the
total space that will be occupied by the fully-grown tree.
4. Write a program to check whether a number is prime number, even number or odd number
using function.
5. Write C++ program to calculate the factorial of number and ask user to enter Y if he wants
to try another number or N if he wants to terminate the program. For example, if user enters
4 then output of the program will be 4*3*2*1 = 24 and user will be asked to type “Y/N”. If
user enters Y, then you will again ask for ‘n’ and recalculate the factorial. You will keep on
doing it until users enters ‘N’. If user enters ‘N’, program should exit.
6. Write a C++ program which asks user to enter two numbers: a dividend (the top number in a
division) and a divisor (the bottom number). It then calculates the quotient (the answer) and
the remainder, using the / and % operators, and prints out the result also ask user to try
again.(if user enter ‘y’ or ‘Y’ then program again ask for two number if user enter ‘n’ or ‘N’
then it terminate). Solve this problem using for loop control structure.
Expected Output:
7. Write a program that uses four arrays numbers, squares, cubes and sums each consisting of
5 elements. The numbers array stores the values of its indexes, the squares array stores the
squares of its indexes, the cubes array stores the cubes of tits indexes and sums array stores
the sum of corresponding indexes of three arrays . The program should display the values of
all arras and the total of all values in sums array.
Output:
Numbers: 0 1 2 3 4
Squares: 0 1 4 9 16
Cubes: 0 1 8 27 64
Sums: 0 3 14 39 84
Grand total: 140
CS212L-Data Structure and algorithms
Data Structure and algorithms CS Lab Manual
8. Write a program that initializes an array. It inputs a value from the user and searches the
number in the array. Finally, it will display the location of number in array if it exists. (using
sequential search)
9. Write a program that inputs two numbers and one arithmetic operator in main() function,
passes them to a function. The function applies arithmetic operation on two numbers based on
the operator entered by user using switch statement.
10. Write a program that inputs base and height of a triangle in main function and passes them to
a function. The function finds the area of triangle and returns it to main function where it is
displayed on the screen. Area = 1/2(Base*Height)
11. Write a program that calls a function for five times using loop. The function uses a static
variable initialized to 0. Each time the function is called, the value of static variable is
incremented by 1 and is displayed on the screen.
12.
Tasks to be submitted:
Initial array :
58 24 13 15 63 9 8 81 1 78
After updating :
58 24 13 15 59 63 9 8 81 1
4. Write a program that uses three arrays Mango, Orange and Banana to store the number of
fruits purchased by customer. The program inputs the number of mangos, oranges and
bananas to be purchased by customer and stores them in corresponding array. The program
finally displays the total bill of ach customer according to the following prices:
Rs. 20 per mango
Rs. 10 per orange
Rs. 5 per banana
The output should appear as follows:
************************************************************************
Customer No. Mangoes Oranges Bananas Total Bill
************************************************************************
1 5 10 12 Rs.260
REFRENCES: