Difference: CodeArray1 (1 vs. 8)

Revision 82018-02-20 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"
Added:
>
>

Sort

<-- SyntaxHighlightingPlugin -->
#include <iostream>
using namespace std;
void bar(int size);
void inputData(int data[], int max, int &values);
void outputData(int data[], int values);
void removeLow(int data[], int &values);
int lowest(int data[], int s, int e);
void sort(int data[], int values);

int main() {

    const int SIZE = 100;
    int score[SIZE];
    int curSize;

    inputData(score, SIZE, curSize);
    outputData(score, curSize);
    sort(score,curSize);
    outputData(score, curSize);
    return 0;
}

void inputData(int data[], int max, int &values) {
    cout << "Enter up to " << max << " scores, -1 when done.\n";
    values = 0;
    int v;
    do {
        cin >> v;
        if (v >= 0) {
            data[values++] = v;
        }
    } while (values << max && v >= 0);
}

void outputData(int data[], int values) {
    for (int i = 0; i < values; i++) {
        cout << data[i] << " ";
        //bar(data[i]);
    }
    cout << endl;
}

void removeLow(int data[], int &values) {
    int low = 0;
    for (int i = 1; i < values; i++) {
        if (data[i] < data[low]) {
            low = i;
        }
    }
    data[low] = data[values - 1];
    values--;
}

void bar(int size) {
    for (int i = 0; i < size; i++) {
        cout << "*";
    }
    cout << endl;

}

int lowest(int data[], int s, int e){
    int low = s;
    for (int i=s+1; i<=e; i++) {
        if (data[i]<data[low]){
            low = i;
        }
    }
    return low;
}

void sort(int data[], int values){
    for (int i=0; i<values; i++) {
        int low=lowest(data, i, values-1);
        //data[low]<=>data[i]
        int temp=data[low];
        data[low]=data[i];
        data[i]=temp;
    }
    cout << endl;    
}
 
<-- end SyntaxHighlightingPlugin -->
 

Scores

%CODE{"c++"}%#include

Revision 72018-02-20 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"
Added:
>
>

Scores

<-- SyntaxHighlightingPlugin -->
#include <iostream>
using namespace std;
void bar(int size);
int main( )
{

    const int SIZE = 5;
    int i, score[SIZE], max;

    cout << "Enter " << SIZE << " scores:\n";
    cin >> score[0];
    max = score[0];
    for (i = 1; i < SIZE; i++)
    {
        cin >> score[i];
        if (score[i] > max)
            max = score[i];
        //max is the largest of the values score[0],..., score[i].
    }

    for (i = 0; i < SIZE; i++) {
        cout << score[i] << " ";
        bar(score[i]);
    }
    return 0;
}

void bar(int size){
    for (int i=0; i<size; i++){
        cout << "*";
    }
    cout << endl;
  
}
 
<-- end SyntaxHighlightingPlugin -->
 

Functions Sample code

Program to drop low score in array using functions

Revision 62017-02-28 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Functions Sample code

Deleted:
<
<
 
Changed:
<
<

Program to drop low score in array

>
>

Program to drop low score in array using functions

 %CODE{"c++"}% #include
Deleted:
<
<
 using namespace std;
Added:
>
>
void getScores(int a[], int aSize, int &scores); void displayScores(int a[], int size); void dropLowest(int a[], int &scores);
 
Deleted:
<
<
/* * */
 int main() {
Changed:
<
<
const int MAX = 10; int a[MAX];
>
>
const int ARRAYSIZE = 10; int numQuizzes, numTests; int quizzes[ARRAYSIZE], tests[ARRAYSIZE];

cout << "Enter Test scores:" << endl; getScores(tests, ARRAYSIZE, numTests); dropLowest(tests, numTests);

cout << "Enter quiz scores:" << endl; getScores(quizzes, ARRAYSIZE, numQuizzes); dropLowest(quizzes, numQuizzes);

cout << "Tests" << endl; displayScores(tests, numTests);

cout << "Quizzes" << endl; displayScores(quizzes, numQuizzes); }

void getScores(int a[], int aSize, int &scores) { cout << "Enter up to " << aSize << " grades, negative to stop:" << endl;

  int i = 0;
Changed:
<
<
int count = 0; cout << "Enter up to 10 grades, negative to stop:" << endl; // input loop
>
>
scores = 0;
  do {
Changed:
<
<
cout << "Enter Element " << i+1 << ":";
>
>
cout << "Enter grade " << i + 1 << ":";
  cin >> a[i];
Changed:
<
<
if (a[i]>= 0) { count++; }
>
>
if (a[i] >= 0) { scores++; }
  i = i+1;
Changed:
<
<
} while (a[i-1]>=0 && i < MAX);
>
>
} while (a[i - 1] >= 0 && i < aSize); }
 
Changed:
<
<
cout << "Num: " << count << endl;
>
>
void displayScores(int a[], int size) {
  // display array
Changed:
<
<
for (int i=0; i < count; i++ ) { cout << a[i] << " ";
>
>
for (int i = 0; i < size; i++) { cout << "Score " << i + 1 << ":" << a[i] << endl;
  }
Changed:
<
<
cout << endl;
>
>
}

void dropLowest(int a[], int& scores) {

  // remove the lowest int lowI = 0;
Changed:
<
<
for (int i=1; i<count; i++) {
>
>
for (int i=1; i<scores; i++) {
  if (a[i] < a[lowI]) { lowI = i; }
Line: 39 to 59
  lowI = i; } }
Deleted:
<
<
  // remove element
Changed:
<
<
a[lowI] = a[--count];

// display array for (int i=0; i < count; i++ ) { cout << a[i] << " "; } cout << endl; return 0;

>
>
a[lowI] = a[--scores];
 }
Added:
>
>
  %ENDCODE%

Program Using an Array

Revision 52017-02-23 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Functions Sample code

Added:
>
>

Program to drop low score in array

<-- SyntaxHighlightingPlugin -->
#include <iostream>

using namespace std;

/*
 * 
 */
int main() {
    const int MAX = 10;
    int a[MAX];
    int i = 0;
    int count = 0;
    cout << "Enter up to 10 grades, negative to stop:" << endl;
    // input loop
    do {
        cout << "Enter Element " << i+1 << ":";
        cin >> a[i];
        if (a[i]>= 0) { count++; }
        i = i+1;
    } while (a[i-1]>=0 && i < MAX);
    
    cout << "Num: " << count << endl;  
    // display array
    for (int i=0; i < count; i++ ) {
        cout << a[i] << " ";
    }
    cout << endl;
    // remove the lowest
    int lowI = 0;
    for (int i=1; i<count; i++) {
        if (a[i] < a[lowI]) {
            lowI = i;
        }
    }
    
    // remove element 
    a[lowI] = a[--count];
    
    // display array
    for (int i=0; i < count; i++ ) {
        cout << a[i] << " ";
    }
    cout << endl;
    return 0;
}
 
<-- end SyntaxHighlightingPlugin -->
 

Program Using an Array

%CODE{"c++"}%

Revision 42016-10-11 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Functions Sample code

Line: 706 to 696
 

%ENDCODE% \ No newline at end of file

Added:
>
>

Program Using an Array

<-- SyntaxHighlightingPlugin -->
#include <iostream>
using namespace std;
void displayScores(int scores[], int scoreCount) {
    for (int i = 0 ; i < scoreCount ; i++) {
        cout << scores[i] << " ";
    }
    cout << endl;
}

void inputScores(int scores[], int aSize, int& scoreCount) {
    int i=0;
    cout << "Input scores, -1 to end: ";
    do {
        cin >> scores[i++];  
    } while (scores[i-1] > 0 && i < aSize);
    if (scores[i-1] < 0) {
        i--;
    }
    scoreCount = i;
}

void dropLowest(int scores[], int& scoreCount) {
    int lowest = 0;
    for (int i = 1 ; i < scoreCount ; i++) {
        if (scores[i] < scores[lowest]) {
            lowest = i;
        }
    }    
    scoreCount--;
    scores[lowest] = scores[scoreCount];
}

double average(int scores[], int& scoreCount) {
    int sum = 0;
    double ave;
    for (int i = 0 ; i < scoreCount ; i++) {
        sum += scores[i];
    }    
    ave = static_cast<double>(sum)/scoreCount;
    return ave;
}
const int MAX = 100;
int main() {
    int theScores[MAX];
    int scount;
    
    inputScores(theScores, MAX, scount);
    displayScores(theScores, scount);
    dropLowest(theScores, scount);
    displayScores(theScores, scount);
    dropLowest(theScores, scount);
    displayScores(theScores, scount);   
    
    cout << "You Average is " << average(theScores, scount) << endl;
    
    return 0;
}


 
<-- end SyntaxHighlightingPlugin -->
 \ No newline at end of file

Revision 32015-10-01 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Functions Sample code

Line: 334 to 334
  input_data(production, NUMBER_OF_PLANTS); scale(production, NUMBER_OF_PLANTS); graph(production, NUMBER_OF_PLANTS);
Deleted:
<
<
system("pause");
  return 0; }
Line: 442 to 441
  cout << "Enter golf scores:\n"; fill_array(score, MAX_NUMBER_SCORES, number_used); show_difference(score, number_used);
Deleted:
<
<
system("pause");
  return 0; } //Uses iostream:
Line: 544 to 542
  }while ((ans = 'n') && (ans = 'N'));

cout << "End of program.\n";

Deleted:
<
<
system("pause");
  return 0; } //Uses iostream:
Line: 603 to 600
  %ENDCODE%
Changed:
<
<

Name

>
>

Sorting an Array

  %CODE{"c++"}%
Added:
>
>
//DISPLAY 7.12 Sorting an Array //Tests the procedure sort. #include using namespace std;

void fill_array(int a[], int size, 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.

void sort(int a[], int number_used); //Precondition: number_used <= declared size of the array a. //The array elements a[0] through a[number_used - 1] have values. //Postcondition: The values of a[0] through a[number_used - 1] have //been rearranged so that a[0] <= a[1] <= ... <= a[number_used - 1].

void swap_values(int& v1, int& v2); //Interchanges the values of v1 and v2.

int index_of_smallest(const int a[], int start_index, int number_used); //Precondition: 0 <= start_index < number_used. Referenced array elements have //values. //Returns the index i such that a[i] is the smallest of the values //a[start_index], a[start_index + 1], ..., a[number_used - 1].

int main( ) { using namespace std; cout << "This program sorts numbers from lowest to highest.\n";

int sample_array[10], number_used; fill_array(sample_array, 10, number_used); sort(sample_array, number_used);

cout << "In sorted order the numbers are:\n"; for (int index = 0; index < number_used; index++) cout << sample_array[index] << " "; cout << endl; return 0; }

//Uses iostream: void fill_array(int a[], int size, int& number_used) {

cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; }

number_used = index; }

void swap_values(int& v1, int& v2) { int temp; temp = v1; v1 = v2; v2 = temp; }

void sort(int a[], int number_used) { int index_of_next_smallest; for (int index = 0; index < number_used - 1; index++) {//Place the correct value in a[index]:

index_of_next_smallest = index_of_smallest(a, index, number_used);

swap_values(a[index], a[index_of_next_smallest]); //a[0] <= a[1] <=...<= a[index] are the smallest of the original array //elements. The rest of the elements are in the remaining positions. } }

int index_of_smallest(const int a[], int start_index, int number_used) { int min = a[start_index]; int index_of_min = start_index; for (int index = start_index + 1; index < number_used; index++) if (a[index] < min) { min = a[index]; index_of_min = index; //min is the smallest of a[start_index] through a[index] }

return index_of_min; }

  %ENDCODE%

Revision 22015-09-30 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Functions Sample code

Line: 79 to 79
 } %ENDCODE%
Changed:
<
<

Name

>
>

Function with an Array Parameter

  %CODE{"c++"}%
Added:
>
>
//DISPLAY 7.4 Function with an Array Parameter //Function Declaration void fill_up(int a[], int size);

//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: void fill_up(int a[], int size) { using namespace std; cout << "Enter " << size << " numbers:\n"; for (int i = 0; i < size; i++) cin >> a[i]; size--; cout << "The last array index used is " << size << endl; }

  %ENDCODE%
Changed:
<
<

Name

>
>

Outline of the Graph Program

<-- SyntaxHighlightingPlugin -->
//DISPLAY 7.5 Outline of the Graph Program
//Reads data and displays a bar graph showing productivity for each plant.
#include <iostream>
const int NUMBER_OF_PLANTS = 4;


void input_data(int a[], int last_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.


void scale(int a[], int size);
//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.


void graph(const int asterisk_count[], int last_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_number


int main( )
{
    using namespace std;
    int production[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);

    return 0;
}
 
<-- end SyntaxHighlightingPlugin -->

Test of Function input_data

<-- SyntaxHighlightingPlugin -->
//DISPLAY 7.6 Test of Function input_data
//Tests the function input_data.
#include <iostream>
const int NUMBER_OF_PLANTS = 4;

void input_data(int a[], int last_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.

void get_total(int& sum);
//Reads nonnegative integers from the keyboard and
//places their total in sum.

int main( )
{
    using namespace std;
    int production[NUMBER_OF_PLANTS];
    char ans;

    do
    {
        input_data(production, NUMBER_OF_PLANTS);
        cout << endl
             << "Total production for each"
             << " of plants 1 through 4:\n";
        for (int number = 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;

    return 0;
}
//Uses iostream:
void input_data(int a[], int last_plant_number)
{
    using namespace std;
    for (int plant_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:
void get_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;
    int next;
    cin >> next;
    while (next >= 0)
    {
        sum = sum + next;
        cin >> next;
    }

    cout << "Total = " << sum << endl;
}


 
<-- end SyntaxHighlightingPlugin -->

The Function scale

<-- SyntaxHighlightingPlugin -->
//DISPLAY 7.7 The Function scale
//Demonstration program for the function scale.
#include <iostream>
#include <cmath>

void scale(int a[], int size);
//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.

int round(double number);
//Precondition: number >= 0. 
//Returns number rounded to the nearest integer.

int main( )
{
    using namespace std;
    int some_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;

    return 0;
}

void scale(int a[], int size)
{
    for (int index = 0; index < size; index++)
        a[index] = round(a[index]/1000.0);
}

//Uses cmath:
int round(double number)
{
    using namespace std;
    return static_cast<int>(floor(number + 0.5));
}


 
<-- end SyntaxHighlightingPlugin -->

Production Graph Program

<-- SyntaxHighlightingPlugin -->
//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;

const int NUMBER_OF_PLANTS = 4;

void input_data(int a[], int last_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.

void scale(int a[], int size);
//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.
void graph(const int asterisk_count[], int last_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_number

void get_total(int& sum);
//Reads nonnegative integers from the keyboard and
//places their total in sum.

int Round(double number);
//Precondition: number >= 0. 
//Returns number rounded to the nearest integer.

void print_asterisks(int n);
//Prints n asterisks to the screen.

int main( )
{
    
    int production[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");
    return 0;
}

//Uses iostream:
void input_data(int a[], int last_plant_number)
{
    using namespace std;
    for (int plant_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:
void get_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;
    int next;
    cin >> next;
    while (next >= 0)
    {
        sum = sum + next;
        cin >> next;
    }

    cout << "Total = " << sum << endl;
}

void scale(int a[], int size)
{
    for (int index = 0; index < size; index++)
        a[index] = Round(a[index]/1000.0);
}

//Uses cmath:
int Round(double number)
{
    using namespace std;
    return static_cast<int>(floor(number + 0.5));
}
void graph(const int asterisk_count[], int last_plant_number)
{
    using namespace std;
    cout << "\nUnits produced in thousands of units:\n";
    for (int plant_number = 1; 
                plant_number <= last_plant_number; plant_number++)
    {
        cout << "Plant #" << plant_number << " ";
        print_asterisks(asterisk_count[plant_number - 1]);
        cout << endl;
    }
}

//Uses iostream:
void print_asterisks(int n)
{
    using namespace std;
    for (int count = 1; count <= n; count++)
        cout << "*";
}


 
<-- end SyntaxHighlightingPlugin -->

Partially Filled Array

<-- SyntaxHighlightingPlugin -->
//DISPLAY 7.9 Partially Filled Array
//Shows the difference between each of a list of golf scores and their average.
#include <iostream>
const int MAX_NUMBER_SCORES = 10;
 
void fill_array(int a[], int size, 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.

double compute_average(const int a[], int number_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].

void show_difference(const int a[], int number_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.

int main( )
{
    using namespace std;
    int score[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");
    return 0;
}
//Uses iostream:
void fill_array(int a[], int size, 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";
    int next, index = 0;
    cin >> next;
    while ((next >= 0) && (index < size))
    {
        a[index] = next;
        index++;
        cin >> next;
    }

    number_used = index;
}

double compute_average(const int a[], int number_used)
{
    double total = 0;
    for (int index = 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";
        return 0;
    }
}

void show_difference(const int a[], int number_used)
{
    using namespace std;
    double average = compute_average(a, number_used);
    cout << "Average of the " << number_used 
         << " scores = " << average << endl
         << "The scores are:\n";
    for (int index = 0; index < number_used; index++)
    cout << a[index] << " differs from average by "
         << (a[index] - average) << endl;
}

 
<-- end SyntaxHighlightingPlugin -->

Searching an Array

  %CODE{"c++"}%
Added:
>
>
//DISPLAY 7.10 Searching an Array //Searches a partially filled array of nonnegative integers. #include using namespace std; const int DECLARED_SIZE = 20;

void fill_array(int a[], int size, 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.

int search(const int a[], int number_used, int target); //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.

int main( ) {

int arr[DECLARED_SIZE], list_size, target;

fill_array(arr, DECLARED_SIZE, list_size);

char ans; int result; 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"; else cout << 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"); return 0; } //Uses iostream: void fill_array(int a[], int size, int& number_used) {

cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; }

number_used = index; }

int search(const int a[], int number_used, int target) {

int index = 0; bool found = false; while ((found) && (index < number_used)) if (target == a[index]) found = true; else index++;

if (found) return index; else return -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; } */

  %ENDCODE%

Revision 12015-09-30 - JimSkon

Line: 1 to 1
Added:
>
>
META TOPICPARENT name="WebHome"

Functions Sample code

Program Using an Array

<-- SyntaxHighlightingPlugin -->
//DISPLAY 7.1 Program Using an Array
//Reads in 5 scores and shows how much each
//score differs from the highest score.
#include <iostream>

int main( )
{
    using namespace std;
    int i, 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;

    return 0;
}
 
<-- end SyntaxHighlightingPlugin -->

Indexed Variable as an Argument

<-- SyntaxHighlightingPlugin -->
//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>

const int NUMBER_OF_EMPLOYEES = 3;

int adjust_days(int old_days);
//Returns old_days plus 5.

int main( )
{
    using namespace std;
    int vacation[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;

    return 0;
}

int adjust_days(int old_days)
{
    return (old_days + 5);
}
 
<-- end SyntaxHighlightingPlugin -->

Name

<-- SyntaxHighlightingPlugin -->

<-- end SyntaxHighlightingPlugin -->

Name

<-- SyntaxHighlightingPlugin -->

<-- end SyntaxHighlightingPlugin -->

Name

<-- SyntaxHighlightingPlugin -->

<-- end SyntaxHighlightingPlugin -->
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 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