// Example 20 // Problem: // Write a function that returns the median of three // numbers. Invoke the function from main(). #include int main() { double median(double, double, double); double x, y, z; cout << "Enter any three numbers"<> x >> y >> z; // The following line invokes the function cout <<"The median is "<< median(x,y,z)< int main() { double p(double, int); double z; double poly; z = 2.5; poly = 2.3*p(z,5)+1.7*p(z,4)-2.0*p(z,3) +1.5*p(z,2)+2.0+0.5*p(z,-1)+2.33*p(z,-2); cout << poly <= 0) { product = 1; for (i=0; i < n; ++i) product = product * x; } else { product = 1; for (i=0; i > n; --i) product = product * x; product = 1.0 / product; } return product; } // Example program 22 // This program demonstrates the use of reference // parameters for function calls. // Problem: // Write a function that swaps the values of two // integer variables. #include int main() { void swap(int&, int&); int x, y; x = 9; y = 20; cout << x <<" " << y << endl; swap(x,y); cout << x <<" "<< y << endl; // Try to invoke the function as swap(56, 8); // This will generate error message. why? return 0; } void swap(int& a, int& b) { int temp; temp = a; // save a in temp a = b; // copy b to a b = temp; // copy temp (prev. a) to b } // Example program 23 // This program demonstrates the use of input // file stream parameters for function calls. // Problem: // Write a function that returns the number of words // in a file. #include int main() { int number_of_words(ifstream&); ifstream infile; infile.open("ex25.in"); cout <<"Number of words: "<> char1; while(file) { ++ number; // ignore all non whitespace characters file.get(char1); while (file) { if(char1 ==' '||char1=='\n'||char1=='\t') break; file.get(char1); } file >> char1; } return number; } // Example program 24 // This program demonstrates how to invoke // functions with reference parameters, value // parameters, and input and output file // stream parameters. // Problem statement: Calculate the area // of a triangle from its height and base. // The program reads unknown number of sets of // data for height and base from an input file // and outputs height, base, and area in an // output file after calculation. If height or // base is negative then it outputs an error // message "*** Invalid data ***". #include typedef int Bool; // Declare type Bool as int Bool const TRUE = 1, FALSE = 0; int main() { void write_heading(ofstream&); // Prototypes void write_line(ofstream&,double,double,double,Bool); Bool get_data(ifstream&,double&,double&,Bool&); double calculate_area(double,double); double height, base, area; Bool eof, valid; ifstream infile; ofstream outfile; infile.open("data20.in"); outfile.open("data20.out"); write_heading(outfile); valid = get_data(infile,height, base, eof); while (!eof) { if (valid) { area = calculate_area(height, base); write_line(outfile,height,base,area,valid); } else write_line(outfile,height,base,area,valid); valid = get_data(infile,height, base, eof); } infile.close(); outfile.close(); return 0; } void write_heading(ofstream& out1) { out1<<"Height Base Area"<> h >> b; if(in) { e = FALSE; if ( h < 0 || b < 0) return FALSE; return TRUE; } else { e = TRUE; return FALSE; } } double calculate_area(double h,double b) { return (h * b /2.0); } // Example program 25 // This program demonstrates the use of // input and output stream variables as reference // parameters for function calls. // Problem: // Write a function that reads an integer amount // from a file and writes the amount in words in // a separate file. #include int main() { ifstream infile; ofstream outfile; void write_in_words(ofstream&,double); double number; infile.open("ex26.in"); outfile.open("ex26.out"); infile >> number; while (infile) { if (number > 0 && number < 1.0e9) write_in_words(outfile, number); infile >> number; } infile.close(); outfile.close(); return 0; } void write_in_words(ofstream& out1, double num) { void write_hundreds(ofstream&, int); int num_mill, num_thous; // Decompose the number millions, // thousands etc. out1 << num << endl; num_mill = num / 1.0e6; // calc. no. of millions num = num - num_mill*1.0e6; // get remainder num_thous = num / 1000; // calc. no. of thousands num = num - num_thous*1000; // get remainder if (num_mill > 0) { write_hundreds(out1, num_mill); out1 <<" million "; } if (num_thous > 0) { write_hundreds(out1, num_thous); out1 <<" thousand "; } write_hundreds(out1, (int)num); out1 << endl; return; } void write_hundreds(ofstream& out2, int num2) { void write_1_to_19(ofstream&, int); void write_20_to_90(ofstream&, int); int num_hundred, num_tenth; num_hundred = num2/100; num2 = num2%100; num_tenth = 0; if (num2 >=20) { num_tenth = num2/10; num2 = num2%10; } if (num_hundred > 0) { write_1_to_19(out2,num_hundred); out2 <<" hundred "; } if (num_tenth > 0) { write_20_to_90(out2,num_tenth); out2 <<" "; } if (num2 > 0) { write_1_to_19(out2,num2); } } void write_1_to_19(ofstream& out3, int num3) { switch(num3) { case 1: out3 << "one"; break; case 2: out3 << "two"; break; case 3: out3 << "three"; break; case 4: out3 << "four"; break; case 5: out3 << "five"; break; case 6: out3 << "six"; break; case 7: out3 << "seven"; break; case 8: out3 << "eight"; break; case 9: out3 << "nine"; break; case 10: out3 <<"ten"; break; case 11: out3 <<"eleven";break; case 12: out3 <<"twelve";break; case 13: out3 <<"thirteen";break; case 14: out3 <<"fourteen";break; case 15: out3 <<"fifteen";break; case 16: out3 <<"sixteen";break; case 17: out3<<"seventeen";break; case 18: out3<<"eighteen";break; case 19: out3<<"ninteen";break; } } void write_20_to_90(ofstream& out3,int num3) { switch (num3) { case 2: out3 <<"twenty"; break; case 3: out3 <<"thirty"; break; case 4: out3 <<"fourty"; break; case 5: out3 <<"fifty"; break; case 6: out3 <<"sixty"; break; case 7: out3 <<"seventy"; break; case 8: out3 <<"eighty"; break; case 9: out3 <<"ninety"; break; } }