User Defined Array, Pointer Help

Hello, been lurking on these forums for help these passed couple weeks. Very helpful.

I registered today as I cannot get a program to run. It is for my college assignment and due tonight at midnight.

Essentially, we are to create a program that asks the user to input how many test scores he/she will enter, takes that number and creates an array of that size, then use a pointer to reference to that array.

Once the array is created, we use 2 functions, 1 that sorts the array using a pointer reference again, and puts it in ascending order.

The other function to take the test scores inputted and output the average.

Here is my code at the moment:

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
  //header files
#include <iostream>
#include <iomanip>

using namespace std;

//function prototypes declared
void sort(double*, int);
double average(double*, int);

//main
int main()
{
        //used to declare array size from user input
	int ttlTestScores;
	
	cout << "How many test scores will you enter?: " << endl;
	cin >> ttlTestScores;
	
       //EDIT: made constant variable
       const int SIZE = ttlTestScores;

        //array created with user defined size
	double array[SIZE];
	
        //user input of actual test scores allocated into array
	cout << "Please enter your " << SIZE << " test scores: " << endl;
	for(int i = 0; i < SIZE; i++)
		{
			cout << "Enter test score " << i+1 <<": " << endl;
			cin >> array[i];
		}
        //pointer reference declared to array previously created
	double *pArray = &array;

        //functions called, using pointer reference
	sort(*pArray, ttlTestScores);
	average(*pArray, ttlTestScores);
	
	return 0;
	
}

//sorts array into ascending order
void sort(double*score, int size)
{
	
	 int startScan, minIndex, minValue;
	 for (startScan = 0; startScan < (size - 1); startScan++)
	 {
		minIndex = startScan;
		minValue = array[startScan];
		for (int index = startScan + 1; index < size; index++)
		{
			if (array[index] < minValue)
			{
				minValue = array[index];
				minIndex = index;
			}
		}
		array[minIndex] = array[startScan];
		array[startScan] = minValue;
    }
	
	cout << "In ascending order, your test scores are: " << endl;
	for(int i = 0; i < size; i++)
		cout << array[i];
}

//calculates array average
double average(double*score, int numScores)
{
	double total = 0, average = 0;
	for (int numScores = 0; numScores < size; numScores++)
	{
		total = total + *score[numScores];
	}
	
	average = (*score / numScores);
				
	cout << "Average score is: " << average << endl;
	
	
}



The link to the assignment is : http://web.cerritos.edu/pnguyen/SitePages/cis180/labs/cis180lab7sum17.html

I am a terrible explainer, so putting that there in case I could not make it clearer. Any feedback would be greatly appreciated. I know a main error at the moment is my pointer reference. But anything is helpful.

- Louie

Last edited on
1
2
> //EDIT: made constant variable
> const int SIZE = ttlTestScores;


This won't make SIZE a constant value known at compile time.



1
2
>  //pointer reference declared to array previously created
>  double *pArray = &array;


Use the implicit array to pointer conversion:
1
2
// double *pArray = &array;
double *pArray = array; // do not take he address of the array 

See: http://en.cppreference.com/w/cpp/language/array#Array-to-pointer_decay



Something like this, perhaps:
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
#include <iostream>
#include <iomanip>

void sort( double* array, int size );
double average( const double* array, int size ); // make it const correct

int main()
{
    // maximum possible number of scores
    const int MAX_SCORES = 1000 ; // constant known at compile-time
    double array[MAX_SCORES] ; // fine; MAX_SCORES is a constant known at compile-time

    int size; // actual number of scores entered by the user
    std::cout << "How many test scores will you enter?: " ;
    std::cin >> size;
    if( size > MAX_SCORES ) size = MAX_SCORES ; // do not allow more than MAX_SCORES

    // user input of actual test scores into array
    std::cout << "Please enter your " << size << " test scores: \n" ;
    for( int i = 0; i < size; i++)
    {
        std::cout << "Enter test score " << i+1 <<": " ;
        std::cin >> array[i];
    }

    sort( array, size );
    // print the sorted array

    const double avg = average( array, size );
    // print the average
}
Last edited on
JLBorges,

Thanks much for the feedback. I will definitely take that into consideration. Is it even possible to create an array size defined by the user?
> Is it even possible to create an array size defined by the user?

Yes.
A vector is an array where the size may be known only at run-time; and it can be resized when required.
See: https://cal-linux.com/tutorials/vectors.html
JLBorges,

Thanks again, big help! 1 last question if I may, I am trying to use my pointer to the array created as a parameter in my function calls. In the sorting function I created:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void sort(double*score, int size)
{
	
	 int startScan, minIndex, minValue;
	 for (startScan = 0; startScan < (size - 1); startScan++)
	 {
		minIndex = startScan;
		minValue = array[startScan];
		for (int index = startScan + 1; index < size; index++)
		{
			if (array[index] < minValue)
			{
				minValue = array[index];
				minIndex = index;
			}
		}
		array[minIndex] = array[startScan];
		array[startScan] = minValue;
    }
	
	cout << "In ascending order, your test scores are: " << endl;
	for(int i = 0; i < size; i++)
		cout << array[i];
}


I have double*score as the pointer passed in, but I cant figure out how I would swap out say array[startScan] as my pointer, for example:

minValue = array[startScan];//with array used

would be

minValue = *pArray[startScan]? //with pointer used?

or

if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}

as

if (pArray[index] < minValue)
{
minValue = pArray[index];
minIndex = index;
}


If that makes any sense..sorry I am still very much a beginner and my professor asks we use pointer references where ever we'd normally use an array for. Thanks again so far!
> professor asks we use pointer references where ever we'd normally use an array for

The functions accept a pointer to the first element of the array and its size.

Since the subscript operator [] operates on a pointer and a position, we can access the element
at position pos in an array where the first element is at address first with the expression first[pos]

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
void sort( double* score, int size ) // score is a pointer to the first element of the array
{
    for( int startScan = 0; startScan < (size - 1); ++startScan)
    {
        int minIndex = startScan;
        double minValue = score[startScan];

        // determine the smallest value starting at position startScan and its index
        for( int index = startScan + 1; index < size; ++index )
        {
            if (score[index] < minValue)
            {
                minValue = score[index];
                minIndex = index;
            }
        }

        // bring the smallest value to the front (at position startScan)
        score[minIndex] = score[startScan];
        score[startScan] = minValue;
    }

    /*
    // print out the sorted scores in main()
    std::cout << "In ascending order, your test scores are:\n" ;
    for(int i = 0; i < size; i++) std::cout << score[i] << ' ' ;
    std::cout << '\n' ;
    */
}

//calculates array average
double average( const double* score, int size ) // size > 0
{
    double total = 0 ;

    // add each of the scores to the total
    for( int i = 0 ; i < size ; ++i ) total += score[i] ;

    // divide total by number of scores to get the average
    const double avg = total / size ;

    return avg ; // return the average (print it out in main)
}
Last edited on
JLBorges,

Everything compiled perfectly. Thank you very much! I could not have been able to finish this on my own, thats for sure. Been working on this for many hours off and on from work too for a few days. Really, Thanks mate!




-Louie
Topic archived. No new replies allowed.