CENG 106 Lab2

download CENG 106 Lab2

of 8

Transcript of CENG 106 Lab2

  • 7/27/2019 CENG 106 Lab2

    1/8

    Qatar University

    College of Engineering

    Dept. of Computer Science & Engg

    Computer Programming

    GENG106

    Lab HandbookFall 2012Semester

    Lab2: Expressions and Interactivity

    Arithmetic Expressions, Built-in Functions and Files

    ObjectivesAt the end of this lab, you should be able to:

    Write C++ programs that solve simple arithmetic calculations Practice operators' priorities Use built-in math functions Write data to files using ofstream Read data from files using ifstream

    Quick Review

    Operator Operation Example (a=14,b=4)

    + Addition a+b=18

    - Subtraction a-b=10

    * Multiplication a*b=56

    / Divisiona/b=3 (integer division if both a and b integers)

    a/b=3.5 (if a or b is float or double)

    % Modulus a%b=2 ( reminder of 14/4)

    Astream is a general name given to a flow of data. In C++ a stream is represented by an objectof a particular class. Examples of stream objects familiar to us are cin and cout.

    Different streams are used to represent different kinds of data flow.

    Input stream Output stream

    Working with disk files requires another set of streams (classes): ifstream forinput, fstream for both input and output, and ofstream foroutput.

    These streams are declared in the fstream file. Include it in your program using

    #include

    1111000010101000101010010100010100011111

    00001010100010101000

    10101000101010001111

    11

    10

    0To program

    1111000010101000101010010100010100011111

    00001010100010101000

    10101000101010001111

    11

    10

    0

    From Program

  • 7/27/2019 CENG 106 Lab2

    2/8

    1

    In formatted I/O, numbers are stored on disk as a series of characters. Thus 6.02, for example, isstored as the characters '6', '. ', '0', and '2'. Characters and strings are stored more or less

    normally.

    To open a file for output create an ofstream object, e.g. ofstream outfile("fdata.txt"); To send output to the file use the ofstream object, e.g. outfilemessage; When you finish working with the file, terminate the connection, e.g. outfile.close();

    1) The following program is intended to calculate the result of the following formula:

    #include

    usingnamespace std;int main()

    {

    double y,x,m,b,n;

    coutx;

    coutm;

    coutb;

    coutn;

    y=m*x+b/n;

    cout

  • 7/27/2019 CENG 106 Lab2

    3/8

    2

    2) Write a program that will read two values double numbers and from the keyboard and thencalculate the values of the following:

    Hint: Use the pow() function Hint: To convert to radians multiply it by . Use = 3.14159Output the results of and on the screen.Hint: You have to use the cmath library; consult the Library Reference panel in

    www.cplusplus.comto explore how to use the sin() and pow() functions.

    3) Write a program that asks the user to type in a time in seconds and display it in the standardformat hours:minutes:seconds (where 1 hour = 3600 seconds and 1 minute = 60

    seconds). Your output should look similar to this:

    4) The midpoint of the line joining two pointsA(x1, y1) andB(x2, y2) is

    and the

    between

    the two points is .Write a C++ program that asks the user to enter the

    coordinates of the end points of a straight line and then

    output the coordinates of its midpoint together with the

    distance between the two points..

    Test your program using the coordinates of the following line:

    http://www.cplusplus.com/http://www.cplusplus.com/http://www.cplusplus.com/
  • 7/27/2019 CENG 106 Lab2

    4/8

    3

    5) The power, P, dissipated in a resistor of resistance Rwhen a current iflows throw it is given byP=i

    2R. Write a C++ program that reads the resistance R(in ohms), i(in amps)andthen outputs

    P.

    6) Write a C++ program to read three numbers , and that represent the coefficients of thequadratic equation 2++=0, and then calculate and print its roots (i.e. the points where thecurve cuts the x-axis) 1 and 2 which are found using the formula:

    Use your program to calculate the roots of the equations represented by the following twocurves. Make sure that your results match the values obtained graphically.

    Now, can you use your program to solve x2+x+1=0? .. (Yes/No)If the answer is No; is it because of an error in your program? Or is it because the equation

    doesnt have real roots? .

    How can you fix your program to avoid passing a negative value to the sqrt()function?

    y = x2 - 5x + 6 y = 2x2 x 3

  • 7/27/2019 CENG 106 Lab2

    5/8

    4

    7) Writing data/output to a file:Save the following program and then run it:

    #include

    #include

    #include

    usingnamespace std;

    int main()

    {

    char ch='X';

    int j=77;

    double d=6.02;

    string str1="Computer";

    string str2="Programming";

    ofstream outfile;

    outfile.open("fdata.txt");

    outfile

  • 7/27/2019 CENG 106 Lab2

    6/8

    5

    8) Reading data from a file:Save the following program and then run it:

    #include

    #include

    #include

    usingnamespace std;

    int main(){

    char ch;

    int j;

    double d;

    string str1, str2;

    ifstream infile;

    infile.open("fdata.txt");

    infile >> ch >> j >> d >> str1 >> str2;

    cout

  • 7/27/2019 CENG 106 Lab2

    7/8

    6

    Program 2: Write a program that opens the file created by Program 1, reads the five numbers,

    and displays them.

    The program should also calculate and display the sum of the five numbers.

    Program screen output

    Additional Exercises

    10)The polar coordinates of a point P(x,y)are r or simply (r, ), where and

    .

    Tip: Use atan() from the cmath library

    Write a C++ program that asks the user to enter the

    Cartesian coordinates (xand y) of a point and then

    print the equivalent polar coordinates of that point in

    the format (, ). (Make sure that the angle isdisplayed in degrees). Test your program using values

    from the following graph:

    11)The sine ru lestates that in any triangle ABC: where , and are the sides opposite to the angles A, Band Crespectively.

    Write a C++ program that reads two angles; say A and B, of a

    triangle and the side between them, say c. Then let your

    program calculate and output the other two sides and the third

    angle. Run your program and enter the values based on the figure below:

    Tip: Remember that angles of a triangle add up to 180.

  • 7/27/2019 CENG 106 Lab2

    8/8

    7

    12)The cosine rulestates that in any triangle ABC where a, band care the sides opposite to the angles A, Band C

    respectively.

    An engineer is making a triangular component from a steel sheet with the dimensions shown inthe figure below. He wants to calculate the length of the side ACfor the following values of

    angle B: 80o, 95o and 105o.

    Write a C++ program that will help this

    engineer by printing each value of angle Band

    the corresponding value of the side AC.

    13)To calculate the flow rate; of a liquidflowing in a pipe, a Venturi meter is

    used (see figure on the right). The

    cross-sectional area of the unrestricted

    pipe is and that of the throat (i.e. therestricted part) is.The flow rate

    is found from:

    where is the difference in height and is the acceleration due to gravity (9.81 m/sec2), whichis constant.

    Write a C++ program that reads r1and r2(the radii of the unrestricted pipe and the throat) and

    the difference in height, , and then output the flow rate, .