> a[size] >> b[size]; if (!fin.fail()) size++; } cout << “items read: “ << size;"> > a[size] >> b[size]; if (!fin.fail()) size++; } cout << “items read: “ << size;">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 1400 March 21, 2007 Odds and Ends Review. Reading to the end of a file Example test.txt : 110 220 330 440 550 660 770 880 990 10 100 … Suppose we need.

Similar presentations


Presentation on theme: "CS 1400 March 21, 2007 Odds and Ends Review. Reading to the end of a file Example test.txt : 110 220 330 440 550 660 770 880 990 10 100 … Suppose we need."— Presentation transcript:

1 CS 1400 March 21, 2007 Odds and Ends Review

2 Reading to the end of a file Example test.txt : 110 220 330 440 550 660 770 880 990 10 100 … Suppose we need to read these two parallel arrays without knowing how many values are in the file?

3 Method 1 int a[50], b[50]; // may be larger than needed fin.open(“test.txt"); while (!fin.fail()) {fin >> a[size] >> b[size]; if (!fin.fail()) size++; } cout << “items read: “ << size;

4 Method 2 int a[50], b[50]; fin.open(“test.txt"); while (fin >> a[size] >> b[size)) {size++; } cout << “items read: “ << size; The file variable itself can be tested for success or failure

5 Review of static variables… A static variable is; –initialized only once (first time the function is called) –retains its value or stays alive when the function exits

6 Example: void DisplayNextFive (bool restart) { static int start_value = 0; if (restart) start_value = 0; for (int n=start_value; n<start_value+5; n++) cout << n << endl; start_value += 5; } int main() {DisplayNextFive (false); DisplayNextFive (false); DisplayNextFive (true); DisplayNextFive (false);

7 Default parameters A function may assign default values to the last parameters in a list using the “=“ operator. Default parameters need not be specified by the caller.

8 Example 1 void DisplayNextFive (bool restart = false) {static int start_value = 0; if (restart) start_value = 0; for (int n=start_value; n<start_value+5; n++) cout << n << endl; start_value += 5; } int main() {DisplayNextFive (); DisplayNextFive (); DisplayNextFive (true); DisplayNextFive (); DisplayNextFive (true); DisplayNextFive ();

9 Example 2 int SumTwoToFiveNumbers (int a, int b, int c=0, int d=0, int e=0) {return (a+b+c+d+e); } int main() {cout << SumTwoToFiveNumbers (2, 4, 3, 5, 7) << endl; cout << SumTwoToFiveNumbers (2, 4, 3, 5) << endl; cout << SumTwoToFiveNumbers (2, 4, 3) << endl; cout << SumTwoToFiveNumbers (2, 4) << endl;

10 Function Overloading C++ matches a function call to a function using the function signature: –function name –number and type of parameters No two functions can have the same signature (but they can have the same name!)

11 Example int Average (int a, int b)// #1: calculate integer average with rounding { return (int((a + b) / 2.0 + 0.5)); } float Average (float a, float b)// #2: calculate average of two float values { return ((a + b) / 2.0); } float Average (float a, float b, float c) // #3: calculate average of three float values { return ((a + b + c) / 3.0); } int main() { int n = 5, k = 8; float x = 5.0, y = 8.0, z = 10.0; cout << Average (n, k) << endl;// calls #1: outputs 7 cout << Average (x, y) << endl;// calls #2: outputs 6.5 cout << Average (x, y, z) << endl;// calls #3: outputs 11.5

12 What about this?? int Average (int a, int b)// #1: calculate integer average with rounding { return (int((a + b) / 2.0 + 0.5)); } float Average (float a, float b)// #2: calculate average of two float values { return ((a + b) / 2.0); } float Average (float a, float b, float c) // #3: calculate average of three float values { return ((a + b + c) / 3.0); } int main() { int n = 5, k = 8; float x = 5.0, y = 8.0, z = 10.0; cout << Average (n, y) << endl;// ambiguous!! cout << Average (5.0, 8.0) << endl;// also ambiguous!!

13 Floating-point constants are double One possible solution – use double variables double Average (double a, double b) { return ((a + b) / 2.0); } Another possible solution – cast constants to float cout << Average (float(5.0), float(8.0)) << endl;


Download ppt "CS 1400 March 21, 2007 Odds and Ends Review. Reading to the end of a file Example test.txt : 110 220 330 440 550 660 770 880 990 10 100 … Suppose we need."

Similar presentations


Ads by Google