Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. [50 points] After your Part 1 testing is complete, implement the other two fu

ID: 3728195 • Letter: 2

Question

2. [50 points] After your Part 1 testing is complete, implement the other two functions: o void printTable(double *values, int n, int perRow) - print the values as a neatly-formatted table with the specified number of values per row, and each value in a field of width 10 showing 2 significant digits o void sortValues (double *first, double last) sort the values from first through last, where these parameters are pointers into the same dynamic array. Write your own sorting routine, such as the selection sort procedure presented in Chapter 7 - but remember you should use pointer notation, not array notation to do it. You may write helper functions if you want, but they should use pointer notation too Use ptrtest2 .cpp to test your implementations. Compile it as you did above, subsituting ptrtest2 for ptrtest1 in both places. This program expects three command line arguments in this order: an input filename, the number of values to read from the file, and the number of values to print per row. Here are two sample runs using data1.txt as the input file: test2-out.txt. Your results must exactly match if we test your program the same way, but you should ensure the program works for different inputs too.

Explanation / Answer

ptrtest2.cpp


#include <iostream>
#include <cstdlib>
#include <fstream>
#include "ptrfuncs.h"

double *get(const char *filename, int n);

int main(int argc, char *argv[]) {
    using namespace std;
  
    int n; // number of values to process
    double *p; // to point at first value
    int perRow; // number of values to print per row
  
    if (argc < 4) {
        cout << "usage: " << argv[0] << " filename n perRow ";
        return 1;
    }
    n = atoi(argv[2]);
    p = get(argv[1], n);
    perRow = atoi(argv[3]);
  
    if (p == 0) {
        cout << "could not read " << argv[1] << endl;
        return 2;
    }
  
    cout << "Processing first " << n
        << " values from " << argv[1] << ". ";
  
    int half = n / 2;
    int rest = n - half;

    cout << "first " << half << " values: ";
    printTable(p, half, perRow);
    cout << "last " << rest << " values: ";
    printTable(p + rest - 1, rest, perRow);

    cout << "all values in sorted order: ";
    sortValues(p, p + n - 1);
    printTable(p, n, perRow);
  
    delete [] p;
    return 0;
}

double *get(const char *filename, int n) {
    using std::ifstream;
    ifstream in;
    in.open(filename);
    if (in.fail())
        return 0;
    double *data = new double[n];
    for (int i = 0; i < n; i++)
        in >> *(data + i);
    return data;
}

ptrfuncs.h


#ifndef PTRFUNCS_H
#define PTRFUNCS_H

double sum(double *values, int n);

double *maxPtr(double *values, int n);

double *minPtr(double *values, int n);

double valueDiff(double *left, double *right);

void printTable(double *values, int n, int perRow);

void sortValues(double *first, double *last);

#endif

ptrfuncs.cpp


#include <iostream>
#include <iomanip> // to use setw
#include "ptrfuncs.h"

void sort(double *a, int numberUsed);
void swap(double &i, double &j);
int indexOfSmallest(double *a, int start, int numberUsed);

// IMPLEMENT ptrfuncs.h FUNCTIONS BELOW
double sum(double *values, int n) {
    double sum = 0, *i;
    for(i = values; i < values + n; i++) {
        sum += *(i);
    }
    return sum;
  
}
double *maxPtr(double *values, int n) {
    double *i, *max;
    max = values;
    for(i = values; i < values + n; i++) {
        if(*max < *i)
            max = i;
    }
    return max;
}

double *minPtr(double *values, int n) {
    double *i, *min;
    min = values;
    for(i = values; i < values + n; i++) {
        if(*min > *i)
            min = i;
    }
    return min;
}

double valueDiff(double *left, double *right) {
     return *left - *right;
}

void printTable(double *values, int n, int perRow) {
    int counter = 0;
    double *i;
    for (i = values; i < values + n; i++) {
        std::cout << std::setprecision(2) << std::setw(10) << std::fixed << *i;
        counter++;
        if(counter % perRow == 0)
            std::cout << " ";
    }
    if (counter % perRow != 0)
        std::cout << std::endl;
}

void sortValues(double *first, double *last) {
    double *i, *j;
    i = first;
    j = last;
    sort(first, last - first + 1);
}

void sort(double *a, int numberUsed) {
    int indexOfNextSmallest;
    for (int index = 0; index < numberUsed - 1; index++) {
        indexOfNextSmallest = indexOfSmallest(a, index, numberUsed);
        swap(*(a + index), *(a + indexOfNextSmallest));
    }
}

void swap(double &i, double &j) {
    double temp;
    temp = i;
    i = j;
    j = temp;
}

int indexOfSmallest(double *a, int start, int numberUsed) {
    double min = *(a + start);
    int indexOfMin = start;

    for (int index = start + 1; index < numberUsed; index++) {
        if (*(a + index) < min) {
            min = *(a + index);
            indexOfMin = index;
        }
    }
    return indexOfMin;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote