0% found this document useful (0 votes)
4 views

Sheet 7 Answer

Uploaded by

tytya483
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Sheet 7 Answer

Uploaded by

tytya483
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Sheet 7

1. Mark the following statements as true or false.


a. A compiler translates a high-level program into assembly language. T
b. A linker links and loads the object code from main memory into the CPU for
execution. F
c. Development of a C++ program includes six steps. T
d. A program written in a high-level programming language is called a source
program. T
e. The first step in the problem-solving process is to analyze the problem. F

2. What are the differences between machine languages and high-level languages?
Machine languages are lowest-level programming language and are the only
Languages understood by computers without translation.
High-level language is a programming language that allows a programmer to
Write the programs which are independent of a particular type of computer.

3. What is a source program?


A program written in a high-level language.

4. Why do you need a compiler?


To checks the source program for syntax errors and, if no error is found, translates
The program into the equivalent machine language.

5. Why do you need to translate a program written in a high-level language into


machine language?
Because the computer only understands Machine Language

6. Why would you prefer to write a program in a high-level language rather than a
machine language?
Readability of instructions is better than machine language and is easier to read, write, and maintain.

7. What are the advantages of problem analysis and algorithm design over directly
writing a Program in a high-level language?
It is much easier to discover errors in a program.
Make designed program is much easier to follow and modify.
8. Design an algorithm to find the weighted average of four test scores. The four
test scores and their respective weights are given in the following format:
testScore1 weightTestScore1
For example, sample data is as follows:
75 0.20
95 0.35
85 0.15
65 0.30
#include <iostream>
using namespace std;
int main()
{
float s1,s2,s3,s4;
float w1,w2,w3,w4;
cout<<"Enter four test scores"<<endl;
cin>>s1>>s2>>s3>>s4;
cout<<"Enter four weights"<<endl;
cin>>w1>>w2>>w3>>w4;

float weighted_average = (s1*w1)+(s2*w2)+(s3*w3)+(s4*w4);


cout<<" weighted_average = "<<weighted_average<<endl;

return 0;
}

9. Design an algorithm to convert the change given in quarters, dimes, nickels, and
pennies into pennies.
#include <iostream>

using namespace std;


int main()
{

float quarters, dimes, nickels, pinnies;


cout<<"enter four coins"<<endl;
cin>>quarters>> dimes>> nickels>> pinnies;
pinnies = (25*quarters)+(10* dimes)+(5* nickels)+pinnies;
cout<<"total pinnies = "<< pinnies<<endl;

return 0;}
10. Given the radius, in inches, and price of a pizza, design an algorithm to find the
Price of the pizza per square inch.

#include <iostream>
using namespace std;
int main()
{

float radius,price,area;
cout<<"Enter values of radius and price of pizza"<<endl;
cin>>radius>>price;
area = 3.14*radius * radius;
price = price / area;
cout<<"Price of the pizza per square = "<<price<<endl;

return 0;
}

11. To make a profit, the prices of the items sold in a furniture store are marked up by 80%. After marking up
the prices each item is put on sale at a discount of 10%. Design an algorithm to find the selling price of an item
sold at the furniture store. What information do you need to find the selling price?

#include <iostream>
using namespace std;
int main()

int price;
float Markup_price, Selling_price;
cout<<"enter price : ";
cin>>price;
Markup_price = price + (price*0.80);
Selling_price = Markup_price - (Markup_price * 0.10);
cout<<"Selling_price : "<<Selling_price<<endl;

return 0;
}

12. Jason typically uses the Internet to buy various items. If the total cost of the items ordered, at one time, is
$200 or more, then the shipping and handling is free; otherwise, the shipping and handling is $10 per item.
Design an algorithm that prompts Jason to enter the number of items ordered and the price of each item. The
algorithm then outputs the total billing amount. Your algorithm must use a loop (repetition structure) to get the
price of each item. (For simplicity, you may assume that Jason orders no more than five items at a time.)

//Number of Items: five

#include <iostream>
using namespace std;
int main()
{

int count = 1 , x;
cout<<”enter number of products no more than 5 “;
cin>>x;
if(x<=5){
float total_cost = 0 , price;
while (count <= x){
cin>> price;
total_cost = total_cost + price;
count = count + 1;

}
float Total_billing;
if (total_cost >= 200)
Total_billing = total_cost;

else
Total_billing = total_cost + (10*5);
cout<<"Total_billing : "<<Total_billing<<endl;
return 0;
}

13. Suppose that the cost of sending an international fax is calculated as follows: The service charge is $3.00,
$.20 per page for the first 10 pages, and $0.10 for each additional page. Design an algorithm that asks the user
to enter the number of pages to be faxed. The algorithm then uses the number of pages to be faxed to calculate
the amount due.

#include <iostream>
using namespace std;
int main()
{

float pages , cost;


cout<<"Enter number of pages ";
cin>>pages;
if (pages <= 10)
cost = 3 + (0.2 * pages) ;
else
cost = 3 + (0.2 * 10) + (0.1 * (pages - 10));
cout<<"cost : "<<cost<<endl;

return 0;
}

14.
An ATM allows a customer to withdraw a maximum of $500 per day. If a customer withdraws more than
$300, the service charge is 4% of the amount over $300. If the customer does not have sufficient money in the
account, the ATM informs the customer about the insufficient funds and gives the customer the option to
withdraw the money for a service charge of $25.00. If there is no money in the account or if the account
balance is negative, the ATM does not allow the customer to withdraw any money. If the amount to be
withdrawn is greater than $500, the ATM informs the customer about the maximum amount that can be
withdrawn. Write an algorithm that allows the customer to enter the amount to be withdrawn. The algorithm
then checks the total amount in the account, dispenses the money to the customer, and debits the account by
the amount withdrawn and the service charges, if any.
#include <iostream>
using namespace std;
int main()
{

float charge = 0, amount ,availableAmount,x ; // get value from System


cout<<"availableAmount = ";
cin>>availableAmount;
cout<<"amount of mony : ";
cin>>amount;
if (amount > 300)
charge = (amount-300)*0.04;
if (availableAmount < amount){
cout<<"you do not have sufficient funds"<<endl;
cout<<"you can withdraw the money for a service charge of $25.00 , press 1"<<endl;
cin>>x;
if(x==1){

charge = 25;

}
}

if (availableAmount <= 0){


cout<<"not allow to withdraw any money"<<endl;
charge = 0;
}
if (amount > 500){
cout<<"Maximum withdrawal amount is $500"<<endl;
charge = 0;
}
if (charge > 0)
availableAmount = availableAmount - (amount + charge);

cout<<"availableAmount : "<<availableAmount<<endl;
cout<<"amount : "<<amount<<endl;

return 0;
}

15. Youare given a list of students’ names and their test scores. Design an algorithm that does the following: a.
Calculates the average test scores.
b. Determines and prints the names of all the students whose test scores are below the average test score.
c. Determines the highest test score.
d. Prints the names of all the students whose test scores are the same as the highest test score.
(You must divide this problem into subproblems as follows: The first subproblem determines the average
test score. The second subproblem determines and prints the names of all the students whose test scores
are below the average test score. The third subproblem determines the highest test score. The fourth
subproblem prints the names of all the students whose test scores are the same as the highest test score.
The main algorithm combines the solutions of the subproblems.)

#include <iostream>

using namespace std;

int main()

// suppose four student

string names[4] ;

double score [4];

double max = score[0] , sum = 0;

for(int i = 0 ; i<4 ; i++) {

cout<<"Enter Name & Score Student "<<i+1<<endl;

cin>>names[i]>>score[i];

sum = sum + score[i];

double avg = sum / 4;

cout<<"names of all the students whose test scores are below the average test score"<<endl;

for(int i = 0 ; i<4 ; i++) {

if(avg > score[i])

cout<<names[i]<<endl;

if(max <= score[i])

max = score[i];

cout<<"max_score "<<max<<endl;

cout<<"names of all the students whose test scores are the same as the highest test score"<<endl;

for(int i = 0 ; i<4 ; i++){

if(max == score[i])
cout<<names[i]<<endl;

return 0;

You might also like