//DISPLAY 7.1 Program Using an Array//Reads in 5 scores and shows how much each//score differs from the highest score.#include<iostream>intmain( ) { using namespace std;inti, score[5], max; cout <<"Enter 5 scores:\n"; cin >> score[0]; max = score[0];for(i = 1; i < 5; i++) { cin >> score[i];if(score[i] > max) max = score[i];//max is the largest of the values score[0],..., score[i].} cout <<"The highest score is "<< max << endl <<"The scores and their\n"<<"differences from the highest are:\n";for(i = 0; i < 5; i++) cout << score[i] <<" off by "<< (max - score[i]) << endl;return0; }

//DISPLAY 7.3 Indexed Variable as an Argument//Illustrates the use of an indexed variable as an argument.//Adds 5 to each employee's allowed number of vacation days.#include<iostream>constintNUMBER_OF_EMPLOYEES = 3;intadjust_days(intold_days);//Returns old_days plus 5.intmain( ) { using namespace std;intvacation[NUMBER_OF_EMPLOYEES], number; cout <<"Enter allowed vacation days for employees 1"<<" through "<< NUMBER_OF_EMPLOYEES <<":\n";for(number = 1; number <= NUMBER_OF_EMPLOYEES; number++) cin >> vacation[number-1];for(number = 0; number < NUMBER_OF_EMPLOYEES; number++) vacation[number] = adjust_days(vacation[number]); cout <<"The revised number of vacation days are:\n";for(number = 1; number <= NUMBER_OF_EMPLOYEES; number++) cout <<"Employee number "<< number <<" vacation days = "<< vacation[number-1] << endl;return0; }intadjust_days(intold_days) {return(old_days + 5); }

//DISPLAY 7.4 Function with an Array Parameter//Function Declarationvoidfill_up(inta[],intsize);//Precondition: size is the declared size of the array a.//The user will type in size integers.//Postcondition: The array a is filled with size integers//from the keyboard.//Function Definition//Uses iostream:voidfill_up(inta[],intsize) { using namespace std; cout <<"Enter "<< size <<" numbers:\n";for(inti = 0; i < size; i++) cin >> a[i]; size--; cout <<"The last array index used is "<< size << endl; }

//DISPLAY 7.5 Outline of the Graph Program//Reads data and displays a bar graph showing productivity for each plant.#include<iostream>constintNUMBER_OF_PLANTS = 4;voidinput_data(inta[],intlast_plant_number);//Precondition: last_plant_number is the declared size of the array a.//Postcondition: For plant_number = 1 through last_plant_number://a[plant_number-1] equals the total production for plant number plant_number.voidscale(inta[],intsize);//Precondition: a[0] through a[size-1] each has a nonnegative value.//Postcondition: a[i] has been changed to the number of 1000s (rounded to//an integer) that were originally in a[i], for all i such that 0 <= i <= size-1.voidgraph(constintasterisk_count[],intlast_plant_number);//Precondition: asterisk_count[0] through asterisk_count[last_plant_number-1]//have nonnegative values.//Postcondition: A bar graph has been displayed saying that plant//number N has produced asterisk_count[N-1] 1000s of units, for each N such that//1 <= N <= last_plant_numberintmain( ) { using namespace std;intproduction[NUMBER_OF_PLANTS]; cout <<"This program displays a graph showing\n"<<"production for each plant in the company.\n"; input_data(production, NUMBER_OF_PLANTS); scale(production, NUMBER_OF_PLANTS); graph(production, NUMBER_OF_PLANTS);return0; }

//DISPLAY 7.6 Test of Function input_data//Tests the function input_data.#include<iostream>constintNUMBER_OF_PLANTS = 4;voidinput_data(inta[],intlast_plant_number);//Precondition: last_plant_number is the declared size of the array a.//Postcondition: For plant_number = 1 through last_plant_number://a[plant_number-1] equals the total production for plant number plant_number.voidget_total(int& sum);//Reads nonnegative integers from the keyboard and//places their total in sum.intmain( ) { using namespace std;intproduction[NUMBER_OF_PLANTS];charans;do{ input_data(production, NUMBER_OF_PLANTS); cout << endl <<"Total production for each"<<" of plants 1 through 4:\n";for(intnumber = 1; number <= NUMBER_OF_PLANTS; number++) cout << production[number - 1] <<" "; cout << endl <<"Test Again?(Type y or n and Return): "; cin >> ans; }while( (ans !='N') && (ans !='n') ); cout << endl;return0; }//Uses iostream:voidinput_data(inta[],intlast_plant_number) { using namespace std;for(intplant_number = 1; plant_number <= last_plant_number; plant_number++) { cout << endl <<"Enter production data for plant number "<< plant_number << endl; get_total(a[plant_number - 1]); } }//Uses iostream:voidget_total(int& sum) { using namespace std; cout <<"Enter number of units produced by each department.\n"<<"Append a negative number to the end of the list.\n"; sum = 0;intnext; cin >> next;while(next >= 0) { sum = sum + next; cin >> next; } cout <<"Total = "<< sum << endl; }

//DISPLAY 7.7 The Function scale//Demonstration program for the function scale.#include<iostream>#include<cmath>voidscale(inta[],intsize);//Precondition: a[0] through a[size-1] each has a nonnegative value.//Postcondition: a[i] has been changed to the number of 1000s (rounded to//an integer) that were originally in a[i], for all i such that 0 <= i <= size -1.intround(doublenumber);//Precondition: number >= 0.//Returns number rounded to the nearest integer.intmain( ) { using namespace std;intsome_array[4], index; cout <<"Enter 4 numbers to scale: ";for(index = 0; index < 4; index++) cin >> some_array[index]; scale(some_array, 4); cout <<"Values scaled to the number of 1000s are: ";for(index = 0; index < 4; index++) cout << some_array[index] <<" "; cout << endl;return0; }voidscale(inta[],intsize) {for(intindex = 0; index < size; index++) a[index] = round(a[index]/1000.0); }//Uses cmath:intround(doublenumber) { using namespace std;returnstatic_cast<int>(floor(number + 0.5)); }

//DISPLAY 7.8 Production Graph Program//Reads data and displays a bar graph showing productivity for each plant.#include<iostream>#include<cmath>using namespace std;constintNUMBER_OF_PLANTS = 4;voidinput_data(inta[],intlast_plant_number);//Precondition: last_plant_number is the declared size of the array a.//Postcondition: For plant_number = 1 through last_plant_number://a[plant_number-1] equals the total production for plant number plant_number.voidscale(inta[],intsize);//Precondition: a[0] through a[size-1] each has a nonnegative value.//Postcondition: a[i] has been changed to the number of 1000s (rounded to//an integer) that were originally in a[i], for all i such that 0 <= i <= size -1.voidgraph(constintasterisk_count[],intlast_plant_number);//Precondition: asterisk_count[0] through asterisk_count[last_plant_number-1]//have nonnegative values.//Postcondition: A bar graph has been displayed saying that plant//number N has produced asterisk_count[N-1] 1000s of units, for each N such that//1 <= N <= last_plant_numbervoidget_total(int& sum);//Reads nonnegative integers from the keyboard and//places their total in sum.intRound(doublenumber);//Precondition: number >= 0.//Returns number rounded to the nearest integer.voidprint_asterisks(intn);//Prints n asterisks to the screen.intmain( ) {intproduction[NUMBER_OF_PLANTS]; cout <<"This program displays a graph showing\n"<<"production for each plant in the company.\n"; input_data(production, NUMBER_OF_PLANTS); scale(production, NUMBER_OF_PLANTS); graph(production, NUMBER_OF_PLANTS); system("pause");return0; }//Uses iostream:voidinput_data(inta[],intlast_plant_number) { using namespace std;for(intplant_number = 1; plant_number <= last_plant_number; plant_number++) { cout << endl <<"Enter production data for plant number "<< plant_number << endl; get_total(a[plant_number - 1]); } }//Uses iostream:voidget_total(int& sum) { using namespace std; cout <<"Enter number of units produced by each department.\n"<<"Append a negative number to the end of the list.\n"; sum = 0;intnext; cin >> next;while(next >= 0) { sum = sum + next; cin >> next; } cout <<"Total = "<< sum << endl; }voidscale(inta[],intsize) {for(intindex = 0; index < size; index++) a[index] = Round(a[index]/1000.0); }//Uses cmath:intRound(doublenumber) { using namespace std;returnstatic_cast<int>(floor(number + 0.5)); }voidgraph(constintasterisk_count[],intlast_plant_number) { using namespace std; cout <<"\nUnits produced in thousands of units:\n";for(intplant_number = 1; plant_number <= last_plant_number; plant_number++) { cout <<"Plant #"<< plant_number <<" "; print_asterisks(asterisk_count[plant_number - 1]); cout << endl; } }//Uses iostream:voidprint_asterisks(intn) { using namespace std;for(intcount = 1; count <= n; count++) cout <<"*"; }

//DISPLAY 7.9 Partially Filled Array//Shows the difference between each of a list of golf scores and their average.#include<iostream>constintMAX_NUMBER_SCORES = 10;voidfill_array(inta[],intsize,int& number_used);//Precondition: size is the declared size of the array a.//Postcondition: number_used is the number of values stored in a.//a[0] through a[number_used-1] have been filled with//nonnegative integers read from the keyboard.doublecompute_average(constinta[],intnumber_used);//Precondition: a[0] through a[number_used-1] have values; number_used > 0.//Returns the average of numbers a[0] through a[number_used-1].voidshow_difference(constinta[],intnumber_used);//Precondition: The first number_used indexed variables of a have values.//Postcondition: Gives screen output showing how much each of the first//number_used elements of a differs from their average.intmain( ) { using namespace std;intscore[MAX_NUMBER_SCORES], number_used; cout <<"This program reads golf scores and shows\n"<<"how much each differs from the average.\n"; cout <<"Enter golf scores:\n"; fill_array(score, MAX_NUMBER_SCORES, number_used); show_difference(score, number_used); system("pause");return0; }//Uses iostream:voidfill_array(inta[],intsize,int& number_used) { using namespace std; cout <<"Enter up to "<< size <<" nonnegative whole numbers.\n"<<"Mark the end of the list with a negative number.\n";intnext, index = 0; cin >> next;while((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } number_used = index; }doublecompute_average(constinta[],intnumber_used) {doubletotal = 0;for(intindex = 0; index < number_used; index++) total = total + a[index];if(number_used > 0) {return(total/number_used); }else{ using namespace std; cout <<"ERROR: number of elements is 0 in compute_average.\n"<<"compute_average returns 0.\n";return0; } }voidshow_difference(constinta[],intnumber_used) { using namespace std;doubleaverage = compute_average(a, number_used); cout <<"Average of the "<< number_used <<" scores = "<< average << endl <<"The scores are:\n";for(intindex = 0; index < number_used; index++) cout << a[index] <<" differs from average by "<< (a[index] - average) << endl; }

//DISPLAY 7.10 Searching an Array//Searches a partially filled array of nonnegative integers.#include<iostream>using namespace std;constintDECLARED_SIZE = 20;voidfill_array(inta[],intsize,int& number_used);//Precondition: size is the declared size of the array a.//Postcondition: number_used is the number of values stored in a.//a[0] through a[number_used-1] have been filled with//nonnegative integers read from the keyboard.intsearch(constinta[],intnumber_used,inttarget);//Precondition: number_used is <= the declared size of a.//Also, a[0] through a[number_used -1] have values.//Returns the first index such that a[index] == target,//provided there is such an index; otherwise, returns -1.intmain( ) {intarr[DECLARED_SIZE], list_size, target; fill_array(arr, DECLARED_SIZE, list_size);charans;intresult;do{ cout <<"Enter a number to search for: "; cin >> target; result = search(arr, list_size, target);if(result == -1) cout << target <<" is not on the list.\n";elsecout << target <<" is stored in array position "<< result << endl <<"(Remember: The first position is 0.)\n"; cout <<"Search again?(y/n followed by Return): "; cin >> ans; }while((ans !='n') && (ans !='N')); cout <<"End of program.\n"; system("pause");return0; }//Uses iostream:voidfill_array(inta[],intsize,int& number_used) { cout <<"Enter up to "<< size <<" nonnegative whole numbers.\n"<<"Mark the end of the list with a negative number.\n";intnext, index = 0; cin >> next;while((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } number_used = index; }intsearch(constinta[],intnumber_used,inttarget) {intindex = 0;boolfound = false;while((!found) && (index < number_used))if(target == a[index]) found = true;elseindex++;if(found)returnindex;elsereturn-1; }/* int search(const int a[], int number_used, int target) { int index = 0; while(index < number_used) { if (target == a[index]) return index; index++; } return -1; } */

Copyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.

Ideas, requests, problems regarding TWiki? Send feedback

Ideas, requests, problems regarding TWiki? Send feedback