0% found this document useful (0 votes)
164 views4 pages

Tutorial 2 Qs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 4

TK 1914, C Programming, Trim.

1 2011/2012

Tutorial 2

Tutorial 2 Basic Elements of C++ &Input Output


Q1: Trace the following programs and tell what each will display. Program 1: Suppose the input is:
Miller 30 100 #include <iostream> #include <string> using namespace std; const int PRIME_NUM = 10; int main() { const int SECRET = 20; string name; int id; int num; int mysteryNum; cout << "Enter last name: "; cin >> name; cout << endl; cout << "Enter a two digit number: "; cin >> num; cout << endl; id = 100 * num + SECRET; cout << "Enter a positive integer less than 1000: "; cin >> num; cout <<endl; mysteryNum = num * PRIME_NUM - 3 * SECRET; cout <<" Name: " << name <<endl; cout <<" id: " << id << endl; cout <<" Mystery number: " << mysteryNum << endl; system ("Pause"); return 0; }

TK 1914, C Programming, Trim. 1 2011/2012

Tutorial 2

Program 2: Suppose the input is: 40000


#include <iostream> using namespace std; int main() { double salary, monthly; cout <<" What is your annual salary? "; cin >> salary; monthly = static_cast<int>(salary) / 12; cout << Your monthly wages are << monthly << endl; system ("Pause"); return 0; }

Program 3:
#include <iostream> using namespace std; int main() { int x, y, z; x = y = z = 0; x +=2; y -= 1; z *= 3; cout << x << << y << << z << endl; system ("Pause"); return 0; }

TK 1914, C Programming, Trim. 1 2011/2012

Tutorial 2

Q2: The following program has syntax mistakes. Correct them. On each successive line, assume that

any preceding error has been corrected.


const char = STAR = * const int PRIME = 71; int main() { int count, sum; double x; count = 1; sum = count + PRIME; x := 25.67; newNum = count * ONE + 2; sum + count = sum; x = x + sum * COUNT; cout << count = << count << , sum = << sum << , PRIME = << Prime << endl; }

Q3: Consider the following program segment. Complete it by following the instructions below.
#include <iostream> using namespace std; int main() { // variable declaration // executable statements return 0; }

1. Write C++ statements that declare the following variables: num1, num2, num3, and average of type int 2. Write C++ statements that store 125 into num1, 28 into num2, and -25 into num3. 3. Write C++ statements that stores the average of num1, num2, and num3, into average. 4. Write C++ statements that output the values of num1, num2, num3, and average.

TK 1914, C Programming, Trim. 1 2011/2012

Tutorial 2

Q4: Write a program that displays the following information, each on a separate line: a. Your name b. Your address, with city, state, and zip code. c. Your telephone number d. Your collage major Using only a single cout statement to display all of this information.

Extra Exercise 1. Newtons law states that the force, F, between two bodies of masses M1 and M2 is given by: M M F k 1 2 2 d In which k is the gravitational constant ( approximately 6.67 x 10-8 dyn. cm2/g2). Write a program that prompts the user to input the masses of the bodies and the distance between the bodies. The program then outputs the force between the bodies as shown below.

2. There are three seating categories at a stadium. For a football game. Class A seats cost RM 20, Class B seats cost RM 15, and Class C costs RM 9. Write a program that asks how many tickets for each class of seats were sold, then displays the amount of income generated from ticket sales. Format your dollar amount in a fixed-point notation with two decimal points and make sure the decimal point is always displayed.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy