Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

TCS1011 Data Structures and Algorithms

Lab01

Lab 1 Are You Ready?


Objectives
a) Test yourself to identify whether you are well-prepared for TCS1011. b) Revise what you have learned in Computer Programming I (CP1) and Computer Programming II (CP2). c) Practice your problem-solving skill.

Introduction
Welcome to the first lab of TCS1011, Data Structures and Algorithms! By now you all should have taken CP1 and CP2. There are many programming concepts that you are expected to know. Therefore, we prepare some programming exercises to see how comfortable you are with the programming concepts you have learned so far. There are 5 questions altogether. The questions are independent of one another. Your goal is to complete as many questions as possible. If you are comfortable with CP1 and CP2, you should be able to solve at least 4 questions. So, are you ready? Get set. Go!

Programming Exercises
Question 1: [10 minutes]
/******************************************************************************* Your Task: Write a program that displays a triangle given the height of the triangle. Sample Run 1: Enter height: 3 * *** ***** Sample Run 2: Enter height: 5 * *** ***** ******* ********* *******************************************************************************/

TCS1011 Data Structures and Algorithms

Lab01

#include <iostream> using namespace std; int main() { unsigned int height = 0; cout << "Enter height: "; cin >> height; // TODO: Write your code here // Begin Question

// End Question return 0; }

Question 2: [15 minutes]


/******************************************************************************* Your Task: This program accepts a name from the user and display the text as shown in the sample run. However, there are syntax as well as logical errors in the program. Correct the errors such that given the sample input below, the output will be correct as indicated. Sample run 1: Enter a name -> william Output : william illiam lliam liam iam am m Sample run 2: Enter a name -> cyberjaya Output : cyberjaya yberjaya berjaya erjaya rjaya jaya aya ya a *******************************************************************************/ #include <iostream> #include <cstdlib> //system using namespace std; int main() { char name[20]; char* ptr1,*ptr2; system("title Lab 1: Are you ready?"); cout << "Enter a name -> "; cin >> name; cout << "Output : "; for (ptr1 = &name; ptr1 != '0'; ptr1++) { for (ptr2 = &ptr1; ptr2 != '0'; ptr2++) cout << *ptr2; cout << ' '; }

TCS1011 Data Structures and Algorithms


cout << endl; system("pause"); return 0; }

Lab01

Question 3: [15 minutes]


/******************************************************************************* Your Task: Write a program that accepts a word as an input, and outputs the number of different consonants (b,d,f,g,h...) in the word. The word consists of small and capital alphabets only. For example, the output for the word "Lolz" is 2 because there are 2 different consonants ('l' and 'z') Sample run 1 : Enter a word -> Lolz Number of different consonants : 2 Sample run 2 : Enter a word -> MamMaMia Number of different consonants : 1 *******************************************************************************/ // TODO: Write your code here // Begin Question

// End Question

Question 4: [10 minutes]


/******************************************************************************* Your Task: The program below has a class "Complex" with all operations needed. A complex number is represented using [real, imaginary] pair. You are required to make use of the class to instantiate the objects c1, c2, c3, and any number of Complex objects required to calculate complex number d according to the formula: d = (c1+c2+c3)*c1 - c2*c3; where c1,c2,c3, and d are complex numbers. You are reminded that the code given below is error-free. DO NOT attempt to modify any of the code given. You just need to add statements in the main() function to make the objects perform the appropriate operations and generate the desired output. Sample run 1: Input: -> 2.5 4 -> 3.1 7 -> 1.5 2.5 Output: [real= -23.4, imag= 43.9] Sample run 2: Input: -> 1 2 -> 6 5 -> 3 4

TCS1011 Data Structures and Algorithms


Output: [real= -10, imag= -8]

Lab01

*******************************************************************************/ #include <iostream> using namespace std; class Complex { public: void set(float r, float im) { real = r; imag = im; } void copy(Complex c) { real = c.real; imag = c.imag; } void display() { cout << "[real= " << real << ", imag= " << imag << "]" << endl; } void multiply(Complex c) { float r = real * c.real - imag * c.imag; float i = real * c.imag + c.real * imag; real = r; imag = i; } void add(Complex c) { real += c.real; imag += c.imag; } void minus(Complex c) { real -= c.real; imag -= c.imag; } private: float real, imag; }; int main() { float r1,i1,r2,i2,r3,i3; cout << "-> "; cin >> r1 >> i1; cout << "-> "; cin >> r2 >> i2; cout << "-> "; cin >> r3 >> i3; // TODO: Write your code here // Begin Question // goal: d = (c1+c2+c3)*c1 - c2*c3;

// End Question

TCS1011 Data Structures and Algorithms


system("pause"); return 0; }

Lab01

Question 5: [20 minutes]


/******************************************************************************* Your Tasks: Write a program that accepts a tic-tac-toe board game played by 'X' and 'O' and output which player wins. The '-' means empty slot. Sample Run 1: Enter a tic-tac-toe game: O X - X O - X Player X wins. Sample Run 2: Enter a tic-tac-toe game: X - O X X O O O Player O wins. Sample Run 3: Enter a tic-tac-toe game: X - O X O X O - X Player O wins. Sample Run 4: Enter a tic-tac-toe game: - - O X X O O - X No player wins. *******************************************************************************/ // TODO: Write your code here // Begin Question

// End Question

Happy Programming! :)

You might also like