CS 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 ( to Exam 1 – New date?? Read pages ( ) for next time
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
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
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!
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.
Class exercises Write a C++ program that reads in five integers and prints out their product (the result of multiplying them together) Example , output , output , 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: Important: Figure out your algorithm (on paper) before you start to code these problems.
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; }
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
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?
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; }
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 final num = 10000
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
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
End of Class 07 Read pages for next time If you have not yet read Chapters 1, 2 & 3 you need to do it now.