Lab 01
Lab 01
Lab01
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 * *** ***** ******* ********* *******************************************************************************/
Lab01
#include <iostream> using namespace std; int main() { unsigned int height = 0; cout << "Enter height: "; cin >> height; // TODO: Write your code here // Begin Question
Lab01
// End Question
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
Lab01
// End Question
Happy Programming! :)