0

>I've changed your function to use pointer arithmetic...
1. *(array+index) - it's exactly array[index] definition in C and C++
2. < *lowest - it's ineffective (slow indirection) variant of the original code with int lowest
3. void sort(int*, int) - it's absolutely the same function type as void sort(int[],int) ;)

0

This is a full code you used?

/***This program dynamically allocates an array large enough to
hold a user-defined number of test scores***/
#include <iostream>
#include <iomanip>
using namespace std;

//Function prototypes
int getnumtestscores();
void gettestscores(int[], int);
void showArray(int [], int);
void sort(int *, int);
double average(int[], int);


int main()
{
	int numts = getnumtestscores();

	int *testscores = new int[numts];		//To dynamically allocate an array

	gettestscores(testscores, numts);

	cout << "The testscores are: \n"; showArray (testscores, numts);

	cout << "The testscores sorted are: \n"; sort (testscores, numts);

	showArray (testscores, numts);

    cout << "The average of all " << numts << " test scores is: " 
         << fixed << showpoint << setprecision(2)
         << average(testscores, numts);
	cout << endl;

    // Free dynamically allocated array in memory
    delete [] testscores;
    // Make scores point to null
    testscores = 0;

	return 0;
}

int getnumtestscores()
{
    int num;

    cout << "\n How many test scores do you want to enter : ";
	cin >> num;
	return num; 
}

void gettestscores(int testscores[], int numts)
{
	cout << "What are the testscores for the students?\n";

	for (int count = 0; count < numts; count++)
	{
		cout << "Testscore " << setw(2) << (count+1) << ":";

		if (testscores < 0)		//Input validation
		{
			cout << "An invalid score was entered, please enter a score more than 0\n";
		}

		cin >> testscores[count];
	}
}

void sort(int *array, int numts)
{
	int lowIndex, *lowest;

	for (int count = 0; count < (numts-1); count++)
	{
		lowIndex = count;
		lowest = array+count;
		
		for (int index = count + 1; index < numts; index++)
		{
			if (*(array+index) < *lowest)
			{
				lowest = array+index;
				lowIndex = index;
			}
		}
		*(array+lowIndex) = *(array+count);
		*(array+count) = *lowest;
	}

	return;
}

void showArray(int array[], int numts)
{
	for (int count = 0; count < numts; count++)
		cout << array[count] <<" \n";
	cout << endl;
}

double average(int array[], int numts)
{
	int total = 0;

	for (int count = 0; count < numts; count++)
	
		total +=array[count];

	  return total/numts;
}

You changed something else? Try this and let me know...it wouldn't have anything to do with my compiler, right? Probably not, I just missed something.

0

>I've changed your function to use pointer arithmetic...
1. *(array+index) - it's exactly array[index] definition in C and C++
2. < *lowest - it's ineffective (slow indirection) variant of the original code with int lowest
3. void sort(int*, int) - it's absolutely the same function type as void sort(int[],int) ;)

I know but he wanted to rewrite his function using pointers, but I agree that using the [] braces is better :P

0

>I've changed your function to use pointer arithmetic...
1. *(array+index) - it's exactly array[index] definition in C and C++
2. < *lowest - it's ineffective (slow indirection) variant of the original code with int lowest
3. void sort(int*, int) - it's absolutely the same function type as void sort(int[],int) ;)

Thanks ArkM, I understand 1 and 3 but I don't understand 2, can you explain? That may be my problem.

0

Well, I don't understand what's your problem. Your original code works, selection sort algorithm is good enough for relatively short arrays. What else?
About note #2.
The more locality in memory access, the better (the faster). The fastest way to get int value is to get it from a local variable. An indirect (via pointer) access requires (potentially) additional addressing register load instruction. The code performs comparison operation on every loop but changes lowest value only if the new lowest candidate is found.
Try to avoid these petty intrigues with local "optimization". Do it only after careful code profiling, do it for real code bottlenecks only.

0

I know but he wanted to rewrite his function using pointers, but I agree that using the [] braces is better :P

Yes I wanted to use pointers so that I could understand how to use them. Pointers are very confusing to me.

0

Well, I don't understand what's your problem. Your original code works, selection sort algorithm is good enough for relatively short arrays. What else?
About note #2.
The more locality in memory access, the better (the faster). The fastest way to get int value is to get it from a local variable. An indirect (via pointer) access requires (potentially) additional addressing register load instruction. The code performs comparison operation on every loop but changes lowest value only if the new lowest candidate is found.
Try to avoid these petty intrigues with local "optimization". Do it only after careful code profiling, do it for real code bottlenecks only.

Ok, just wanted to understand pointers but I gotcha.

1

Yes I wanted to use pointers so that I could understand how to use them. Pointers are very confusing to me.

You have used pointers in a[i] construct because a[i] <=> *((a)+(i)) by the language definition. You have used pointers in sort(int a[],... construct because an array argument converts to the pointer to the 1st element by the language definition. It's not the best code to become familiar with pointer concept and handling. Better try to implement any pointer-based data structure (list or tree, for example).
Otherwise you have a good chance to become OOP programmer (alas, OOP means Only One Project, not Object-Oriented Programming).
;)

Votes + Comments
You're absolutely right :P
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.