Download presentation
Presentation is loading. Please wait.
Published byDamon Cook Modified over 9 years ago
1
CS114-009 - Class 07 Topics – When software goes wrong Count controlled loops Sentential controlled loops putting it all together Announcements Programming Project 1 due Thurs. by midnight (e-mail to vrbsky@cs.ua.edu) vrbsky@cs.ua.edu Exam 1 – New date?? Read pages 143-177 (145-179) for next time
2
FYI iostream is a header file using for input/output in C++ that is part of the C++ standard library Collection of classes, constants, functions, objects and templates If you didn’t have using namespace std You would need to add std:: in front of cout to tell it to use the std (standard) library e.g. std::cout << “hello” << endl; more than on library can use same name
3
When software goes wrong… What does software do? Payrolls, bills, businesses… Phones & other ‘net’ systems Elevator controls Anti-lock brake controllers Insulin pumps, heart pacemakers X-ray therapy equipment Aircraft control (“fly by wire”) What happens when software fails? Bills and paychecks wrong When phones die… Elevators… Auto brakes… Insulin pumps… X-ray machines with bad software have killed Therac-25 (100 times dose) Aircraft Commercial passenger planes have crashed
4
How important is it for software to work? If software doesn’t work, Things go wrong, Businesses fail, People lose their jobs, People are injured, People even die… Many people have died!
5
How can we make software work? Careful planning: algorithms, good teaming skills, good communication Teaching those skills Making sure students know what we’re teaching.
6
Class exercises Write a C++ program that reads in five integers and prints out their product (the result of multiplying them together) Example 1 2 3 4 5, output 120 10 11 12 13 0, output 0 -2 -1 1 2 3, output 12 Write a C++ program that reads in two integers and prints out all the numbers from the first to the second The first value read will always be less than (or equal to) the second Example 8 13 Output: 8 9 10 11 12 13 Important: Figure out your algorithm (on paper) before you start to code these problems.
7
Count-controlled loops You know exactly how many times the loop will execute Have this value in a variable Example int main( ) { int num, count = 0, sum = 0; while (count < 10) { cout << “Enter a num: ”; cin >> num; sum = sum + num; count = count + 1; } cout << “The sum is :” << sum << endl; return 0; }
8
Don’t know when the data will stop, you just know the last value. Sentinel-controlled loops (input) Read data until input is –1 int main( ) { int sum=0, data; cout<< “Enter data (-1 to quit)” << endl; cin >> data; while (data != -1) { sum = sum + data; cin >> data; } cout << “Sum is: “ << sum << endl; return 0; } –1
9
Class exercise Write a C++ program that reads in a sequence of 1’s and 0’s, stopping when the value 2 is read, and then prints out the value that occurred the most often. Consider the algorithm: This is only a minor change from the previous example. What needs to change?
10
Tracing – What is the output? #include using namespace std; int main ( ) { int k = 0, num = 1; while (k < 4) { cout << num << endl; k = k + 1; num = num * 10; } cout << "final num = " << num << endl; return 0; }
11
Tracing What is output by the program below? #include using namespace std; int main ( ) { int k = 0, num = 1; while (k < 4) { cout << num << endl; k = k + 1; num = num * 10; } cout << "final num = " << num << endl; return 0; } knumOutput 011 110 2100 31000 410000 final num = 10000
12
More tracing #include using namespace std; int main( ) { int a= 5, b = 0; while (a > 0) { cout << a << “ “; cout << b << endl; b = b + a; a = a – 1; } cout << b << endl; return 0; } Variables Output AB
13
Class exercises (do at least two) Write a C++ program that reads in 20 integers, counts the number of odd and even numbers, and prints out the number of odd and even numbers Hint: what does num%2 do? Write a C++ program that reads in 10 integers and prints out the largest and smallest values seen Write a C++ program that reads in 20 characters (you will only see vowels in the input – a, e, i, o, and u) and prints out the vowel that occurred the most often Important: figure out your algorithm before you start to code these problems
14
End of Class 07 Read pages 143-177 for next time If you have not yet read Chapters 1, 2 & 3 you need to do it now.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.