Lab2a 10 Final

You might also like

You are on page 1of 3

ECCE3258 – Applied Engineering Programming (Section-10) [ Lab-2A ]

[ Name:Ahmed Saleh Alfarsi 115078 ]


In Lab Tasks:

1. Suppose that the input is:


58 23 46 75 98 150 12 176 145 -999
What is the output of the following program?

#include <iostream>
using namespace std; 58 23 46 75 98 150 12 176 145 //input
int main()
{ 8 23 21 0 23 0 12 1 20 //output
int num;
cin >> num;
while (num != -999)
{
cout << num % 25 << " ";
cin >> num;
}
cout << endl;
return 0;
}

2. The following program has more than five syntax errors that prevent it from compiling and/or
running. Identify and correct all such errors.

#include <iostream>
using namespace std;
const int N = 2,137;//incorrect number
main ()//incorrect function
{
int a, b, c, d:// it should be semi column
a := 3;
b = 5;
c = c + d; // d is not declared
N = a + n;//n is not declared
for (i = 3; i <= N; i++)// i is not declared
{
cout << setw(5) << i; //library does not included
i = i + 1; // i is not declared
}
return 0;
}

#include <iostream>
#include <iomanip>
using namespace std;
const int N = 2.137;
int n=0;
int main ()
{
int a, b, c, d;
a := 3;
b = 5;
d=5;
c = c + d;
N = a + n;
for (i = 3; i <= N; i++)
{
cout << setw(5) << i;
ECCE3258 – Applied Engineering Programming (Section-10) [ Lab-2A ]

i = i + 1;
}
return 0;
}

3. Implement a computer solution for the fourth problem in the problem set (P2-4).
1. /***********************************************************************
*******
2.
3. Named : Ahmed Alfarsi
4. ID : 115078
5.
6. ************************************************************************
*******/
7. #include <iostream>
8.
9. using namespace std ;
10.
11.
12.
13. int main()
14. {
15. int a0=0;
16. int num=0;
17.
18. cout << "please enter value of x: " ;
19. cin >> a0;
20. while (a0!=1)
21. {
22. if (a0%2==0)
23. {
24. a0=a0/2;
25.
26. }
27. else
28. {
29. a0=3*a0+1;
30.
31. }
32. cout << a0 << " ";
33. num++;
34. }
35. cout << endl<< "there is " << num << " numbers" <<endl;
36. system("PAUSE");
37. return 0; }
ECCE3258 – Applied Engineering Programming (Section-10) [ Lab-2A ]

Problem set:

P2- Write a program that computes the factorial of a non-zero positive integer number.
1
P2- The total resistance of n resistors in parallel is:
1 n 1
2
=∑
R i=1 𝑅𝑖
Suppose we have a network of two resistors with the values 400 Ω and 200 Ω. Then our
equation would be:
1 1 1
= +
R 400 200
and the total resistance of our two-resistor network is 133.3 Ω.
Write a program to compute the total resistance for any number of parallel resistors.

P2- Your little sister asked you to help her with her multiplication, and you decided to write a
3 program that test her skills. The program will let her input a starting number, such as 5. It
will generate multiplication problems ranging from 5 X 1 to 5 X 10. For each problem, she
will be prompted to enter the correct answer. The program should check her answer and
should not let her advance to the next question until the correct answer is given to the
current answer. Then, she will be prompted if she wants to repeat with different number or
just exist.

P2- Suppose that the first number of a sequence is x, in which x is an integer.


4 Define a0=x;
an+1= an/2 if an is even;
an+1= (3*an+1) if an is odd.
Then, there exists an integer k such that ak=1.
Write a program that prompts the user to input the value of x. The program output the
integer k such that ak=1 and the numbers a0, a1, a2, . . . , ak.
(For example, if x=75, then k =14, and the numbers a0, a1, a2, . . ., a14, respectively, are
75, 226, 113, 340, 170, 85, 256, 128, 64, 32, 16, 8, 4, 2, 1.)
Test your program for the following values of x: 75, 111, 678, 732, 873 2048, and 65535.

P2- Develop a solution to calculate a student’s grade point average (GPA) for one semester.
5 The letter grades should be entered and the grade average printed out. An A is equivalent to
4 grade points, a B is 3 grade points; a C is 2 grade points, a D is 1 grade point, and an F is
zero grade points. For each course the students should enter a letter grade and number of
credits earned with that grade. He should enter number of registered courses first.

P2- Input an integer containing only 0s and 1s (i.e., a "binary" integer) and print its decimal
6 equivalent. Use the modulus and division operators to pick off the "binary" number's digits
one at a time from right to left. Much as in the decimal number system, where the rightmost
digit has a positional value of 1, the next digit left has a positional value of 10, then 100,
then 1000, and so on, in the binary number system the rightmost digit has a positional value
of 1, the next digit left has a positional value of 2, then 4, then 8, and so on. Thus the
decimal number 234 can be interpreted as 2 * 100 + 3 * 10 + 4 * 1. The decimal equivalent
of binary 1101 is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8 or 1 + 0 + 4 + 8, or 13.

You might also like