Pointers in function

I lost track of pointers at some point. Can anyone help me to find the bug?
Thanks,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// ptrtest1.cpp
// tests sum, maxPtr, minPtr and valueDiff functions

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

// IMPLEMENT ptrfuncs.h FUNCTIONS BELOW

double sum(double *values, int n) {
	int addition = *x;
	for (int i = 0; i < n - 1; i++) {
		addition += *(x + i);
		return addition;
	}
}

double *maxPtr(double *values, int n) {
	int x;
	maxPtr = *x;
	for (int i = 0; i < n; i++) {
		if (*(x + i) > maxPtr)
			maxPtr = *(x + i);
	}
	return *maxPtr;
}

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

double valueDiff(double *left, double *right) {
	double range = maxPtr - minPtr;
	return range;
}
int main() {
    using namespace std;
    double x[] = {
        406.94, 198.63, 961.99, 238.36, 910.21, 188.81, 467.37, 639.6,
        125.1, 581.16, 144.5, 354.07, 812.45, 716.26, 150.35, 753.4,
        387.7, 491.69, 788.64, 624.17, 263.02, 545.16, 908.81, 829.42,
        287.31, 913.75, 811, 245.32, 997.68, 701.87, 806.56, 76.68,
        368.48, 792.79, 645.42, 868.44, 87.5, 702.63, 403.33, 698.98,
        772.66, 793.88, 678.93, 37.28, 328.28, 893.33, 80.62, 721.98,
        213.74, 251.96, 375.7, 18.64, 541.57, 117.97, 315.98, 136.18,
        743.54, 519.56, 863.66, 359.53, 340.57, 283.71, 908.8, 600.22,
        620.95, 684.85, 479.14, 210.61, 685.58, 23.42, 466.19, 641.73,
        985.22, 803.12, 139.89, 106.97, 463.64, 575.13, 461.76, 572.48
    };
    int nx = (sizeof x) / sizeof(double);  // total number of values
    int half = nx / 2;
    
    double *first = x;
    double *max, *min;
    
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    cout << "results for whole set of values:\n";
    cout << "  sum = " << sum(first, nx) << endl;
    max = maxPtr(first, nx);
    cout << "  max = " << *max
        << ", at position " << max - first << endl;
    min = minPtr(first, nx);
    cout << "  min = " << *min
        << ", at position " << min - first << endl;
    cout << "  range = " << valueDiff(max, min) << endl;
    
    cout << "results for first half of values:\n";
    cout << "  sum = " << sum(first, half) << endl;
    max = maxPtr(first, half);
    cout << "  max = " << *max
        << ", at position " << max - first << endl;
    min = minPtr(first, half);
    cout << "  min = " << *min
        << ", at position " << min - first << endl;
    cout << "  range = " << valueDiff(max, min) << endl;
    
    cout << "results for second half of values:\n";
    cout << "  sum = " << sum(first + half, half) << endl;
    max = maxPtr(first + half, half);
    cout << "  max = " << *max
        << ", at position " << max - first << endl;
    min = minPtr(first + half, half);
    cout << "  min = " << *min
        << ", at position " << min - first << endl;
    cout << "  range = " << valueDiff(max, min) << endl;
    
    return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
double sum(double *values, int n) {
    // x isn't defined
    // even if it was, you'd end up 
    // with the wrong sum
    // also, wrong type
    // you're summing an array of doubles
    // initialise it with 0
    int addition = *x;
    // n - 1, means you skip the
    // second last element
    for (int i = 0; i < n - 1; i++) {
        // prefer to use subscript operator
        // i.e., x[i]
        addition += *(x + i);
        // this will make your for loop
        // end prematurely
        // move it outside the loop
        return addition;
    }
}

Last edited on
I got it and it's working, but I kind of have problem some other functions related to this program.
I have to write these two functions and I don't know where to start. Could anyone help me?

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// ptrtest2.c
// tests printTable and sortValues functions

#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\n";
        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] << ".\n";
    
    int half = n / 2;
    int rest = n - half;

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

    cout << "all values in sorted order:\n";
    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;
}
Last edited on
1
2
3
4
5
6
7
8
9
void printTable( double *values, int n, int perRow )
{
    if( values == nullptr ) return;
    for( int i = 0; i < n; i++ ) {
        // print each element of array
        // to know when to start a new line
        // use the modulo operator (%)
    }
}


As for sorting the array, you can just use a simple (but inefficient) sorting algorithm, known as bubble sort. https://www.tutorialspoint.com/data_structures_algorithms/bubble_sort_algorithm.htm
Topic archived. No new replies allowed.