Week4 Huayi Li 2011-9-15
1. Summary of Quiz1
2. An example of if-else statement Recall that, given a Quadratic ax 2 + bx + c = 0 if a = 0, then problem reduces to linear equation. x = -c /b if b 0 x is unsolvable if b=0 and c 0 x is arbitrary if b=0 and c=0 If a 0 let = b*b 4*a*c if <0 there are two complex roots ( -b ± sqrt(- ) i) /(2a) if =0 there is one real root -b/(2a) if >0 there is two real roots ( -b ± sqrt( ) ) /(2a) C++ codes can be downloaded from my webpage http://www.cs.uic.edu/~hli/#tabs-9
If(A) some codes; If(A) { some codes; 3. Some notes on If-else If some codes = 1 (# of statements), then the above two are equivalent, otherwise not. Conclusion: { } can be omitted only when there is only one statement under if or under else
3. Some notes on If-else cont d #include<iostream> using namespace std; intmain(){ boola = false; cout << " \n\nnow a is false see the difference below " <<endl; if(a) cout<<" 1st code statement under if a"<<endl; cout<<" 2nd code statement 'under' if a"<<endl; // this statement is // supposed not to be executed // however no { } are used, the statement is actually outside if(a) system("pause"); }
3. Some notes on If-else cont d else always matches the last if if( A) { if (B){ if(c){ else{ // which if this else matches? --- if(c) else{ // which if this else matches? --- if(a)
4. SimpleFunction // simple function, no parameters (input), no return value (output) // try to debug it #include<iostream> using namespace std; void f(){ cout<<"f() is called\n"; void g(){ cout<<"g() is called\n"; void h(){ f(); g(); intmain(){ f(); h(); system("pause");
5. Local variables and global variables #include<iostream> using namespace std; intglobala= 1; void func(){ cout<<"locala="<<locala<<endl; //error main.localais invisible to func() cout<<"globala="<<globala<<endl; intmain(){ intlocala= 2; cout<<"locala="<<locala<<endl; cout<<"globala="<<globala<<endl; func(); system("pause");
6. Incremental Programming on HW2
6.1 Version 2.1 #include<iostream> using namespace std; intmain(){ intopt; cout <<"====================Menu======================="<<endl; cout << "1. SSS Problem " << endl; cout << "2. ASA Problem For Single Triangle " << endl; cout << "3. ASA Problem For Multiple Triangles" << endl; cout << "4. SAS Problem " << endl; cout << "choose the option to proceed "<<endl; cin >> opt; switch(opt){ case 1: cout<<"solving SSS"<<endl; break; case 2: cout<<"solving ASA For Single Triangle"<<endl; break; case 3: cout<<"solving ASA For Multiple Triangles"<<endl; break; case 4: cout<<"solving SAS"<<endl; break; system("pause");
6.2 version2.2 // version 2.2 // add while loop to allow user use it for whatever times he wants #include<iostream> using namespace std; intmain(){ while(true){ // new codes for while int opt; cout <<"====================Menu======================="<<endl; cout << "1. SSS Problem " << endl; cout << "2. ASA Problem For Single Triangle " << endl; cout << "3. ASA Problem For Multiple Triangles" << endl; cout << "4. SAS Problem " << endl; cout << "5. Exit " << endl; // new codes for menu 5 cout << "choose the option to proceed "<<endl; cin >> opt; switch(opt){ case 1: cout<<"solving SSS"<<endl; break; case 2: cout<<"solving ASA For Single Triangle"<<endl; break; case 3: cout<<"solving ASA For Multiple Triangles"<<endl; break; case 4: cout<<"solving SAS"<<endl; break; case 5: system("pause"); return 0 ; break; // new codes for case 5 system("pause"); return 0;
// version 2.3 // 2.1 -> 2.2 add while loop to allow user use it for whatever times he wants // 2.2 -> 2.3 add system clear to let user have a better view #include<iostream> using namespace std; intmain(){ while(true){ intopt; cout <<"====================Menu======== ==============="<<endl; cout << "1. SSS Problem " << endl; cout << "2. ASA Problem For Single Triangle " << endl; cout << "3. ASA Problem For Multiple Triangles" << endl; cout << "4. SAS Problem " << endl; cout << "5. Exit " << endl; 6.3 version2.3 cout << "choose the option to proceed "<<endl; cin >> opt; switch(opt){ case 1: cout<<"solving SSS"<<endl; break; case 2: cout<<"solving ASA For Single Triangle"<<endl; break; case 3: cout<<"solving ASA For Multiple Triangles"<<endl; break; case 4: cout<<"solving SAS"<<endl; break; case 5: system("pause"); return 0 ; break; system("pause"); // new codes system("cls"); // new codes system("pause"); return 0;
// version 2.4 // 2.1 -> 2.2 add while loop to allow user use it for whatever times he wants // 2.2 -> 2.3 add system clear to let user have a better view // 2.3 -> 2.4 use function to ease our maintenance and readability #include<iostream> using namespace std; void printmenu(){ // new codes defining a printmenufunction cout <<"====================Menu======================="<<endl; cout<< "1. SSS Problem " << endl; cout<< "2. ASA Problem For Single Triangle " << endl; cout<< "3. ASA Problem For Multiple Triangles" << endl; cout<< "4. SAS Problem " << endl; cout << "5. Exit " << endl; cout<< "choose the option to proceed "<<endl; } void SSS(){ // new codes defining a Function solving SSS problem cout<<"solving SSS"<<endl; } void ASASingle(){ // new codes defining a Function solving ASASingle problem cout<<"solving ASA For Single Triangle"<<endl; } void ASAMultiple(){ // new codes defining a Function solving ASAMultipleproblem cout<<"solving ASA For Multiple Triangles"<<endl; } void SAS(){ cout<<"solving SAS"<<endl; } 6.4 version 2.4 intmain(){ while(true){ intopt; printmenu(); // replaced with newcodes cin >> opt; switch(opt){ case 1: SSS(); break; case 2: ASASingle(); break; case 3: ASAMultiple(); break case 4: SAS(); break; case 5: system("pause"); return 0 ; // switch system("pause"); system("cls"); //while system("pause"); return 0; }// main
6.5 version 2.5.. double side1,side2,side3 ; // global variabelssuch that all functions have access to them double angle1, angle2,angle3;. void SSS(){ // real codes here void ASASingle(){ // real codes here void ASAMultiple(){ // real codes here void SAS(){ // real codes here intmain(){. }