Explore BrainMass

Explore BrainMass

    C++ pointer/array workout

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    Write the following C++ routines using pointers only. Do not use any [] 's in your code.

    1. Write table_fill function that will fill a table of size with the value num.
    table_fill(array, size, num);

    2. Write table_print function to print out a table of size. Print output on one line.
    table_print(array, size);

    3. Write table_print_rev function to print out a table of size in reverse order. Print output on one line.
    table_print_rev(array, size);

    4. Write table_add1 function that will add one to each element in array of size.
    table_add1(array, size);

    5. Write table_addV function that will add val to each element in array of size.
    table_addv(array, size, val);

    6. Write table_max function to find the largest value in an array.
    int table_max (array, size);

    7. Write table_min function to find the smallest value in an array.
    int table_min (array, size);

    8. Write table_avg function to find the average of an array.
    int table_avg(array, size);

    9. Write table_find function to find a value of val in the array of size and returns the position of where the value is, if not found, returns -1.
    int table_find(array, size, val);

    © BrainMass Inc. brainmass.com October 10, 2019, 6:08 am ad1c9bdddf

    Solution Preview

    #include < iostream>
    #include < iomanip>
    #include < cmath>
    #include < fstream>

    using namespace std;

    void table_fill(int *, int, int);
    void table_print (int *, int);
    void table_print_rev (int *, int);
    void table_add1 (int *, int);
    void table_addv (int *, int, int);
    int table_max (int *, int);
    int table_min (int *, int);
    int table_avg (int *, int);
    int table_find (int *, int, int);

    void table_fill (int *p, int max, int num)
    int i;

    for (i = 0; i < max; i++)
    *p = num; // Store the value num in current array element.
    p++; // Move to next higher array element.

    void table_print (int *p, int max)
    int i = 0;

    while (i < max)
    cout << *p << ' ';
    p++; // Point to next higher array element.
    i++; // Seen one more element in the array.

    void table_print_rev (int *array, int size)
    int i = (size - 1);

    array += (size - 1); // Point to the last element of the array;

    while (i >= 0)
    cout << *array << ' ';
    array--; // Point to next lower array element.
    i--; // One lesser array element to print now.

    void table_add1 (int *array, int size)
    int i = 0;

    while (i++ < size) // Using post-increment operator here.
    *array += 1; // Add 1 to the value of current array element.
    array++; // Point to the next higher array element.

    void table_addv (int *array, int size, int val)
    int i = 0;

    while (i++ < size)
    // It is same as writing following two statements:
    // *array += val;
    // array++;
    *array++ += val; // Using post-increment operator here.

    // This ...

    Solution Summary

    Since system has removed the .cpp attachment from solution and instead placed the c++ code in solution text, kindly copy the c++ code from there into a text file with the extension .C or .cpp (indicating C++ code). Kindly verify the code after copying, whether it is same as the code shown in the solution on screen. At times HTML formatting of the text can mess up the C/C++ code.