function to display dynamic array

Hello I have written this program
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
#include <iostream>
#include <array>

using namespace std;



double avg(const double *begin, const double * end)
{
	const double * pt;
	double total = 0;
	
	{

	for (pt = begin; pt != end; pt++)
			total = total + *pt;
	}
	
	return (total/(end - begin));
}
int main()
{
	cout << "Hello Do You wish To Enter Your Golf Scores ?" << endl;
	cout << "If Yes Enter Y, If No Enter N" << endl;
	char ans;
	std::cin >> ans;
	
	if (ans == 'N')
	{
		cout << "Good Bye!" << endl;
	}
	else if (ans == 'Y')
	{
		cout << "Please Enter The Number Of Scores You wish To Record, Up To 10 Different Scores" << endl;
		int size;
		std::cin >> size;

		double*p = new double[size];

		if (size > 10)
		{
			cout << "ILLEAGAL NUMBER OF SCORES ENTERED" << endl;
		}

		else if (size == 0)
		{
			cout << "ILLEAGAL NUMBER OF SCORES ENTERED" << endl;
		}
		else 
		for (int i = 0; i < size; i++)
		{
			cout << "Please Enter The Scores You Wish To Record" << endl;
			std::cin >> p[i];

		}
		cout << "The Scores You Have Entered Are " << endl;
		{
			for (int i = 0; i < size; i++)
				cout << p[i] << ",";
		}
		cout << " " << endl;
		cout << "Your First 3 Scores Are: " << endl;
		cout << p[0] << ',' << p[1] << ',' << p[2] << endl;
		cout << "Their Average Is " << avg(p + 0, p + 3) << endl;

		cout << "Your last 3 Scores Are: " << endl;
		cout << p[7] << ',' << p[8] << ',' << p[9] << endl;

		
	}	
	else
	{
		cerr << "Error: Array Size Is Illegal" << endl;
		exit(EXIT_FAILURE);
	}
	

	

	std::cin.get();
	std::cin.get();

	return 0;
}


I am trying to write a Function that will display the whole array or specific parts of the array. Say the first 3 positions how can I do that?
You already have the code for the 1st 3 positions, what's not working is the last 3.
you do not need line 57 and 60
line 71-75 doesn't do what you think it does.

To make a function, take the code from main() and put in your function.
You have one called avg, so you already know how to make a function.


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
#include <iostream>
// #include <array>

using namespace std;

double avg(const double *begin, const double * end)
{
	const double * pt;
	double total = 0;
	{
	for (pt = begin; pt != end; pt++)
			total = total + *pt;
	}
	return (total/(end - begin));
}

int myfunction ()
{
			cout << "Please Enter The Number Of Scores You wish To Record, Up To 10 Different Scores" << endl;
		int size;
		std::cin >> size;

		double*p = new double[size];

		if (size > 10)
			{
				cout << "ILLEAGAL NUMBER OF SCORES ENTERED" << endl;
			}

		else if (size == 0)
			{
				cout << "ILLEAGAL NUMBER OF SCORES ENTERED" << endl;
			}
		else 
		for (int i = 0; i < size; i++)
			{
				cout << "Please Enter The Scores You Wish To Record" << endl;
				std::cin >> p[i];
	
			}

		cout << "The Scores You Have Entered Are " << endl;
		for (int i = 0; i < size; i++)
			cout << p[i] << ",";
		cout << " " << endl;
		cout << "Your First 3 Scores Are: " << endl;
		cout << p[0] << ',' << p[1] << ',' << p[2] << endl;
		cout << "Their Average Is " << avg(p + 0, p + 3) << endl;

		cout << "Your last 3 Scores Are: " << endl;
		cout << p[size-3] << ',' << p[size-2] << ',' << p[size-1] << endl;
}	




int main()
{
	cout << "Hello Do You wish To Enter Your Golf Scores ?" << endl;
	cout << "If Yes Enter Y, If No Enter N" << endl;
	char ans;
	std::cin >> ans;
	
	if (ans == 'N')
		{
			cout << "Good Bye!" << endl;
		}
	
	else if (ans == 'Y')
		{
	myfunction();
		}

	return 0;
}




Hello Do You wish To Enter Your Golf Scores ?
If Yes Enter Y, If No Enter N
Y
Please Enter The Number Of Scores You wish To Record, Up To 10 Different Scores
5
Please Enter The Scores You Wish To Record
11 22 33 44 55
Please Enter The Scores You Wish To Record
Please Enter The Scores You Wish To Record
Please Enter The Scores You Wish To Record
Please Enter The Scores You Wish To Record
The Scores You Have Entered Are
11,22,33,44,55,
Your First 3 Scores Are:
11,22,33
Their Average Is 22
Your last 3 Scores Are:
33,44,55
Last edited on
Topic archived. No new replies allowed.