friday, december 22, 2006 when you have a hammer, everything starts looking like a nail. - abraham...
Post on 19-Dec-2015
214 views
TRANSCRIPT
![Page 1: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/1.jpg)
Friday, December 22, 2006
When you have a hammer, everything starts looking like
a nail.
- Abraham Maslow
![Page 2: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/2.jpg)
break review
for (i=0; i<10; i++){
…
for (j=0; j<100; j++){
…
if(some_cond) break;
}
}
![Page 3: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/3.jpg)
What is wrong here?
for (i=0; i<10; i++){
…
}
if (some_cond) {
…
break;
}
![Page 4: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/4.jpg)
The Switch Statementint main(){ int choice; cout << "Help on:\n\n"; cout << "1. for\n"; cout << "2. if\n"; cout << "3. switch\n\n"; cout << "Enter choice: (1-3): "; cin >> choice;
![Page 5: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/5.jpg)
The Switch Statementswitch(choice) { case 1: cout << "for is C++'s most versatile loop.\n"; break; case 2: cout << "if is C++'s conditional branch statement.\n"; break; case 3: cout << "switch is C++'s multi-way branch statement.\n"; break; default: cout << "You must enter a number between 1 and 3.\n"; } return 0;}
![Page 6: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/6.jpg)
The Switch Statement switch (expression) {
case constant1:statements;break;
case constant2:statements;break;
case constant3:statements;break;
default:statements;
}
![Page 7: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/7.jpg)
The Switch StatementThe switch statement must evaluate to either a character or an integer value (Floating point expressions are not allowed)
No case constants in the same switch can have identical values
Switch can test only for equality
![Page 8: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/8.jpg)
switch(choice) { case 1: cout << "for is C++'s most versatile loop.\n"; break; case 2: cout << "if is C++'s conditional branch statement.\n"; break; case 3: cout << "switch is C++'s multi-way branch statement.\n"; break; default: cout << "You must enter a number between 1 and 3.\n"; } return 0;}
Self Test: Alternate if-else statements?
![Page 9: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/9.jpg)
The Switch Statementint main(){ int i; for(i=0; i<5; i++) { switch(i) { case 0: cout << "less than 1\n"; case 1: cout << "less than 2\n"; case 2: cout << "less than 3\n"; case 3: cout << "less than 4\n"; case 4: cout << "less than 5\n"; } cout << “\n”; } return 0;}
![Page 10: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/10.jpg)
less than 1less than 2less than 3less than 4less than 5
less than 2less than 3less than 4less than 5
less than 3less than 4less than 5
less than 4less than 5
less than 5
![Page 11: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/11.jpg)
Nested if-else BAD CODING STYLEif (code == 10)
cout<<“Too hot - turn off equipment\n”;else{
if (code == 11)cout<<“Caution !\n”;
else{ if (code==13)
cout<<“Turn on fan\n”;else
cout<<“Normal Mode\n”;}
}
![Page 12: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/12.jpg)
The Switch Statementswitch (code){
case 10:cout<<“Too hot - turn off equipment\n”;break;
case 11:cout<<“Caution! recheck in 5 mins\n”;break;
case 13:cout<<“Turn on fan\n”;break;
default:cout<<“Normal Mode\n”;break:
}
![Page 13: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/13.jpg)
The Switch Statementswitch(i) { case 1: case 2: case 3: //do_something_here; break; case 4: //do_something_else_here; break;}
![Page 14: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/14.jpg)
The Switch Statementswitch(op_code) { case ‘N’ : case ‘R’: cout<<“Normal operating range\n”;
break; case ‘M’: cout<<“Maintenance needed\n”;
break; default: cout<<“Error in code value\n”;
break;}
![Page 15: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/15.jpg)
Nested Switchchar ch1, ch2;cin>>ch1>>ch2;switch(ch1) {case 'A':
cout << "This A is part of outer switch\n";switch(ch2) {case 'A':
cout << "This A is part of inner switch\n";break;
case 'B': cout << "This B is part of inner switch\n";
}break;
case 'B': cout << "This B is part of outer switch\n";break;
}
Equivalent statements?
![Page 16: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/16.jpg)
Nested Switchchar ch1, ch2;cin>>ch1>>ch2;switch(ch1) {case 'A':
cout << "This A is part of outer switch\n";switch(ch2) {case 'A':
cout << "This A is part of inner switch\n";break;
case 'B': cout << "This B is part of inner switch\n";
}break;
case 'B': cout << "This B is part of outer switch\n";break;
}
Equivalent statements?
![Page 17: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/17.jpg)
Functions
![Page 18: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/18.jpg)
T1
T2
A/DConverter
A/DConverter
AmplifierDisplaycircuit
Motor Control
AmplifierTemperature
DifferenceAlarm
Td1 Din Dout
Mout
AoutTd2
Tdin
![Page 19: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/19.jpg)
Hardware design Subparts or modules Interface
Software design
![Page 20: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/20.jpg)
int main(){double T1, T2, Td1, Td2, Din, Tdin, Dout, Mout, Aout;cin>>T1>>T2;Td1 = AtoD_Converter(T1);Td2 = AtoD_Converter(T2);Din = Amplifier(Td1);Tdin = Amplifier(Td2);Dout = DisplayCircuit(Din);Mout = MotorControl(Din);Aout = TemperatureAlarm(Din, Tdin);//…
T1
T2
A/DConverter
A/DConverter
AmplifierDisplaycircuit
Motor Control
AmplifierTemperature
DifferenceAlarm
Td1 Din Dout
Mout
AoutTd2
Tdin
![Page 21: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/21.jpg)
Easy to understandEasier to changeEasier to test and debug.
![Page 22: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/22.jpg)
Helps in dealing with large programs.
![Page 23: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/23.jpg)
Function PrototypeA function prototype tells you all you need
to know to write a call to the functionPrototypes are normally placed before the
main part of your program return_type Function_Name ( Parameter_List );
![Page 24: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/24.jpg)
Function PrototypeA function prototype tells you all you need
to know to write a call to the functionPrototypes are normally placed before the
main part of your program return_type Function_Name ( Parameter_List );Parameter_list is a comma separated list of parameters:Type1 Parameter_1, Type2 Parameter_2 ,
Type3 Parameter_3 …. TypeN Parameter_N
![Page 25: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/25.jpg)
![Page 26: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/26.jpg)
![Page 27: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/27.jpg)
int sum(int x, int y);int main(){ int answer;cout<<“In main”;answer = sum(2,3);cout<<answer;
return 0;}int sum (int x, int y){int z=x+y;cout<<“In sum”;return z;
}
![Page 28: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/28.jpg)
A function prototype declares the function prior to its definition.
Declaration is done before the first time the function is called.
![Page 29: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/29.jpg)
Three places where variables are declared:
1. Inside functions: local variables
2. In the definition of function parameters: formal parameters
3. Outside of all functions: global variables
![Page 30: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/30.jpg)
local variables are created when function is called and destroyed when function is exited.
formal parameters are used like local variables. Their value is lost once the function terminates. They have a special task of receiving the value of arguments.
global variables hold their value throughout the lifetime of your program
![Page 31: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/31.jpg)
Some functions may not have a return value.
void can be used to indicate that the function does not return a value.
Some functions may not have formal parameters.
void can be used to indicate that the function does not take any formal parameters.
![Page 32: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/32.jpg)
A few examples …
void function1 (int a , int b);int function2 (void)void function3 (void)int function4 (int a, int b);
![Page 33: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/33.jpg)
Examples of Functions#include <iostream.h>
void function1(void);//function prototype
int main(void){
cout<<"I am in main"<<endl;
function1();
cout<<"back from function1 to main"<<endl;
return 0;
}
void function1(void){
cout<<"I am in function1"<<endl;
}
![Page 34: Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d355503460f94a0cd24/html5/thumbnails/34.jpg)
Examples of Functions
I am in main
I am in function1
back from function1 to main