Sei sulla pagina 1di 25

1 Experiment #03A: Functions

Part 1 Requirement specification Read three resistor values in series in main function then compute and display the current across the circuit in user defined function. Analysis Data input Data output Constrain Define V=10, rt=1, r, n=0 Current across resistor r<=0, n<=3

2
Design
Begin main Define V=10

Read n, r, rt

n=1, rt=0

Display Enter three resistor values in ohms: Input r

r<=0

T F

Display Resistor value must positive value

Display Reenter resistor value

Compute rt=rt+r n+=1

n<=3

F
Call function current(rt) A End main

A Begin current Read I

Compute I=V/rt

Display Current, I

End current

Implementation

#include<iostream> #define V 10 using namespace std; void current(double rt); void main() { int n=1,r; double rt=0; do { cout<<"Enter three resistor values in ohms: "; cin>>r; while(r<=0) { cout<<"Resistor value must positive value."<<endl; cout<<"Reenter resistor value: "; cin>>r; } rt=rt+r; n+=1; } while(n<=3); current(rt); } void current(double rt) { double I; I=V/rt; cout<<"The current value is "<<I<<" A"<<endl; }

4
Verification and Testing

Tested output:

Verification using calculation:

5
Part 2 Requirement Specification C++ program that can prepare conversion table from kilograms to pound or vice versa. User is able to choose the range of weight to display and the weight value must always be positive. The program creates two functions which are: 1. Function for converting and displaying the conversion table from kilograms to pound. 2. Function for converting and displaying the conversion table from pound to kilograms. Analysis Data Input Data output Constrain Choice, a Range of conversion, x & y Conversion of Kilograms to Pound, x*2.2 Conversion of Pound to Kilograms, x/2.2 x<=0 or y<=0 x>=y x<=y

6
Design

Begin main

Display 1. Convert Kilograms to pound. 2. Convert Pound to Kilograms. 0. Exit program Please choose your conversion:

Input a

Call Function KGtoP

T If(a==1) F T Call function PtoKG

If(a==2) F T If(a==0) F

Display Please follow the instruction given Press any key to reenter.

Call function main

End main

A Begin KGtoP

Display You choose to convert Kilograms to Pound Enter the range x-y (Value must be positive) :

Input x, y

T x<=0 || y<=0 F Display The value of x must be less than y T x>=y F Display Kg \t Pound

Display The value of x and y must be positive.

Please reenter range x-y:

Please reenter range x-y:

T x<=y F Display Press any key to return to menu.

Display x \t x*2.2

x++

Call function main M End KGtoP

B Begin PtoKG

Display You choose to convert Pound to Kilograms Enter the range x-y (Value must be positive) :

Input x, y

T x<=0 || y<=0 F Display The value of x must be less than y T x>=y F Display Pound \t Kg

Display The value of x and y must be positive.

Please reenter range x-y:

Please reenter range x-y:

T x<=y F Display Press any key to return to menu.

Display x \t x/2.2

x++

Call function main M End PtoKG

9
Implementation

/* Muhammad Syakirin bin Annuar AE110057 Universiti Tun Hussein Onn Malaysia */ #include<iostream> #include<conio.h> using namespace std; void KGtoP(); void PtoKG(); int main() { system("cls"); int a; cout<<"1. Convert Kilograms to Pound.\n\n"; cout<<"2. Convert Pounds to Kilogram.\n\n"; cout<<"0. Exit program.\n\n"; cout<<"Please choose your conversion:"; cin>>a; if(a==1) KGtoP(); else if(a==2) PtoKG(); else if(a==0) return 0; else { cout<<"\nPlease follow the instruction given\n"; cout<<"Press any key to reenter."; getch(); main(); } return 0; }

10

void KGtoP() { int x, y; cout<<"\nYou choose to convert Kilograms to Pound.\n\n"; cout<<"Enter the range x-y (Value must be Positive):"; cin>>x>>y; while(x<=0 || y<=0) { cout<<"The value of x and y must be positive.\n"; cout<<"Please reenter range x-y:"; cin>>x>>y; } while(x>=y) { cout<<"The value of x must be less than y\n"; cout<<"Please reenter range x-y:"; cin>>x>>y; } cout<<"Kg\tPound\n"; while(x<=y) { cout<<x<<"\t"<<x*2.2<<endl; x++; } cout<<"Press any key to return to menu."; getch(); main(); }

11

void PtoKG() { int x, y; cout<<"\nYou choose to convert Pound to Kilograms.\n\n"; cout<<"Enter the range x-y (Value must be Positive):"; cin>>x>>y; while(x<=0 || y<=0) { cout<<"The value of x and y must be positive.\n"; cout<<"Please reenter range x-y:"; cin>>x>>y; } while(x>=y) { cout<<"The value of x must be less than y\n"; cout<<"Please reenter range x-y:"; cin>>x>>y; } cout<<"Pound\tKg\n"; while(x<=y) { cout<<x<<"\t"<<x/2.2<<endl; x++; } cout<<"Press any key to return to menu."; getch(); main(); }

12
Testing & Verification Tested output: Program menu

Convert Kilograms to Pound

13
Convert Pound to Kilograms

If any number besides 0, 1, and 2 is entered by user

Exit program

14 Experiment #03B: File Processing Part 1 Question 1 Requirement Specification Read and display the content of input.txt Analysis Input Output Constrain
Design

Read the input from the file input.txt Display the content of input.txt Read the input until the end of the file

Begin #include<iostream> #include<fstream> const SIZE=81 Read inFile, item[SIZE]

Open inFile

Input item F !inFile.eof T Close inFile

Display item

End

15
Implementation

/* Muhammad Syakirin bin Annuar AE110057 Universiti Tun Hussein Onn Malaysia */ #include<iostream> #include<fstream> using namespace std; int main() { const int SIZE=81; fstream inFile("input.txt", ios::in); char item[SIZE]; inFile>>item; while(!inFile.eof()) { cout<<item<<endl; inFile>>item; } inFile.close(); return 0; }

16
Verification & Testing Output of the program:

The content of the input.txt:

17
Question 2 Requirement Specification Organize the data and store the output in the file output.txt Analysis Input SIZE=5 nums[SIZE]={100.279, 1.719, 8.602, 7.777, 5.099} Store the output of the data after organized to two decimal places into output.txt. count<5

Output Constrain

Design

Begin

#include<iostream #include<fstream> #include<iomanip> const int SIZE=5

Read outFile, nums[SIZE]

Open outFile

Nums[SIZE]={100.279, 1.719, 8.602, 7.777, 5.099}

Store outFile Setprecision(2)

int count=0; count<5; count++ F end

Store in outFile setw(8)<<nums[ count]

18
Implementation

/* Muhammad Syakirin bin Annuar AE110057 Universiti Tun Hussein Onn Malaysia */ //File name: E3Bb.cpp //File description: store the data in a file name out.txt #include<iostream> #include<fstream> #include<iomanip> using namespace std; int main() { const int SIZE=5; ofstream outFile("output.txt"); double nums[SIZE]={100.279, 1.719, 8.602, 7.777, 5.099}; outFile<<fixed<<setprecision(2); for(int count=0; count<5; count++) { outFile<<setw(8)<<nums[count]; } outFile.close(); return 0; }

Verification & Testing Output stored in the output.xtx:

19
Part 2 Requirement Analysis Program that asks the user for two file names. The first file will be opened for input and the second file will be opened for output. The program will read the contents of the first file and change all the letters to uppercase. The revised contents should be stored in the second file. Analysis Input Output Constrain Input file name, Output file name Revised content of the input file stored in output file. Read the content of the input file until the end of the file.

20
Design
Begin #include<iostream> #include<fstream> #include<cstdlib> #include<cctype>

Read inFile[16], outFile[16], hello, masuk, keluar

Display Enter a input file name: Input inFile Display Enter a output file name: Input outFile Open masuk Display Cannot read the file.

if(masuk.fail()) F Open keluar

Exit(1)

Exit(1)

Display Cannot read the file.

if(keluar.fail()) F masuk.get(hello) F !masuk.eof() T Display File conversion done. toupper(hello)

Store in keluar

Close masuk, keluar

End

21
Implementation /* Muhammad Syakirin bin Annuar AE110057 Universiti Tun Hussein Onn Malaysia */ #include<iostream> #include<fstream> #include<cstdlib> #include<cctype> using namespace std; int main() { char inFile[16], outFile[16], hello; ifstream masuk; ofstream keluar; cout<<"Enter a input file name: "; cin>>inFile; cout<<"Enter a output file name: "; cin>>outFile; masuk.open(inFile); if(masuk.fail()) { cout<<"Cannot read the file.\n"; exit(1); } keluar.open(outFile); if(keluar.fail()) { cout<<"Cannot read the file.\n"; exit(1); } masuk.get(hello); while(!masuk.eof()) { keluar.put(toupper(hello)); masuk.get(hello); } cout<<"File conversion done.\n"; masuk.close(); keluar.close(); return 0; }

22
Testing & Verification Output of the program;

Input file(kilin.txt):

Output file(pinky.txt):

23
Discussion There are many benefits using modular design. That is: a) Ease of understanding Each module should perform just one function. b) Reusable code Modules used in one program can be also be used in other programs. c) Elimination of redundancy Using modules can help to avoid the repetition of writing the out the same segment of code more than once. d) Efficiency of maintenance Each module should be self-contained and have little or no effect on other modules within the program. There are two categories of functions that is standard library functions and user-defined functions. Furthermore, there are 3 function elements which is function definition, function prototype and function call. Function definition has two principle components; function header and function body. Function header consists of 3 elements. Firstly, a function specified the returned value of type. Secondly, a function name by applying the same rules to naming variable. Lastly, a formal parameters enclosed in parenthesis (optional). While a function body may consist of variable declarations, statements that indicate the actual task and return statement. Function prototype declares the function definition. If the function is called before defining it, function prototype must be placed before the main function. There is no need for a prototype if the function is called after its definition. A function call accessed the function definition. The function call can be place within main function or user-defined function. The benefits using data files method: a) Files allow store data permanently. Data output to file lasts after the program ends. b) An input file can be used over and over. No typing of data again and again for testing. c) File allows deal with larger data sets.

24
When using data file method, the program must include fstream library by typing #include<fstream> at the beginning of the program. Ifstream is used for input stream while ofstream is used for output stream. When using ifstream, the program must always check if the file to be opened is exist or not, for example: ifstream masuk; masuk.open(inFile); if(masuk.fail()) { cout<<"Cannot read the file.\n"; exit(1); } This is because, ifstream does not generate the file automatically while ofstream always generate the file automatically if the file does not exist. The library cstdlib must be included to use this method. Eof() function is used on the input object to detect end of file. The following instruction is normally used to determine when we are not at the end of file: if(!in_stream.eof()) There is two method to test end of file. 1. Use extraction operator method. When processing numeric data 2. Use eof() method. When input is treated as text and reads input.

25
Conclusion 1. The usage of user-defined function can simplify one long complicated main function into several simple user-defined functions. 2. It is easier to detect any error when using user-defined function. 3. Easy to maintain the program if user-defined function used. 4. The data can be stored permanently when using data files method. 5. Allows deal with larger data sets. 6. An input file can be used over and over by simply using the stored data in form of text file or binary files.

Potrebbero piacerti anche