assignment final.docx

download assignment final.docx

of 94

description

sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss

Transcript of assignment final.docx

  • 7/16/2019 assignment final.docx

    1/94

    Introduction to Computing

    SUBMITTED BY:

    MUHAMMAD KHIDR ZARA MALIK AQSA SHAHID AMNA ZAHEER SAMIA ZAKIR

    REGISTRATION #:

    L1S13BSCS0145 L1S13BSCS0152 L1S13BSCS2169 L1S13BSCS2142 L1S13BSCS0170

    SECTION: D

    SUBMITTED TO:

    SIR AZEEM AHMAD

  • 7/16/2019 assignment final.docx

    2/94

    WEEK 6 LAB 1:

    Program 1:

    State the order of evaluation of the operators in each of the following C++ statements and find the

    value of variable, after each statement is performed. Write C++ program to check your answers.

    a = 12 / 3 * 3 b = 10 % 3 6 / 2 c= 5.0 * 2.0 / 4.0 * 2.0 d = 5.0 * 2.0 / (4.0 * 2.0) e= 5.0 + 2.0 / (4.0 * 2.0) f = ( 3 * 9 * ( 3 + ( 9 * 3 / (3) ) ) ); g = 2 * 3 / 4 + 4 / 4 + 8 2 + 5 / 8;

    #include

    void main ()

    {

    int a,b,f,g;

    float c,d,e;

    a=12/3*3;

    cout

  • 7/16/2019 assignment final.docx

    3/94

    cout

  • 7/16/2019 assignment final.docx

    4/94

    #include

    void main ()

    {

    int c,f,d;

    float b;

    double a,e;

    a=(10.0/3.0)+(5*2);

    cout

  • 7/16/2019 assignment final.docx

    5/94

    Program 3:

    Identify and correct the errors in each of the following statements. (Note: There may be none or more

    than one error per statement.)

    int a=2; cout

  • 7/16/2019 assignment final.docx

    6/94

    c= (c++ * b-- - ++a) / --c d= (c++ % ++b) + --a + a

    #include

    void main ()

    {

    int a=10,b=12,c=0,d=0;

    a=a++;

    cout

  • 7/16/2019 assignment final.docx

    7/94

    }

    Program 5:

    Convert the following mathematical formula into C++ expressions and evaluate it through C++

    program.

    where y a 0.

    #include

    void main ()

    {

    int x,y,z,a,b,A,B,C,D,M;

    coutx;

    cout

  • 7/16/2019 assignment final.docx

    8/94

    cout

  • 7/16/2019 assignment final.docx

    9/94

    Program 6:

    Suppose you want to verify the interest calculation used by your bank on a loan. The interest rate is

    15.5 percent, stored as .155 in a floating-point variable. The amount of interest, you owe is computed,

    by multiplying the interest rate by the amount of the loan balance, then multiplying that by the

    number of days in the year since the loan originated. The following program finds the daily interest

    rate by dividing the annual interest rate by 365, the number of days in a year. C++ must convert the

    integer 365 to a floating-point literal automatically, because it is used in combination with a floating-

    point variable.

    #include

    void main ()

    {

    int days;

    float principle,daily_intrest,intrest_rate=0.155;

    coutdays;

    cout

  • 7/16/2019 assignment final.docx

    10/94

    daily_intrest=intrest_rate/365;

    daily_intrest=principle*daily_intrest*days;

    principle=principle+daily_intrest;

    cout

  • 7/16/2019 assignment final.docx

    11/94

    coutcal;

    coutch2;

    couteng;

    coutch3;

    coutbe;

    coutch4;

    coutisl;

    coutch5;

    ttl_earned=(itc*ch1)+(cal*ch2)+(eng*ch3)+(be*ch4)+(isl*ch5);

    ttl_credit_hours=(ch1+ch2+ch3+ch4+ch5)/5;

    CGPA=ttl_earned/ttl_credit_hours/100;

    cout

  • 7/16/2019 assignment final.docx

    12/94

    }

    Program 8:

    Assume that a video store employee works 50 hours. He is paid $4.50 for the first 40 hours, time-and-

    a-half (1.5 times the regular pay rate) for the first five hours over 40, and double-time pay for all hours

    over 45. Assuming a 28 percent tax rate, write a program that prints his gross pay, taxes, and net pay

    to the screen. Label each amount with appropriate titles (using string literals) and add appropriate

    comments in the program.

    #include

    void main ()

    {

    double tax,net_pay,gross_pay,A,B,C;

    A=4.50;

    cout

  • 7/16/2019 assignment final.docx

    13/94

    }

  • 7/16/2019 assignment final.docx

    14/94

    Week 6 Lab 02You have written numerous pseudo codes and drawn different flow charts to solve problems

    in previous lab sessions. Now implement those solutions in C++ programs. There are 5

    different problems (below) and you must write the solutions in one C++ file.

    Program#1

    Obtain two numbers, from user through keyboard, and determine which number (first orsecond) is greater. Result should be displayed with proper message as shown in expected

    output table below.

    #include

    void main ()

    {

    int a,b;

    couta;

    coutb;

    if (a

  • 7/16/2019 assignment final.docx

    15/94

    }

    }

    Program#2

    Write a program that takes two integers, from user through keyboard, and determines ifthe first number is a multiple of the second number. (Hint: assignment operator will be

    used in this program)

    #include

    void main ()

    {

    int a,b,c;

    couta;

    coutb;

    c=a%b;

    if (c==0)

    {

    cout

  • 7/16/2019 assignment final.docx

    16/94

    else

    {

    cout

  • 7/16/2019 assignment final.docx

    17/94

    {

    cout

  • 7/16/2019 assignment final.docx

    18/94

    cout

  • 7/16/2019 assignment final.docx

    19/94

    cin>> CGPA;

    if (CGPA>=3.0||warningCount==0)

    {

    cout

  • 7/16/2019 assignment final.docx

    20/94

    {

    cout

  • 7/16/2019 assignment final.docx

    21/94

    Program #7

    Write a C++ program thattakes the year of birth as input, from the user, and calculates the

    age. If the age is between 15 and 20 then the user can take the testotherwise user cannot

    take any test. Furthermore, if the age is between 15 and 17 then display user can take test

    A. If the age is between 18 and 20 then displays user can take testB.

    #include

    void main()

    {

    int year,age;

    coutyear;

    age=2013-year;

    if(age>=15&&age

  • 7/16/2019 assignment final.docx

    22/94

    Program # 8

    Write a C++ Program that takes input, from user through keyboard, as single character (a-z)

    and determine whether the given character is vowel or not. (Hint: Write this program using

    logical and relational operators. The data type is char).

    #include

    void main ()

    {

    char z;

    cout

  • 7/16/2019 assignment final.docx

    23/94

    cin>>z;

    switch(z)

    {

    case 'a':cout

  • 7/16/2019 assignment final.docx

    24/94

    WEEK 7 Lab 1Program 1:

    Write a C++ program to allow the user to input his/her age. Then the program will show if the person

    is eligible or not eligible to vote. A person who is eligible to vote must be older than or equal to 18

    years old.

    #include

    void main ()

    {

    int age;

    cout

  • 7/16/2019 assignment final.docx

    25/94

    {

    cout

  • 7/16/2019 assignment final.docx

    26/94

    cin>>a;

    if (a>5||8

  • 7/16/2019 assignment final.docx

    27/94

    void main ()

    {

    int a;

    cout

  • 7/16/2019 assignment final.docx

    28/94

    }

    Program 4:

    Write a C++ program to take two integers from user and determine/print the relationship between

    given numbers. The relationship should be measured as follows:

    If the first number is less than the second number

    If the first number is equal to second number

    If the first number is greater than second number

    #include

    void main ()

    {

  • 7/16/2019 assignment final.docx

    29/94

    int a,b;

    cout

  • 7/16/2019 assignment final.docx

    30/94

    Program 5:

    Develop a C++ program that will determine if a department stores customer has exceeded the credit

    limit on a charge account. For each customer, take input of the following facts:

    o Account numbero Previous balance at the beginning of the montho Total of all items charged by this customer this month

    #include

    void main ()

    {

    int acc,beg_bal,tchrg,tcrdt,crdt_lmt,nw_bal;

    cout

  • 7/16/2019 assignment final.docx

    31/94

    cout

  • 7/16/2019 assignment final.docx

    32/94

    Program 6:

    Suppose you are an amusements park ride operator. Rides often come with restrictions on who can

    and cannot ride, usually based on height and weight. If the height of the individual is less than or

    equal to 54 inches or the weight of the individual is less than 60 pounds, he or she cannot ride. If the

    height of the individual is greater than 84inches or his/her weight is greater than 300pounds, he or

    she cannot ride. Otherwise (if the rider does not match with above mentioned conditions), he/she can

    ride. Write a C++ program, which prompts the user for his/her height and weight (both integers) and

    outputs a descriptive message indicating their eligibility to ride.

    # include

    void main ()

    {

    int h,w;

    cout

  • 7/16/2019 assignment final.docx

    33/94

    cin>>w;

    if (h

  • 7/16/2019 assignment final.docx

    34/94

    Program 7:

    Write a C++ program for auto insurance agency. This program should determine the cost of an

    automobile insurance premium, based on driver's age and the number of accidents that the driver has

    had. The basic insurance surcharge is $500. There is a surcharge of $100 if the driver is under 25.

    Consider the following table in order to calculate the surcharge for accidents:

    # of accidents Accident Surcharge

    1 50

    2 125

    3 225

    4 375

    5 575

    6 or more No insurance

    #include

  • 7/16/2019 assignment final.docx

    35/94

    void main ()

    {

    int age,no_acci,i_prem,basic_in=500,s_chrg=100,a,b;

    cout

  • 7/16/2019 assignment final.docx

    36/94

    if(no_acci==3)

    {

    b=a+225;

    cout

  • 7/16/2019 assignment final.docx

    37/94

    }

    }

    }

    else

    {

    if (no_acci==1)

    {

    b=a+50;

    cout

  • 7/16/2019 assignment final.docx

    38/94

    else

    {

    if (no_acci==4)

    {

    b=a+375;

    cout

  • 7/16/2019 assignment final.docx

    39/94

  • 7/16/2019 assignment final.docx

    40/94

    Week 7 Lab 02

    Program 1:

    (Dangling Else Problem) Modify the following code to produce the output as shown below.

    You may not make any changes other than inserting braces at proper places. We eliminated

    the indentation from the following code to make the problem more challenging.

    *Note: Its possible that no modification is necessary.]

    if( y == 8 )

    if( x == 5 )

    printf( "@@@@@\n" );

    else

    printf( "#####\n" );

    printf( "$$$$$\n" );

    printf( "&&&&&\n" );

    #include

    void main ()

    {

    int x,y;coutx;

    couty;

    if( y == 8 )

    {

    if( x == 5 )

    {

    cout

  • 7/16/2019 assignment final.docx

    41/94

    }

    }

    Program 2:

    Write a C++ program that reads the marks obtained by a student in a test (maximum 100

    marks) and computes his grade according to the following criteria.

    Marks>=80 -->grade=A

    Marks>=70 &grade=B

    Marks>=60 & grade=C

    Marks>=50 & grade=D

    Otherwise grade=F

    #include void main ()

    {

    int marks;

    coutmarks;

    if (marks>=80)

  • 7/16/2019 assignment final.docx

    42/94

    {

    cout

  • 7/16/2019 assignment final.docx

    43/94

    Program 3:

    Consider the following statement.

    if age >minAgethen

    if income >minIncomethen

    put "Accept"

    else

    put "Reject"

    endif

    endif What will the statement print, if

    age >minAge and income

  • 7/16/2019 assignment final.docx

    44/94

    cin>>income;

    coutminAge;

    if (age>minAge)

    {

    if (income>minIncome)

    {

    cout

  • 7/16/2019 assignment final.docx

    45/94

    For all those students, who satisfy the condition for eligibility of TA (cgpa and gpa is greater

    than 3), There are four types of TA, offered to a student according to following criteria.

    - Full time TA if he/she is student of MS.

    - Part time TA if he/she is student of BS.

    The program takes input, the type of degree in a variable of type char, where M is for MS

    student and B represents BS student.

    #include

    void main ()

    {

    float gpa,cgpa;

    char degree,M,B;

    coutgpa;

    coutcgpa;

    coutdegree;

    if (gpa>3&&cgpa>3)

    {

    cout

  • 7/16/2019 assignment final.docx

    46/94

    if (degree=='B')

    {

    cout

  • 7/16/2019 assignment final.docx

    47/94

    Code I: first 4 hundred gallons: $1500

    $0.125 for each additional gallon used

    #include

    void main()

    {

    float an,g,r,rr,rrr,rrrr;

    char c;

    coutan;

    coutc;

    coutg;

    if(c=='h')

    {

    if(g>100)

    {

    r=g-100;

    rr=r*0.005;

    rrrr=r+rr;

    cout

  • 7/16/2019 assignment final.docx

    48/94

    {

    cout

  • 7/16/2019 assignment final.docx

    49/94

    if(c=='i')

    {

    if(g>400)

    {

    r=g-400;

    rr=r*0.125;

    rrr=r+rr;

    cout

  • 7/16/2019 assignment final.docx

    50/94

  • 7/16/2019 assignment final.docx

    51/94

    WEEK 8 LAB 1Program 1:

    Write a C++ program that:

    1. Declare two variables. Initialize the second variable to be 1000.2. Gets a value of the first variable from the user.3. If the first variable is 0, the program increments the first variable.4. If the first variable is 1, the program decrements the first variable and increments the

    second variable.

    5. If the first variable is -100, the program increments the first variable and decrementsthe second integer.

    6. In all other cases, the program sets both variables to 0.7. Print the final result (both variable) on screen.

    #include

    void main ()

    {

    cout

  • 7/16/2019 assignment final.docx

    52/94

    cout

  • 7/16/2019 assignment final.docx

    53/94

    }

    Program 2:

    Write a nested if statement to print the appropriate activity depending on the value of a

    variable (temperature and humidity) as in the table below: Assume that the temperature can

    only be warm and cold, and the humidity can only be dry and humid.

    If temperature is if humidity is Print this activity

    Warm Dry "Play tennis"

    Warm Humid "Swim"

    Cold Dry "Play basketball"

    Cold Humid "Watch TV"

    #include

    void main ()

  • 7/16/2019 assignment final.docx

    54/94

    {

    cout

  • 7/16/2019 assignment final.docx

    55/94

    else if (temp==2&&humid==4)

    {

    cout

  • 7/16/2019 assignment final.docx

    56/94

    In all other cases the driver is not insured. If the marital status, sex and age of the driver are

    the inputs, write a program to determine whether the driver is to be insured or not. Here

    after checking a complicated set of instructions the final output of the program would be one

    of the twoEither the driver should be ensured or the driver should not be ensured.

    #include

    void main ()

    {

    cout

  • 7/16/2019 assignment final.docx

    57/94

    if(gender==1&&age>=30)

    {

    cout

  • 7/16/2019 assignment final.docx

    58/94

    Program 4:

    In a company, worker efficiency is determined on the basis of the time required for a worker

    to complete a particular job. If the time taken by the worker is between 2 3 hours, then the

    worker is said to be highly efficient. If the time required by the worker is between 3 4

    hours, then the worker is ordered to improve speed. If the time taken is between 4 5 hours,

    the worker is given training to improve his speed, and if the time taken by the worker is morethan 5 hours, then the worker hasto leave the company. If the time taken by the worker is

    input through the keyboard, find the efficiency of the worker.

    #include

    void main ()

    {

    cout

  • 7/16/2019 assignment final.docx

    59/94

    cout

  • 7/16/2019 assignment final.docx

    60/94

    Program 5:

    Write a program using SWITCH structure to determine the total weight of chocolates being

    sold. The program takes as input, the number of chocolates being sold, weight of one

    chocolate in ounces and the choice of weighing i.e. ounces, pounds, grams or kilograms. User

    enters O as a choice to calculate weight in ounces, P for pounds, G for grams and K for

    kilograms. Depending on the choice entered by the user the program calculates the weight ofchocolates. Display an appropriate message if invalid choice is entered. Use the following

    formulas to calculate total weight of chocolates.

    For weighing in Ounces

    total_weight = number_of_chocolates * weight_of_Chocolate

    For weighing in Pounds

    total_weight = number_of_chocolates * weight_of_Chocolate / 16

    For weighing in Grams

    total_weight = number_of_chocolates*weight_of_Chocolate * 28.349

    For weighing in Kilograms

    total_weight = number_of_chocolates*weight_of_Chocolate*28.349/1000;

  • 7/16/2019 assignment final.docx

    61/94

    #include

    void main ()

    {

    cout

  • 7/16/2019 assignment final.docx

    62/94

    }

    }

    Program 6:

    In the Chinese calendar, every year is associated with a particular animal. The 12-year animal

    cycle is rat, ox, tiger, rabbit, dragon, snake, horse, goat, monkey, rooster, dog, and boar. The

    year 1900 is a year of the rat; thus 1901 is a year of the ox and 1912 is another year of the rat.

    If you know in what year a person was born, you can compute the offset from 1900 and

    determine the animal associated with that persons year of birth. (! First, think of the

    formula)

    Create a program using SWITCH structure that determines the animal corresponding to an

    input year of birth. The program should take as input the year of birth in a variable of typeint. The program should then display the corresponding animal.

    #include

  • 7/16/2019 assignment final.docx

    63/94

    void main ()

    {

    cout

  • 7/16/2019 assignment final.docx

    64/94

    case 11:cout

  • 7/16/2019 assignment final.docx

    65/94

    #include

    void main ()

    {

    cout

  • 7/16/2019 assignment final.docx

    66/94

    };break;

    case 2:cout

  • 7/16/2019 assignment final.docx

    67/94

  • 7/16/2019 assignment final.docx

    68/94

    WEEK 8 LAB 2

    Program 1:

    Write the answers on the answer sheet:

    1) After execution of the following code, what is stored in num?

    (All variables are of type int.)

    Hint: use i=3, j=5, and k=7 as sample values.

    if (j > k)

    if (i > j)

    num = i;

    else

    num = j;

    else

    if (i > k)

    num = i;

    else

    num = k;

    a. 3

    b. 5

    c. 7e. 9

    #include

    void main ()

    {

    cout

  • 7/16/2019 assignment final.docx

    69/94

    cout

  • 7/16/2019 assignment final.docx

    70/94

    num = k;

    cout

  • 7/16/2019 assignment final.docx

    71/94

    {

    y = y + 5;

    cout

  • 7/16/2019 assignment final.docx

    72/94

    a. 1

    b. 2

    c. 3

    d. 5

    e. 8

    #include

    void main ()

    {

    cout

  • 7/16/2019 assignment final.docx

    73/94

    {

    angle = angle + 3;

    cout

  • 7/16/2019 assignment final.docx

    74/94

    Program 3:

    1) After execution of the following code, what will be the value of angle?

    int angle = 13;

    if (angle

  • 7/16/2019 assignment final.docx

    75/94

    {

    cout

  • 7/16/2019 assignment final.docx

    76/94

    cout

  • 7/16/2019 assignment final.docx

    77/94

    WEEK 9 LAB 1Program 1:

    While loop that is counting

    Type the given code and observe the output

    1. #include 2. int main()3. {4. int counter = 0; // initialize the condition5. while(counter < 5) // test condition still true6. {7. counter++; // body of the loop8. cout

  • 7/16/2019 assignment final.docx

    78/94

    Program 2:

    Type the given code and observe the output

    1. #include2. main()3. {4. int x = 1;5.while (x

  • 7/16/2019 assignment final.docx

    79/94

    {

    cout age_user;6.cout > age_friend;8.cout

  • 7/16/2019 assignment final.docx

    80/94

    # include

    void main ()

    {

    int age_user,age_friend;

    char loop_response = 'y';

    while (loop_response == 'y')

    {

    cout > age_user;

    cout > age_friend;

    cout

  • 7/16/2019 assignment final.docx

    81/94

    cin >> loop_response;

    }

    }

    Program 4:

    Write a program using while loop to repeatedly print the value of the variable xValue, decreasing it

    by 0.5 each time, as long as xValue remains positive.

    #include

    void main ()

    {

    float a;

    cout

  • 7/16/2019 assignment final.docx

    82/94

    while (a>0)

    {

    a=a-0.5;

    cout

  • 7/16/2019 assignment final.docx

    83/94

    cout

  • 7/16/2019 assignment final.docx

    84/94

    #include

    void main()

    {

    int c,a=1,b;

    cout

  • 7/16/2019 assignment final.docx

    85/94

    program 7:

    Write a program to print the multiplication table of the number entered by the user.

    # include

  • 7/16/2019 assignment final.docx

    86/94

    void main ()

    {

    int a,b=1;

    cout

  • 7/16/2019 assignment final.docx

    87/94

    WEEK 9 LAB 2Program 1:Write the statements, including a While loop, required to calculate y (t) from theequation:

    3t2 +5 t 0

    3t2 +5 t

  • 7/16/2019 assignment final.docx

    88/94

    cout

  • 7/16/2019 assignment final.docx

    89/94

    Program 2:

    Write a program that accepts numbers from the user and computes their sum. The numbers should

    be entered one at a time and the output of all the numbers entered thus far should then be displayed.

    The program should halt when the user enters the number m 2:

    #include

    void main ()

    {

    cout

  • 7/16/2019 assignment final.docx

    90/94

    }

    cout

  • 7/16/2019 assignment final.docx

    91/94

    void main ()

    {

    cout

  • 7/16/2019 assignment final.docx

    92/94

    while(num1

  • 7/16/2019 assignment final.docx

    93/94

    Program 4:

    Write a program that takes as input time expressed in 2400 format (e.g. 23:05 hours), convert it to

    1200 hours format (e.g. 11:05 p.m) and display the new time on the screen. Your program should

    repeat the processing until a sentinel time of -99 is entered.

  • 7/16/2019 assignment final.docx

    94/94