0% found this document useful (0 votes)
12 views7 pages

Codes

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

ً‫ دمحم اىزمي‬.

‫ – د‬2222 ‫جامعت اىقاهزة – مييت اىحاسباث و اىمعيىماث اىفزقت األوىً – اىبزمجت اىهينييت‬
2222 ‫ مايى‬22 ‫) ىيتذرب عيً اهم أخطاء اىبزمجت بيغت سً بيس بيس (اختياري) – اىتسييم‬1( ‫مسائو اضافيت‬
Faculty of Computers and Artificial Intelligence – CS112 / 2022 – Structured Programming
Extra Problems (1) to Train on C++ Program Errors (Optional) – Deadline 20 May 2022

‫ قم بتشغيو‬، ‫جميع اىبزامج اىمزفقت تعمو ىنه فيها مشنيت مما فً اىىصف حاوه امتشاف اىمشنيت فً مو مه اىبزامج اىتاىيت‬
‫ بزوامج اىتاىيت ستجذ أوها تعمو ىنه مو مىها ال يؤدي اىمطيىب مما يىبغً وعييل‬11 ‫وسخت اىبزوامج اىمزفقت ىنو مه اىـ‬
.‫امتشاف اىمشنيت و حيها‬
‫ مجمىعاث يحيىن اىمسائو ميها وحذهم و يقذمىن اىىتيجت ىيذمتىر‬12 ‫ و تقذيم اىحو ىً و اوه‬2 ‫قم باىعمو فً مجمىعاث مه‬
ً‫ يزفع اىحو عيً مىصت بالك بىرد فً صىرة ميف مضغىط يحىي تقزيز ب‬، ‫ بامى معمىه باىعجىة‬2 ً‫اىزميً يحصيىن عي‬
.‫دي اف يصف خطأ مو بزوامج مع بزوامج صحيح يعمو مقابو ىنو مه هذي اىبزامج بعذ تصحيح اىخطأ‬
All included programs have an error and although they work, they do not do the supposed task correctly.
Work in groups of 2 to find the errors and upload your solution in a zip file with a pdf report of the errors
and a correct version of each program in Blackboard. Prizes are given for first 10 correct solutions.

1. This program is supposed produce a list of the squares of the numbers from 1 to 10. It does
produce a list of squares, but this is not what the programmer expected.
1 /************************************************
2 * Print out the square of the numbers *
3 * from 1 to 10 *
4 ************************************************/
5 #include <iostream>
6
7 int main()
8 {
9 int index; /* Index into the table */
10
11 for (index = 1; index <= 10; ++index);
12 std::cout << index << " squared " <<
13 (index * index) << '\n';
14
15 return (0);
16 }

2. This program is designed to test to see if two numbers are non-zero. The problem is that the
programmer used a little too much shorthand, and something is going wrong:
1 /************************************************
2 * if_test -- Simple test of the if statement. *
3 ************************************************/
4 #include <iostream>
5
6 int main()
7 {
8 int i1 = 12; // A number
9 int i2 = 3; // Another number
10
11 if (i1 & i2)
12 std::cout << "Both numbers are non-zero\n";
13 else
14 std::cout << "At least one number is zero\n";
15 return (0);
16 }
3. The following program is designed to print out a 3-by-3 matrix. But the results aren't the
elements of the matrix; they are something else instead. What's going on?
1 /************************************************
2 * print_element -- Print an element in a *
3 * matrix. *
4 ************************************************/
5 #include <iostream>
6
7 // A simple matrix
8 int matrix[3][3] = {
9 {11, 12, 13},
10 {21, 22, 23},
11 {31, 32, 33}
12 };
13
14 int main()
15 {
16 std::cout << "Element[1,2] is " <<
17 matrix[1,2] << std::endl;
18 return (0);
19 }

4. A classic mathematical problem is to add the numbers 1 to 100. But this program seems
come up with the wrong answer (correct answer 5050):

1 /************************************************
2 * A program to sum the numbers from 1 to 100 *
3 * using a brute force algorithm. *
4 ************************************************/
5 #include <iostream>
6
7 int main()
8 {
9 int sum; // The running sum
10 int count; // The current number
11
12 for (count = 1; count <= 100; ++count)
13 sum += count;
14
15 std::cout <<
16 "The sum of the numbers " <<
17 "between 1 and 100 is " <<
18 sum << '\n';
19 return (0);
20 }

5. This is a short program to compute and print the squares of the numbers from 1 to 5. It's
simple enough, so what's wrong in the program even if it prints the correct answer?

1 /************************************************
2 * squares -- Print the squares of the numbers *
3 * from 1 to 5. *
4 ************************************************/
5 #include <iostream>
6
7 int main()
8 {
9 // An array for the squares
10 int array[5];
11
12 int i; // Index into the array
14 for (i = 1; i <= 5; ++i) {
15 array[i] = i*i;
16 }
17
18 for (i = 1; i <= 5; ++i) {
19 std::cout << i << " squared is " <<
20 array[i] << '\n';
21 }
22 return (0);
23 }
6. This program computes the area of a triangle. The formula is simple, the program is simple,
and it's clear that everything works. But there is a surprise lurking within this code:
1 /************************************************
2 * triangle -- Compute the area of a triangle *
3 ************************************************/
4 #include <iostream>
5 int main()
6 {
7 int base = 0; /* Base of the triangle */
8 int height = 0; /* Height of the triangle */
9
10 base = 5; /* Set the base of the triangle
11 height = 2; /* Set the height */
12
13 // Area of the triangle
14 int area = (base * height) / 2;
15
16 std::cout << "The area is " <<
17 area << std::endl;
18 return (0);
19 }

7. This is a simple program designed to figure out how many significant digits are used for
floating point. The idea is simple: Pick a nice repeating fraction such 1/3 (0.333333), print it,
and see how many digits you get.
However, the results puzzled this programmer. He knew the computer couldn't be that stupid. So
what happened?
1 /************************************************
2 * divide -- Program to figure out how many *
3 * digits are printed in floating point *
4 * by print 1/3 or 0.333333. *
5 ************************************************/
6 #include <iostream>
7
8 int main()
9 {
10 float result; // Result of the divide
12 result = 1/3; // Assign result something
13
14 std::cout << "Result is " << result << '\n';
15 return (0);
16 }
8. The programmer knows that shifting left is the same as multiplying by a power of two. In
other words:
1
 x << 1 is the same as x * 2 (2 = 2 )
2
 x << 2 is the same as x * 4 (4 = 2 )
3
 x << 3 is the same as x * 8 (8 = 2 )

The programmer uses this trick to quickly perform a simple calculation. But something goes
wrong:

1 /************ *********** ************ **********


2 * Simple syntax testing. *
3 ************************************************/
4 #include <iostream>
5
6 int main(void)
7 {
8 int x,y; // Two numbers
9
10 x = 1;
11
12 y = x<<2 + 1; // x<<2 = 4 so y = 4+1 = 5
13 std::cout << "Y=" << y << std::endl;
14 return (0);
15 }

9. If you are a programmer, you've made the mistake contained in this program. If you're
becoming a programmer, you will make this mistake. And it will drive you nuts until you
figure out what it is.

So what does this program do:

1 /************************************************
2 * Test the logic for a simple accounting *
3 * program. *
4 ************************************************/
5 #include <iostream>
6
7 int main()
8 {
9 // Amount owed (if any) by the user
10 int amount;
11
12 std::cout << "Enter current balance: ";
13 std::cin >> amount;
14
15 if (amount = 0)
16 std::cout << "You owe nothing\n";
17 else
18 std::cout << "You owe " << amount << "\n";
19
20 return (0);
21 }
10. The program is a simple one designed to check the numbers between 2 and 9 to see if they
are prime. The algorithm that is used is a bit simplistic and does its work using the brute
force method, but it looks like it should work. So what really happens?

1 /************************************************
2 * prime -- A very dump program to check to see *
3 * if the numbers 2-9 are prime. *
4 ************************************************/
5 #include <iostream>
6
7 int main()
8 {
9 int i; // Number we are checking
10
11 for (i = 2; i < 10; ++i) {
12 switch(i) {
13 case 2:
14 case 3:
15 case 5:
16 case 7:
17 std::cout << i << " is prime\n";
18 break;
19 defualt:
20 std::cout << i <<
21 " is not prime\n";
22 break;
23 }
24 }
25 return (0);
26 }

11. This program divides two integers. Although it's too simple to fail, it does.

1 /************************************************
2 * Simple divide program. *
3 ************************************************/
4 #include <iostream>
5
6 int main()
7 {
8 int n1, n2; // Two integers
9
10 std::cout << "Enter two integers: ";
11 std::cin >> n1 >> n2;
12
13 if (n2 =! 0)
14 std::cout << "Result is: " <<
15 (n1/n2) << '\n';
16 else
17 std::cout << "Can not divide by zero\n";
18
19 return (0);
20 }
12. This program is supposed to make sure that the width and height don't get too small. It works
for width, but there's a problem with height.

1 /************************************************
2 * Test the logic to limit the width and height *
3 * of a rectangle. *
4 ************************************************/
5 #include <iostream>
6
7 int main()
8 {
9 // The smallest legal value
10 // of width and height
11 const int MIN = 10;
12
13 int width = 5; // Current width
14 int height = 50; // Current height
15
16 if (width < MIN) {
17 std::cout << "Width is too small\n";
18 width = MIN;
19
20 if (height < MIN)
21 std::cout << "Height is too small\n";
22 height = MIN;
23 }
24
25 std::cout << "area(" << width << ", " <<
26 height << ")=" <<
27 (width * height) << '\n';
28 return (0);
29 }

13. The following program should output ABC. What does it really do?

1 /************************************************
2 * Toy program to print three characters. *
3 ************************************************/
4 #include <iostream>
5
6 int main()
7 {
8 //A character to be printed
9 char ch = 'A';
10
11 std::cout << ch; // Output A
12 std::cout << ch+1; // Output B
13 std::cout << ch+2; // Output C
14 std::cout << std::endl;
15 return (0);
16 }
14. We all know that 1 + 1 = 2 and 1 + 1 + 1 = 3. Also 1/3 + 1/3 + 1/3 is 3/3 or 1.
The following computer program demonstrates this. But for some reason it doesn't work. Why?
1 /************************************************
2 * test out basic arithmetic that we learned in *
3 * first grade. *
4 ************************************************/
5 #include <iostream>
6
7 int main()
8 {
9 float third = 1.0 / 3.0; // The value 1/3
10 float one = 1.0; // The value 1
11
12 if ((third+third+third) == one)
13 {
14 std::cout <<
15 "Equal 1 = 1/3 + 1/3 + 1/3\n";
16 }
17 else
18 {
19 std::cout <<
20 "NOT EQUAL 1 != 1/3 + 1/3 + 1/3\n";
21 }
22 return (0);
23 }

15. Are a,b,c in descending order? Does the program agree with you?
1 /************************************************
2 * test to see if three variables are in order. *
3 ************************************************/
4 #include <iostream>
5
6 int main()
7 {
8 int a,b,c; // Three simple variables
9
10 a = 7;
11 b = 5;
12 c = 3;
13
14 // Test to see if they are in order
15 if (a > b > c)
16 std::cout << "a,b,c are in order\n";
17 else
18 std::cout << "a,b,c are mixed up\n";
19 return (o);
20 }

You might also like