0% found this document useful (0 votes)
182 views9 pages

Yousaf Esa Lab Report 3 ME-41 C

The document contains 6 tasks from a lab assignment on expressions, type casting, and formatting in C++, with the student providing code samples and output for each task, which includes expressions, automatic type conversions, random number generation, and error checking code samples.

Uploaded by

Tallal Rajpoot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
182 views9 pages

Yousaf Esa Lab Report 3 ME-41 C

The document contains 6 tasks from a lab assignment on expressions, type casting, and formatting in C++, with the student providing code samples and output for each task, which includes expressions, automatic type conversions, random number generation, and error checking code samples.

Uploaded by

Tallal Rajpoot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

NUST COLLEGE OF ELECTRICAL & MECHANICAL

ENGINEERING

LAB #03:

Name:Muhammad Yousaf Esa


Reg #: 00000296216
Lab Objective:
Expressions, type casting, coercion, formatting, random numbers.
LAB:
Task 1:
Assume that the following variables are defined:
int age;
double pay;
char section;
Write a single cin statement that will read input into each of these variables.
Code:
#include<iostream>
using namespace std;
int main()
{
int age;
double pay;
char section;
cout << "put your values" << '\n';
cin >> age >> pay >> section;
cout << "My age is=" << age << "My pay is=" << pay << "My section is=" << section
<< '\n';

system("pause");
return 0;
}
Output:
Task 2:
Complete the following table by writing the value of each expression in the Value column
according C++ language rules.

Table:
Expressions Values
28/4–2 5
6+12*2–8 22
4+8*2 20
6+17%3–2 6
2+22*(9-7) 46
(8+7)*2 30
(16+7)%2–1 0
12/(10-6) 3
(19-3)*(2+2)/4 16

Task 3:
Assume a program has the following variable definitions:
int units;
float mass;
double weight;
weight = mass * units;
Which automatic data type conversion will take place?
A. mass is demoted to an int, units remains an int, and the result of mass * units is an int.
B. units is promoted to a float, mass remains a float, and the result of mass * units is a float.
C. units is promoted to a float, mass remains a float, and the result of mass * units is a
double.
Code:
#include<iostream>
using namespace std;
int main()
{
int units;
float mass;
double weight;
cout << "Mass demoted in int" << '\n';
cin >> mass;
cout << "units in int" << '\n';
cin >> units;
weight = mass*units;
cout << "result of mass*unit=" << weight << '\n';
system("pause");
return 0;
}
Output:

Task 4:
Assume a program has the following variable definitions:
int a, b = 2; float c = 4.2;
and the following statement: a = b * c;
What value will be stored in a?
A. 8.4 B. 8 C. 0 D. None of the above
Code:
#include<iostream>
using namespace std;
int main()
{
int a, b = 2;
float c = 4.2;
a = b*c;
cout << "Stored Value of a=" << a << '\n';
system("pause");
return 0;
}
Output:
Task 5:
Assume that qty and salesReps are both integers. Use a type cast expression to rewrite the
following statement so it will no longer perform integer division.
unitsEach = qty / salesReps;
Code:
#include<iostream>
using namespace std;
int main()
{
int qty;
int salesReps;
cout << "Put value of qty" << "\n";
cin >> qty;
cout << "Put value of salesReps" << '\n';
cin >> salesReps;
float a = qty;
float b = salesReps;
float x=a/b;
cout << "Value of unitsEach=" << x << '\n';
system("pause");
return 0;
}
Output:
Task 6:
Math Tutor: (hint rand())
Write a program that can be used as a math tutor for a young student. The program should
display two random numbers to be added, such as
247
+ 129
---------
The program should then pause while the student works on the problem. When the student
is ready to check the answer, he or she can press a key and the program will display the
correct solution:
247
+ 129
--------
376
Code:
#include<iostream>
using namespace std;
int main()
{
int a = rand() ;
int b = rand() ;
cout << "solve the given problem:" << '\n';
cout <<"a="<< a << '\n';
cout <<"b="<< b << '\n';
float x;
x = a + b;
cout << "your required answer is=" << x << '\n';

system ("pause");
return 0;
}
Output:
Home Tasks 1:
Each of the following programs has some errors. Locate as many as you can.
Program-1
using namespace std;
void main ()
{
double number1, number2, sum;
cout << "Enter a number: ";
Cin << number1;
cout << "Enter another number: ";
cin << number2;
number1 + number2 = sum;
cout "The sum of the two numbers is " << sum
}
Code:
#include <iostream>
using namespace std;
void main()
{
int number1, number2;
cout << "Enter two numbers and I will divide\n";
cin >> number1;
cout << "the first by the second for you.\n";
cin >> number2;
float quotient = number1 / number2;
cout <<"quotient="<< quotient;
system("pause");
}
Output:
Program-2
#include <iostream>
using namespace std;
void main()
{
int number1, number2;
float quotient;
cout << "Enter two numbers and I will divide\n";
cout << "the first by the second for you.\n";
cin >> number1, number2;
quotient = float<static_cast>(number1) / number2;
cout << quotient
}
Code:
#include<iostream>
using namespace std;
void main()
{
double number1, number2;
cout << "Enter a number: ";
cin >> number1;
cout << "Enter another number: ";
cin >> number2;
double sum;
sum = number1 + number2;
cout << "The sum of the two numbers is " << sum<<'\n';
system("pause");
}
Output:
Home Task 2:
Average of Values to get the average of a series of values, you add the values up and then
divide the sum by the number of values. Write a program that stores the following values in
five different variables: 28, 32, 37, 24, and 33. The program should first calculate the sum of
these five variables and store the result in a separate variable named sum. Then, the
program should divide the sum variable by 5 to get the average. Display the average on the
screen.
Code:
#include<iostream>
using namespace std;
int main()
{
int a, b, c, d, e;
cout << "Please enter values of a,b,c,d,e" << '\n';
cin >> a >> b >> c >> d >> e;
float sum;
sum = a + b + c + d + e;
cout << "your sum =" << sum << '\n';
int y;
y = sum / 5;
cout << "Average of numbers =" << y << '\n';
system("pause");
return 0;

Output:

You might also like