Program | Demonstrates |
---|---|
checkDigit.cpp. | Demonstrates one of the predefined functions isdigit( ) |
days8.cpp | Demonstrates the passing of arrays to functions. |
EXAMPL2A.CPP | This program demonstrates the use of the definition, the declaration, and the call of a function. |
EXAMPL2B.CPP | This program demonstrates the use of the definition, the call of a function but where the definition of the function is in a header file tools.h. |
generalLedger.cpp | Demonstrates how to pass multidimensional arrays to functions as arguments. |
grades.cpp | This program received the student name, the number of grades to average and the value of each grade. It then calculates the average of the grades and displays the student name and average. |
Design for grades.cpp sc_grades pc_grades | The structure chart and the pseudo code for the program: grades.cpp |
payroll.cpp | This program receives the name, number and sales of each of 20 employees. It then calculates the gross pay and displays the name and gross pay for each employee. This program utilizes a non-modular approach. |
Design for payroll.cpp pc_payroll | This is the pseudo code for the program payroll.cpp. |
payroll2.cpp | This program receives the name, number and sales of each of 20 employees. It then calculates the gross pay and displays the name and gross pay for each employee. This program utilizes a modular approach. |
Design for payroll2.cpp sc_payroll2 pc_payroll2 | These are the design files for payroll2.cpp |
powers.cpp | This program uses one of the predefined functions pow(,) to raise the first argument to the power of the second argument. |
random.cpp | This program uses one of the predefined functions rand() to demonstrate how to do random numbers. Notice the problem when run more than once. |
salary.cpp | Demonstrates the use of function declaration, call and definition. |
stringconversion.cpp | Uses functions to convert a string with spaces to a string with no spaces and then a second function to convert the string back to one with the spaces. |
taxes.cpp | This program displays a table report of tax values to the screen given the maximum property value and the tax rate from keyboard entry. |
Design for taxes.cpp tax_sc tax_pc | The design files for taxes.cpp. |
TEXTIN.CPP | This program demonstrates input of strings and characters from a disk file one character at a time. |
TEXTIN2.CPP | This program demonstrates input of strings and characters from a disk file a variable for each line. |
TEXTOUT.CPP | This program demonstrates output of strings or numbers to a disk file in ASCII text format. |
tools.h | This header file is used to hold the function definitions used in the program EXAMPL2B.CPP |
// program_id checkDigit.cpp // written_by don voils // date_written 05/25/2006 // descriptoin This program demonstrates the // use of the function isdigit(). // #include<iostream> #include<cctype> using namespace std; void main() { char digit; cout << "Enter a digit. "; cin >> digit; if(isdigit(digit)) cout << endl << "That was the digit " << digit << endl; else cout << endl << "That was not a digit!!! That was the character " << digit << endl; char resp; cout << endl << endl << "Continue? "; cin >> resp; cout << endl << endl; } // program_id days8.cpp // written_by Don Voils // date_written 7/3/2006 // Program_description This program demonstrates the // passing of arrays to functions. #include<iostream> #include<iomanip> // Notice that the following header must be added inorder to // output strings. // #include<string> using namespace std; // Notice that the brackets of an array are listed in // the declarations of the following functions but // the dimensions of the array are not. // void enterSales(double salesAmounts[],string salesDays[]); void displaySales(short arraySize, double salesAmounts[],string SalesDays[]); void main() { const int arraySize = 7; double salesAmounts[arraySize]; cout << fixed << setprecision(2); // The following is not only defining the array but initializing it // with strings that are the names of the days of the week. // string salesDays[arraySize] = {"Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday"}; // Notice that the brackets of an array are not listed // in the call. // enterSales(salesAmounts, salesDays); cout << endl << endl; // Notice that the brackets of an array are not listed // in the call. // displaySales(arraySize, salesAmounts,salesDays); cout << endl << endl; char resp; cout << "Continue? "; cin >> resp; cout << endl << endl; } void enterSales(double salesAmounts[],string salesDays[]) { for(int index = 0;index<=6;index++) { cout << "What was the sales on " << salesDays[index] << "? "; cin >> salesAmounts[index]; } } // The brackets of the arrays that are passed to the // function are listed and the size of the arrays is because // the arrays are not controled by the enumerators. // void displaySales(short arraySize, double salesAmounts[], string salesDays[]) { for(short index = 0;index< arraySize;index++) { cout << "The sales on " << salesDays[index] << " was $" << salesAmounts[index] << endl; } } // program_id: EXAMPL2A.CPP // author: DON VOILS // date written: 07/10/93 // modified by dlv // date(s) modified: 08/23/2006 // // program description: This program demonstrates the use of the // definition, the declaration, and the call // of a function. // #include<iostream> using namespace std; // Function Declaration by Prototypes void intro(); void conclude(); void main() { //Function Calls intro(); conclude(); char resp; cout << endl << endl << "Continue? "; cin >> resp; } // Function Definitions // Notice that these functions have a void signature and a void outputypename void intro() { cout << "Hello my name is Bit Twiddler." << endl << endl; } void conclude() { cout << "We will be talking throughout the course. See ya!!!" << endl; } // program_id EXAMPL2B.CPP // written_by don voils // date_written 8/2/2006 // // program description: This program demonstrates the use of the // definition, the call of a function // but where the definition of the function // is in a header file. // #include<iostream> using namespace std; // Notice that this header is listed after the namespace std. // This was needed because tools.h uses cout that is in std // and so must be after the using namespace std statement. // #include"tools.h" void main() { // Function Calls intro(); conclude(); char resp; cout << endl << endl << "Continue? "; cin >> resp; } // program_id generalLedger.cpp // written_by Don Voils // date_written 06/26/2006 // // description This program demonstrates how to pass multidimentional // arrays to functions as arguments. // #include<iostream> #include<iomanip> #include<string> using namespace std; // Constants used to control the arrays // const short numberMonths = 12; const short numberAccounts = 5; // Function declarations // void clearScreen(); void enterAmounts(double generalLedger[][numberAccounts],string theMonths[]); void displayAmounts(double generalLedger[][numberAccounts],string theMonths[],short numberMonths,short numberAccounts); void main() { short resp; bool continueProgram = true; string theMonths[]= { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October","November","December"}; double generalLedger[numberMonths][numberAccounts] = {{0.00,0.00,0.00,0.00,0.00}}; do { clearScreen(); cout << "General Ledger" << endl << endl << "1. Enter Amounts" << endl << "2. Display Amounts" << endl << "3. Quit " << endl << endl; cin >> resp; switch(resp) { case 1: clearScreen(); enterAmounts(generalLedger,theMonths); break; case 2: clearScreen(); displayAmounts(generalLedger,theMonths, numberMonths,numberAccounts); break; case 3: clearScreen(); continueProgram = false; break; default: break; } }while(continueProgram); } void clearScreen() { for(short index = 0; index <=50; ++index) cout << endl; } void enterAmounts(double generalLedger[][numberAccounts],string theMonths[]) { for(int indexMonths = 0; indexMonths <= 11; indexMonths++) { cout << endl << "Enter the amounts for " << theMonths[indexMonths] << endl; for(int indexAccounts = 0; indexAccounts <= 4; indexAccounts++) { cout << "The amount for account #" << static_cast<int>(indexAccounts+1) << " "; cin >> generalLedger[indexMonths][indexAccounts]; } } cout << endl << endl << "All of the General Ledger Accounts have been entered." << endl; cout << "Continue? "; char resp; cin >> resp; } void displayAmounts(double generalLedger[][numberAccounts], string theMonths[],short numberMonths,short numberAccounts) { cout << fixed << setprecision(2); double totalLedger = 0.00; for(short indexMonths = 0; indexMonths < numberMonths; ++indexMonths) { double totalAccountAmount = 0.00; cout << endl << "The amounts for " << theMonths[indexMonths] << endl; for(short indexAccounts = 0; indexAccounts < 5; ++indexAccounts) { cout << "The amount for account #" << (indexAccounts+1) << " $" << setw(8) << generalLedger[indexMonths][indexAccounts] << endl; totalAccountAmount += generalLedger[indexMonths][indexAccounts]; } cout << " __________" << endl; cout << "Total amount for " << theMonths[indexMonths] << " $" << setw(8) << totalAccountAmount << endl << endl; totalLedger += totalAccountAmount; } cout << endl << "General Ledger Total $" << setw(8) << totalLedger << endl << endl; char resp; cout << "Continue? "; cin >> resp; } /* program_id grades.cpp written_by don voils date_written 05/04/2006 program_description This program received the student name, the number of grades to average and the value of each grade. It then calculates the average of the grades and displays the student name and average. */ #include<iostream> #include<string> using namespace std; double calculateAverage(short number); int main() { string name; short numberGrades; double theAverage; cout << endl << "What is the student's last name? "; getline(cin,name); cout << endl << "How many grades need to be averaged? "; cin >> numberGrades; theAverage = calculateAverage(numberGrades); cout << "Student " << name << " had an average grade of " << theAverage << endl << endl; char resp; cout << endl << endl << "Continue? "; cin >> resp; return 0; } double calculateAverage(short number) { short index; double sum,score; double average; sum = 0; for(index=1;index <= number; ++index) { cout << endl << "What was score number "<< index << "? "; cin >> score; sum = sum + score; } average = sum/number; return average; } /* prograd_id payroll2.cpp written_by don voils date_written 05/05/2006 program_description This program receives the name, number and sales of each of 20 employees. It then calculates the gross pay and displays the name and gross pay for each employee. This program utilizes a modular approach. */ #include<iostream> #include<string> using namespace std; // The following is the function declarations // void commission(); // Notice that instead of creating another function for // these constants, that in C++ it is possible to create // global variables that may be accessed inside of any // block of code in the program. // // A different approach would have been to place these statements // inside of the function commission(). // const double base_gross = 300.00; const double base_sales = 500.00; const double commission_rate1 = .15; const double commission_rate2 = .10; const short number_employees = 20; int main() { short index; // Notice that the function commission() is called 20 times. // for(index = 1;index <= number_employees;++index) commission(); return 1; } void commission() { // Since the following definitions are made inside of the function // these variables are local and are defined each time the // function is called. // double gross_pay, sales, commission1, commission2; string name, number; cout << endl << "What is the employee's last name? "; cin >> name; cout << endl << "What is the employee's number? "; cin >> number; cout << endl << "What was the employee's sales for the week? "; cin >> sales; // Notice that base_sales, commission_rate1 and commission_rate2 // are defined outside of all functions. They are therefore global // variables and therefore are accessible in this function. // if(base_sales < sales) { commission2 = (sales-base_sales) * commission_rate2; commission1 = (base_sales) * commission_rate1; } else { commission2 = 0; commission1 = sales * commission_rate1; } // The variable base_gross is a global variable defined outside // of all functions and therefore is accessible inside of this function. // gross_pay = base_gross + commission1 + commission2; cout << endl << "Employee last name " << name; cout << endl << "Employee number " << number; cout << endl << "Weekly gross pay is $" << gross_pay << endl <<endl; } /* program_id payroll.cpp written_by don voils date_written 05/05/2006 program_description This program receives the name, number and sales of each of 20 employees. It then calculates the gross pay and displays the name and gross pay for each employee. This program utilizes a non-modular approach. */ #include<iostream> #include<string> using namespace std; int main() { const double base_gross = 300.00; const double base_sales = 500.00; const double commission_rate1 = .15; const double commission_rate2 = .10; const short number_employees = 20; double gross_pay, sales, commission1, commission2; string name, number; short index; for(index = 1;index <=number_employees;++index) { cout << endl << "What is the employee's last name? "; getline(cin,name); cout << endl << "What is the employee's number? "; cin >> number; cout << endl << "What was the employee's sales for the week? "; cin >>sales; if(base_sales <= sales) { commission2 = (sales-base_sales) * commission_rate2; commission1 = (base_sales) * commission_rate1; } else { commission2 = 0.00; commission1 = sales * commission_rate1; } gross_pay = base_gross + commission1 + commission2; cout << endl << "Employee last name " << name; cout << endl << "Employee number " << number; cout << endl << "Weekly gross pay is $" << gross_pay << endl <<endl; } return 1; } // program_id pc_grades.txt // written_by don voils // date_written 3/4/2006 // Grades() Display request for student name Input name Display a request for the number of grades Input numberGrades Perform calculateAverage(numberGrades) Receive theAverage from calculateAverage() Display name, theAverage End calculateAverage(numberGrades) Set sum = 0 Do index=1 to numberGrades Display request for grade Input grade Calculate sum = sum + grade ENDDO Calculate average = sum / numberGrades Return average // program_id pc_payroll2.txt // written_by don voils // date_written 4/3/2006 // PAYROLL2 Process INITIALIZE() SET index = 1 DO WHILE index <= number_employees Process COMMISSION() CALCULATE index = index + 1 ENDDO END INITIALIZE() SET base_gross = 300.00 SET base_sales = 500.00 SET commission_rate1 = .15 SET commission_rate2 = .10 SET number_employees = 20 RETURN COMMISSION() Input name, number, sales IF (sales >= base_sales) THEN CALCULATE commission2 = (sales - base_sales) * commission_rate2 CALCULATE commission1 = base_sales * commission_rate1 ELSE CALCULATE commission1 = sales * commission_rate1 CALCULATE commission2 = 0.00 ENDIF CALCULATE gross_pay = base_gross + commission1 + commission2 Display name,number, gross_pay; RETURN // program_id pc_payroll.txt // written_by don voils // date_written 3/4/2006 // PAYROLL Set base_gross = 300.00 Set base_sales = 500.00 Set commission_rate1 = .15 Set commission_rate2 = .10 Set number_employees = 20; DO index = 1 to number_employees Input name, number, sales IF (sales >= base_sales) THEN Calculate commission2 = (sales - base_sales) * commission_rate2 Calculate commission1 = base_sales * commission_rate1 ELSE Calculate commission2 = 0.00 Calculate commission1 = sales * commission_rate1 ENDIF Calculate gross_pay = base_gross + commission1 + commission2 Display name,number, gross_pay; ENDDO STOP /* program_id powers.cpp written by Don Voils date written 9/24/2006 program_description This program uses one of the predefined functions: pow(,) to raise the first argument to the power of the second argument. */ #include<iostream> #include<cmath> #include<iomanip> using namespace std; void main() { int numberPowers; cout << "How many entries do you want ? "; cin >> numberPowers; cout << endl << endl; for(float index = 1;index <= numberPowers;++index) cout << setw(5) << index << setw(10) << pow(index,2) << setw(10) << pow(index,3)<< endl; char resp; cout << endl << endl << "Continue? "; cin >> resp; } // program_id random.cpp // written_by don voils // date_written 06/03/2006 // description This program demonstrates the use of the // rand() function in the header: cstdlib // #include<iostream> #include<cstdlib> #include<iomanip> using namespace std; int random(int theMaximum); void main() { int maximumNumber; cout << "What is the maximum value of the random number you want? "; cin >> maximumNumber; cout << endl << endl; for(int index = 1; index <= 20 ; ++index) cout << setw(5) << index << setw(5) << random(maximumNumber) << endl; char resp; cout << endl << endl << "Continue? "; cin >> resp; cout << endl << endl; } int random(int theMaximum) { return rand()% theMaximum + 1; } // program_id salary.cpp // written_by don voils // date_written 2/3/2006 // description Demonstrates the use of function declaration, call // and definition. // #include<iostream> #include<iomanip> using namespace std; double salary(double, double); void main(void) { double weeklyPay = salary(40, 35.95); cout << setprecision(2); cout << fixed; cout << endl << "The weekly pay is $" << weeklyPay << endl << endl; } double salary(double hoursWorked, double hourlyRate) { double grossPay; grossPay = hoursWorked * hourlyRate; return grossPay; } // Program_id sc_grades.gif // Written_by don voils // Date_written 3/2/2006 //
// program_id sc_payroll2.gif // written_by don voils // date_written 3/2/2006 //
// program_id stringconversion.cpp // written_by don voils // date_written 8/17/2006 // // description This program uses functions to // convert a string with spaces to // a string with no spaces and then // a second function to convert the // string back to one with the spaces. // #include<iostream> #include<string> using namespace std; void doit(string& theOne); void undoit(string& it); void main() { string it; cout << "What is the string? "; getline(cin,it); cout << endl << endl << it; doit(it); cout << endl << endl << it << endl; undoit(it); cout << endl << endl << it << endl; } void doit(string& it) { for(short index=0;index < (short) it.length();++index) if(it[index]==' ') it[index]= '*'; } void undoit(string& it) { for(short index=0;index < (short) it.length();++index) if(it[index]=='*') it[index]= ' '; } // program_id taxes.cpp // written_by don voils // date_written 5/3/2006 // modified by // date(s) modified // // program description This program displays a table report of // tax values to the screen given the // maximum property value and the tax // rate from keyboard entry. // #include<iostream> // The header below includes tools for formatting output. // #include<iomanip> using namespace std; // The following are global variables. // // Compare the following with the design. // const double increment_amount = 100.00; double property_value = 1000.00; short number_lines = 52, page_number = 1; // // functional declarations // void clear_screen(); void DetailRecord(double tax_rate); void PrintHeading(); void PrintDetailLine(double tax); void main() { // // The following command tells the output to have a fixed // decimal point. // cout.setf(ios::fixed); // // The following function will clear the screen. // clear_screen(); double maximum_value, tax_rate; // // Enter from the keyboard the two values needed // to start the report. // cout << "What is the maximum property value for the table? "; cin >> maximum_value; cout << "What is the tax rate? "; cin >> tax_rate; // // The following function will clear the screen. // clear_screen(); do { DetailRecord(tax_rate); // // Increment the amount of increase for each line // of the property_value. // property_value = property_value + increment_amount; }while(property_value <= maximum_value); } // // The following function will clear the screen. // void clear_screen() { short index; for(index=0;index<=25;++index) cout << endl; } // The following function is the main module of // the program. It calls two other modules. // void DetailRecord(double tax_rate) { // The following is a local variable that is only // available in this function. // double tax; // The following conditional will enable the display // of the report header every 52 lines. // if(number_lines >= 52) { PrintHeading(); ++page_number; // // Set the number of lines to the number lines in the heading. // number_lines = 3; } // // Calculate the tax on the property. // tax = property_value * tax_rate; // // Output the detail line and add 1 to the number of // lines printed // PrintDetailLine(tax); ++number_lines; } // // The following function will display the report heading. // void PrintHeading() { // // If we were sending the data to a printer we would need // to send a command to force a page feed here. // cout << endl << setw(60) << "City of Boca Raton, FL " << endl << "Page no. " << page_number << endl << setw(30) << " Property Value" << setw(30) << " Property Tax" << endl; } // // The following function will display the detail line of the report // void PrintDetailLine(double tax) { cout << setw(30) << setprecision(2) << property_value << setw(30) << setprecision(2) << tax << endl; } // program_id tax_pc.txt // written_by don voils // date_written 3/3/2006 // TAXES Process Initialization() Input maximum_value, tax_rate DOWHILE property_value <= maximum_value Process DetailRecord(tax_rate) CALCULATE property_value = property_value + increment_amount ENDDO END Initialization() SET increment_amount = 100.00 SET property_value = 1000.00 SET number_lines = 52 SET page_number = 1 Return DetailRecord(tax_rate) IF(number_lines >=52)THEN Process PrintHeading() CALCULATE page_number = page_number + 1 SET number_lines = 3 ENDIF CALCULATE tax = property_value * tax_rate Process PrintDetailLine(tax) CALCULATE number_lines = number_lines + 1 Return PrintHeading() Display City Name, page_number and Column Headings Return PrintDetailLine(tax) Display property_value, tax Return // Program_ID tax_sc.gif // Written_by Don Voils // Date_written 2/3/2006 //
// program_id textin2.cpp // author don voils // date written 11/25/2006 // // program description This program demonstrates input of // strings and characters from a disk file // a variable for each line. // #include<fstream> #include<iostream> #include<string> using namespace std; void main() { string line; // The following two lines open the files TESTING.TXT in // same folder at the program. The variable infile is used // to manipulate the data coming in from the file. // ifstream infile; infile.open("TESTING.TXT"); if(!infile) { cout << "Error opening the file." << endl << endl; exit(1); } // // Look at the data in the file. Notice that it contains text. // Notice further that a number was sent to the file // yet it was stored as text. When it is read in, it appears // to be a number but it is text. Each character is set out // and retrieved as a single character. It would have been // possible to read this in as a number by using a variable // defined as say a short. // getline(infile,line); cout << line << endl; getline(infile,line); cout << line << endl; getline(infile,line); cout << line << endl; getline(infile,line); cout << line << endl; getline(infile,line); cout << line << endl; getline(infile,line); cout << line << endl; infile.close(); cout << endl << endl; } // program_id textin.cpp // author don voils // date written 11/25/2006 // // program description This program demonstrates input of // strings and characters from a disk file // one character at a time. // #include<fstream> #include<iostream> using namespace std; void main() { char ch; // The following line opens the files TESTING.TXT on // in the same folder as the program. The variable infile // is used to manipulate the data coming in from the file. // ifstream infile("TESTING.TXT"); if(!infile) { cout << "Error opening the file."; exit(1); } // // Look at the data in the file. Notice that it contains text. // Notice further that a number was sent to the file // yet it was stored as text. When it is read in, it appears // to be a number but it is text. Each character is set out // and retrieved as a single character. It would have been // possible to read this in as a number by using a variable // defined as say a short. // infile.get(ch); while(infile) { cout << ch; infile.get(ch); } cout << endl << endl; } // program_id textout.cpp // author don voils // date written 11/25/2006 // // program description This program demonstrates output // of strings or numbers to a diskfile // in ASCII text format. // #include<fstream> #include<string> #include<iostream> using namespace std; void main() { string text = "This is a statement."; int aNumber = 125; // The following opens the file TESTING.TXT in the same // location as the program. The variable outfile is then // used to pass data to the file. // ofstream outfile("TESTING.TXT"); // The following conditional test to determine whether // a connection has been made to the file TESTING.TXT. // If no connection was made, a message is sent to the // screen and the funciton exit() terminates the program // and returns 1 to the operation system. // if(!outfile) { cout << "File did not open." ; exit(1); } // The following six lines are sent to the file. // Because of the use of the object endl, each // of the lines will be separate lines in the file. // After testing the program, open the data file and // observe the output. // outfile << "This is a test of outputting text in line 1." << endl; outfile << "This is a test of outputting text in line 2." << endl; outfile << "This is a test of outputting text in line 3." << endl; outfile << "This is a test of outputting text in line 4." << endl; outfile << text << endl; outfile << "The number = " << aNumber; } // header_id tools.h // written_by don voils // date_written 9/02/2006 // Function Definitions // Notice that these functions have a void signature and a void outputypename void intro() { cout << "Hello my name is Bit Twiddler." << endl << endl; } void conclude() { cout << "We will be talking throughout the course. See ya!!!" << endl; }