Need help on pointers and dynamically allocated arrays.

Hello this is for an assignment. I'm not looking for the answer but rather a point in the right direction. The areas where I have comments are places I still need to fill in. And I need help knowing if what little I have right now is right or very wrong. I haven't done programming in a while and have suffered for it.

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include<iostream>
#include<iomanip>
#include<string>

using namespace std;

// Function prototypes
	double* getScores(int &); // calls getScores function
	void sort(double*, int); // calls sort function
	double calcAvg(double*, int); // calls calcAvg function
	void displayRpt(double*, int, double); // calls displayRpt function

int main()
{

//Declares a pointer to an array of doubles.


// Welcome message
	cout << "Welcome to my Quiz Score Calculator." << endl;
	cout << endl;

// Free array


	system("pause");
// Exit
	return 0;
}
double* getScores(int &)
{
// Promp for number of scores
	int *arrayPtr = NULL;
	cout << "How many test scores will you enter? " << endl;
	int scores;
	cin >> scores;

// Dyanamically allocate array
	arrayPtr = new int[scores];

	int temp;

// Loop and prompt for scores
	for (int i = 0; i < scores; i++)
	{
		cout << "Enter test score " << i + 1 << ":" << endl;
		cin >> temp;
		*(arrayPtr + i) = temp;
	}

// Do not accept negative scores
	while(scores < 0)
	{
		cout << "Negative scores not accepted." << endl;
		cin >> scores[arrayPtr];
	}

//Returns arrayPtr.
	return arrayPtr;
}
void sort(double*, int)
{
// Loop through array for all elements
	int i, temp, swapped;

// Loop through array + 1 finding lowest element
	for (i = 0; i < numOfScores; i++)
	{
		scores[i] = (scores + 1);
	}

// Swap with outer loop element
	while (1)
	{
		swapped = 0;

		for (i = 0; i < numOfScores - 1; i++)
		{
			if (scores[i]>scores[i + 1])
			{
				int temp = scores[i];
				scores[i] = scores[i + 1];
				scores[i + 1] = temp;
				swapped = 1;
			}
		}
			if (swapped == 0)
			{
				break;
			}

	}
}
double calcAvg(double*, int)
{
// Loop through array

// Sum each average

// Calculate average

}
void displayRpt(double*, int, double)
{
// Print headings
	cout << "The test scores in ascending order, and their average, are: " << endl;
	cout << endl;

// Loop through array and print scores
	cout << "Score" << endl;
	cout << "-----" << endl;
	cout << endl;

// Print average score
	cout << "Average score: " << endl;

}


The sample run is supposed to look like this.
Welcome to Pat Programmer's Quiz Score Calculator

How many test scores will you enter? 5
Enter test score 1: 60.00
Enter test score 2: 70.00
Enter test score 3: 80.00
Enter test score 4: 90.00
Enter test score 5: 100.00

The test scores in ascending order, and their averages, are:

Score
-----

60.00
70.00
80.00
90.00
100.00

Average score: 80.00
Not sure what you are asking. You have an int pointer and allocate it into a dynamic array correctly.

I personally avoid pointer syntax in favor of array syntax
*(arrayPtr + i) = temp;
looks like
arrayPtr[i] = temp;

it looks good, just incomplete.

the sort seems odd, with the while(1). The standard sorts would just loop using the array's size. I am not sure the sort is correct or not, but if it is working, leave it be and focus on the other stuff.

Given what is there I can't see anything you should not be able to do. If the sort works, I would say its on the right track.



Sorry yeah it's very incomplete right now.

I'm pretty new to pointers and this assignment I'm supposed to use them. I've also never done bubblesort before and I'm supposed to use that in order to get the user input sorted in ascending order. I'm not sure I'm doing it right personally.

The comment
// declares a pointer to an array of doubles

I don't think I know the proper syntax.

About the bubblesort: https://en.wikipedia.org/wiki/Bubble_sort

In getScores you know how to create an pointer to an int array. Just change int to double.
BTW. getScores is supposed to return a double* but you return an int*
sure you do :)


int *arrayPtr = NULL;
cout << "How many test scores will you enter? " << endl;
int scores;
cin >> scores;

// Dyanamically allocate array
arrayPtr = new int[scores];

for a double, change int to double: //losing the pointless null assignment and rolling into one statement
double * d = new double[some_size];

bubble sort is a pair of for loops over the size of the array. the inner loop moves the smallest value to the top, and the outer loop cuts the array down by 1 each time and does the inner loop on the new, reduced array (because you skip the sorted values that you already moved).

so 4321
inner loop 1 moves 1: 1432
inner loop 2 moves 2: 1243
inner loop 3 moves 3: 1234
inner loop 4 moves 4 which happens to be correct (you can skip this last iteration if you like).

Topic archived. No new replies allowed.