COMP1170 Lab Exercise 02 Questions and Answers Task 1: Selection structures: if statement Task 2: Using only the techniques you learned in this chapter, write a program that calculate the squares and cubes of the number from 0 to 10 and uses tabs to print the following table of values: number 0 1 2 3 4 5 6 7 8 9 10 sequare 0 1 4 9 16 25 36 49 64 81 100 cube 0 1 8 27 64 125 216 343 512 729 1000 Reference Code: #include <stdio.h> int main(void) { // initialization int i; printf("Number\tSquares\tCubes\n"); // iterate i for ten times for (i=0; i<=10; i++){ printf("%d\t%d\t%d\n", i, i*i, i*i*i); } return 0; } Note: we use a repetition sturcure by for statement. Task 3: Write a program that reads in the radius of a circle and prints the circle’s diameter, circumference and area. Hints: 1. using floating-point numbers %f instead of %d to print out the result. 2. float pi; pi = 3.14156. 3. diameter = 2*radius; circumference = 2*pi*radius; area = pi*radius*radius. Reference Code: #include <stdio.h> int main(void) { // initialization float radius, diameter, circumference, area; float pi = 3.14156; // input radius printf("Please enter the radius:\n"); scanf("%f", &radius); // calculate diameter, circumference and area diameter = 2 * radius; circumference = 2 * radius * pi; area = radius * radius * pi; // output and termination printf("Diameter is: %f\n", diameter); printf("Circumference is: %f\n", circumference); printf("Area is: %f\n", area); // return the value of main function return 0; } Task 4: Write a program that asks the users to enter two numbers, and obtains the two numbers from the user and prints the sum, product, difference, quotient and remainder of the two numbers. Reference Code: #include <stdio.h> int main(void) { // initialization int num1, num2; int sum, product, qotient, remainder; // input two integers printf("Please enter two integers:\n"); scanf("%d %d", &num1, &num2); // calculate sum, product, qotient and remainder sum = num1 + num2; product = num1 * num2; qotient = num1 / num2; remainder = num1 % num2; // output and termination printf("Sum is: %d\n", sum); printf("Product is: %d\n", product); printf("Qotient is: %d\n", qotient); printf("Remainder is: %d\n", remainder); // return the value of main function return 0; } Task 5: Write a program that asks the user to enter two integers, obtains the numbers from the user, then prints the larger number followed by the words “is larger”. Use If … else statement. Hints: if (a >b) …. else …. Reference Code: #include <stdio.h> int main(void) { // initialization int num1, num2; // Enter two numbers printf("Please enter two integers:\n"); scanf("%d %d", &num1, &num2); // Decide which number is larger if (num1 > num2) { printf("The larger number is: %d\n", num1); } else if (num1 == num2) { printf("Two numbers are equal.\n"); } else { printf("The smaller number is: %d\n", num1); } // return the value of main function return 0; } Q1: What is “header file”, “resource file” and “source file”? A1: A header file typically contains definitions for functions and class declarations. The purpose of the header file is to define functions/ classes/ methods so that you can use the library that MAY be built using that header file. A resource file just contains dialog/window/icon/bitmap/image information that is designed to be included with a project so that you can use a GUI resource editor to design the layout of an application. A source file is just where you have regular code any .cpp file where you have your main definition etc. For short, header files are intended to contain function and class declarations. Source files contain the actual class or function code. Resource files contain objects such as icons, bitmaps, pointers, and window layout information. Q2: How to debug your program? A2: We can right click our program and set “breakpoint” to the statement. Then we click “Start debugging”, and program will stop at the breakpoint you set. You will check the variable value and data type in the debugging mode. You can press F10 to continue the debugging until the program exits.