Convert Program to use pointer arithmetic?

I am supposed to convert a program that was given to me to use pointer arithmetic. I really do not understand how to use pointer arithmetic and want to know how it will be to convert this program. The program calculates wind chill.

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
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;

void instruct();

void readin(double num[], int& n);

int count(int n);

double calcMean(double num[], int n);

double calcDev(double num[], double mean, int n);

int main()
{
 //declarations
 int n;
 double num[100], mean, stdDev;

//input
instruct();

readin(num, n);

count(n);
//processing
mean = calcMean(num, n);

stdDev = calcDev(num, mean, n);
//output
cout << "Standard Deviation: " << stdDev << endl;
cout << "Mean: " << mean << endl;
}

void instruct()
{
cout << "This program calculates the mean and standard deviation of house prices. " << endl;
}

void readin(double num[], int& n)
{
  ifstream infile;
  infile.open("prices.txt");
  n = 0;
  while (infile >> num[n]) n++;
}

 int count(int n)
{
  n = 0;
  double data;
  ifstream fin;
  fin.open("prices.txt");
  while (fin >> data) n++;
  fin.close();
  return n;
}

double calcMean(double num[], int n)
{
 double sum = 0.0, mean = 0.0;
 int i;

  for(i = 0; i < n; i++)
  {
    sum += num[i];
  }

  mean = sum/n;

  return mean;
}

double calcDev(double num[], double mean, int n)
{
int i;
double stdDev = 0.0;
  for(i = 0; i < n; i++)
  {
    stdDev += pow(num[i] - mean, 2);
  }

  return sqrt(stdDev / n);
}

Here's a start, follow the pattern:

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
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;

double calcMean(const double* , const int* );

int main()
{
    // TEST
    int n{3};
    double num[]{5.3, 12.2, 13.1};
    
    //processing
    double mean = calcMean(num, &n);
    std::cout << "Mean: " << mean << '\n';
    
    //ALTERNATIVELY
    int j{2};
    double* arr = new double[j];
    arr[0] = 5.6;
    arr[1] = 6.8;
    std::cout << "Mean: " << calcMean(arr, &j) << '\n';
    
    delete[] arr;
}

double calcMean(const double* array, const int* how_many)
{
    double sum = 0.0, mean = 0.0;
    for(int i = 0; i < *how_many; i++)
    {
        sum += array[i];
    }
    
    mean = sum / *how_many; // MAYBE SHOULD CHECK NOT ZERO?
    
    return mean;
}


Mean: 10.2
Mean: 6.2
Program ended with exit code: 0
> The program calculates wind chill.
Does it?
 
cout << "This program calculates the mean and standard deviation of house prices. " 


Your attempt at calling count is broken.
It's a good job that readin doesn't depend on it working at all.
I tried to replicate what you did into my program but I just cant seem to get it to work. I tried to modify my calcMean function to use the pointers but it won't compile and I am completely lost

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
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
//pre: none
//post: user is aware of what program is supposed to do
void instruct();
//pre: none
//post: num[] is properly initialized
void readin(double num[], int& n);
//pre: none
//post: data is properly counted and returned
int count(int n);
//pre: num[] is properly initialized
//post: mean is calculated
double calcMean(const double* num[], const int* n);
//pre: num[] is properly initialized
//post: stdDev is calculated
double calcDev(double num[], double mean, int n);

int main()
{

//declarations
 int n;
 double mean, stdDev;
 double* num = new double[i];
//input
instruct();

readin(num, n);

count(n);
//processing
mean = calcMean(num, &n);

stdDev = calcDev(num, mean, n);
//output
cout << "Standard Deviation: " << stdDev << endl;
cout << "Mean: " << mean << endl;
}

void instruct()
{
cout << "This program calculates the mean and standard deviation of house price$
}
void readin(double num[], int& n)
{
  ifstream infile;
  infile.open("prices.txt");
  n = 0;
  while (infile >> num[n]) n++;
}

int count(int n)
{
  n = 0;
  double data;
  ifstream fin;
  fin.open("prices.txt");
  while (fin >> data) n++;
  fin.close();
  return n;
}

double calcMean(const double* num[], const int* n)
{
 double sum = 0.0, mean = 0.0;
 double* num = new double n[];
 int i;

  for(i = 0; i < n; i++)
  {
    sum += *(num + i);
  }

  mean = sum/n;

  return mean;
}

double calcDev(double num[], double mean, int n)
{
int i;
double stdDev = 0.0;
  for(i = 0; i < n; i++)
  {
    stdDev += pow(num[i] - mean, 2);
  }

  return sqrt(stdDev / n);
}

 
Last edited on
For a start you need to forget about testing and debugging in one huge slab of code. You are wasting your time.

Do each one of the functions separately with a main and just one function. Leave readin() and count() to last.

Create the test data in main() exactly the way I did and through the pointer parameters transfer/send the relevant info to be processed by the function.

I would do calcDev() next. Use variable names etc consistently with calcMean() so when these updated functions are put back in to replace those in your original slab they should work easily.


Consider (not tried):

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
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;

void instruct();
void readin(double* num, int n);
int count();
double calcMean(double* num, int n);
double calcDev(double* num, double mean, int n);

int main()
{
	instruct();

	const int n {count()};
	auto num {new double[n] {}};

	readin(num, n);

	const auto mean {calcMean(num, n)};

	cout << "Standard Deviation: " << calcDev(num, mean, n) << endl;
	cout << "Mean: " << mean << endl;
}

void instruct()
{
	cout << "This program calculates the mean and standard deviation of house prices. " << endl;
}

void readin(double* num, int n)
{
	ifstream infile("prices.txt");

	for (int m = 0; m < n && infile >> *(num + m); ++m);
}

int count()
{
	int n {};
	ifstream fin("prices.txt");

	for (double data {}; fin >> data; ++n);

	return n;
}

double calcMean(double* num, int n)
{
	double sum {};

	for (int i = 0; i < n; ++i)
		sum += *(num + i);

	return sum / n;
}

double calcDev(double* num, double mean, int n)
{
	double stdDev {};

	for (int i = 0; i < n; ++i)
		stdDev += pow(*(num + i) - mean, 2);

	return sqrt(stdDev / n);
}

Topic archived. No new replies allowed.