Hello,

I would really appreciate if someone could tell me what is wrong with this function. When I execute the program it returns the memory address and not the calculated average. I have not included the entire program, there is a sort function and scores are entered by the user in Main. I tested Main and it is fine also sort is a tad wanky but I think perhaps it could be because I am not using the pointers correctly in function avg.

Does it look to be correct in logic and syntax?

Thanks for any help.

#include<iostream>
using namespace std;


double avg(double *scores, int numScores)
{
	double total = 0.0;
	double average;
	sortScores(scores, numScores);
		
	for(int count = 0; count < numScores; count++)
	{
		total += *scores;
		scores++;
	}
		average = total / numScores;

	return average;
}

Recommended Answers

All 24 Replies

Try removing the sortScores call, because I don't see anything wrong in the avg function. The average should be the same whether the list is sorted or not, so that will tell you if the error is in sort or in avg (and I would bet it is in sort).

Note: your loop is a bit different to what people normally use to iterate through an array:

#include<iostream>
using namespace std;

double avg(double *scores, int numScores)
{
  double total = 0.0;
  double average;
  sortScores(scores, numScores);

  //this is the most usual way:		
  for(int count = 0; count < numScores; ++count)
    total += scores[count];

  //people sometimes do this too:
  for(double* scores_end = scores + numScores; scores != scores_end; ++scores)
    total += *scores;

  average = total / numScores;
  return average;
}

Both styles for the for-loop are equivalent to each other and equivalent to yours, but yours is a bit redundant (either you increment the pointer (or iterator) or you increment an index, not both).

Well, that should work. I think your comment about sortScores() being hinky (the correct technical term) is probably correct. However, it would be better just to treat scores as an array. Ie,

double avg(double *scores, int numScores)
{
	double total = 0.0;
	double average;
	sortScores(scores, numScores);
 
	for(int count = 0; count < numScores; count++)
	{
		total += scores[count];
	}
        average = total / numScores;
 
	return average;
}

The avg( ) function is working fine for me.

You should perhaps do an output of the values after the sort, to be sure that you've not corrupted the data there. Just what do you mean by "a tad wanky"? If it's not correct, it's not correct.

I see mike_2000_17 and I are saying just about the same thing, in different words... :-)

Thanks everyone. It's a bit late here to try right now but I did actually take out the call to sort and it still points to an address. I'll post the sort code in the am...and again thanks!

Ah.. and one more note ~ by wanky I mean it sorts ascending sometimes and sorts descending sometimes...weird right? I seems it puts the last score entered by the user in the first position. Ok..so here is sort:

void sortScores(double *scores, int numScores)
{
	int startScan, minIndex;
	double minElem;
	
	

	for(startScan = 0; startScan < (numScores -1); startScan++)
	{
		minIndex = startScan;
		minElem = scores[startScan];

		for(int index = startScan; index < numScores; index ++)
		{
			if(scores[numScores] < minElem)
			{
				minElem = scores[index];
				minIndex = index;
			}
		}
		scores[minIndex] = scores[startScan];
		scores[startScan] = minElem;
	}
	
}

Your problem is in line 15. Can you see it?

commented: Thank you for helping and not solving! +1

Your problem is in line 15. Can you see it?

Scores is less than minElem and they are one and the same since minElem = scores[startScan] right?

Sort always takes the last number entered by the user and makes it the first number.

Here is main and sort;

#include<iostream>
#include<iomanip>
using namespace std;

void sortScores(double *, int);
double avg(double *, int);

int main()
{
	double *scores;

	int numScores, count;

	cout << "How many scores do you wish to enter?  ";
	cin >> numScores;

	scores = new double[numScores];

	if(numScores <= -1)
	{	
		return NULL;
	}

	cout << "Enter test scores below.\n";

	for(count = 0; count < numScores; count++)
	{
		cout << "Test # " << (count +1) << ": ";
		cin >> scores[count];
		
	}

	cout << fixed << showpoint << setprecision(2);

	sortScores(scores, numScores);

	for(count = 0; count < numScores; count++)
	cout << *(scores + count)<< endl;


	
	


	system("pause");


	return 0;
}

void sortScores(double *scores, int numScores)
{
	int startScan, minIndex;
	double minElem;
 
 
 
	for(startScan = 0; startScan < (numScores -1); startScan++)
	{
		minIndex = startScan;
		minElem = scores[startScan];
 
		for(int index = startScan; index < numScores; index ++)
		{
			if(index < minElem)
			{
				minElem = scores[index];
				minIndex = index;
			}
		}
		scores[minIndex] = scores[startScan];
		scores[startScan] = minElem;
	}
 
}

Oh, now you broke it even more.
Your line 66 above

if(index < minElem)

is comparing apples and oranges - is there any purpose to compare the index (location in the array) to a value in the array?

From your previous example of sort(), the problem was what you used as the index into the array to compare to your smallest found value:

if(scores[numScores] < minElem)

Replace [numScores] with [index] and it should sort correctly.

Myself, I'd just quicksort it with qsort() and be done with it. Unless you are supposed to implement a sorting routine for a class exercise, it almost never pays to implement something for which there is really good standard functions for.

@rubberman: Even better, using the C++ function for sorting: std::sort from #include <algorithm>.

Thank you all so much for helping and not solving, especially vmanes! Everything is working now including removing the lowest user entered number from the average total. So appreciate the help! I would post the solution however I really do try to learn not acquire the answer and I would like to afford that opportunity to others as well! You guys rock ~ Thanks Again!!

@rubberman: Even better, using the C++ function for sorting: std::sort from #include <algorithm>.

I thought about mentioning that, std::sort as well, but the advantage with qsort() is that it also works for C programs, and for a simple array that was the case here, a lot (I think) easier to use, and understand. Anyway, it is a good point for pure C++ programs. Myself, starting with early C++ compilers (pre std-c++ days without templates and such) in the late 80's and early 90's, is that I tend to do more mixed-mode programming, using what makes the most sense to me, illustrating my intention in the code, and is the simplest and easiest to understand (sticking to the KISS principal). I love classes and generics (template classes), but there are times when C-style functional programming is just cleaner. Nothing but my (not so) humble opinion, and others may well disagree with me there, but I can still respect that.

@rubberman, I certainly meant no disrespect and I have no problem with mixed C/C++ programming (especially when done by professionals). I just like to point out to those who are learning (the OP) the pure C++ techniques and functions because I think that in the long run it's better to get used to STL style programming as early as possible, but that's just my (not so) humble opinion. On a final note, I hope, for your sake, that you are not suffering from the C-hacker syndrome, you know that the first step to recovery is admission (a few red flags from your post: "C-style functional programming is just cleaner", "a lot (I think) easier to use", "using what makes the most sense to me", etc. these are not rational nor objective arguments, no offense, but they do sound an awful lot like they come from a C-hacker).

@rubberman, I certainly meant no disrespect and I have no problem with mixed C/C++ programming (especially when done by professionals). I just like to point out to those who are learning (the OP) the pure C++ techniques and functions because I think that in the long run it's better to get used to STL style programming as early as possible, but that's just my (not so) humble opinion. On a final note, I hope, for your sake, that you are not suffering from the C-hacker syndrome, you know that the first step to recovery is admission (a few red flags from your post: "C-style functional programming is just cleaner", "a lot (I think) easier to use", "using what makes the most sense to me", etc. these are not rational nor objective arguments, no offense, but they do sound an awful lot like they come from a C-hacker).

No offence taken Mike. Most of my colleagues would tell you that I am as hard-core a C++ engineer as there is. But as an engineer (I am a director of an IEEE network) I prefer to use the best/most appropriate tool for the job at hand. In any case, everyone uses those tools they are most comfortable with, and a lot of this stuff, algorithmic templates and such, are pretty recent in development. Not to say that they aren't good tools, or that one should discourage their use (I sure won't do that) if one isn't that familiar with them. I have been programming primarily in C++ since 1991, and have been awarded patents on work that I have done in C++ for adaptive system software. So, I certainly don't consider myself, in your terms, a "C-Hacker". I only use C now for kernel module and driver development, because one really has no choice for that.

Anyway, I have enjoyed reading your comments, and clarity of observation on many of these posts. I'm sure that over time we will learn a lot from each other.

-Rubberman

I didn't understand the most of that link, but kudos on a huge rant.

I didn't understand the most of that link, but kudos on a huge rant.

That wasn't a rant! That was a "discussion"! :-) :lol:

A discussion requires more than 1 person.:)
But some of my best have been with myself.

>>that link, but kudos on a huge rant.
The article I linked to was not a rant, it was an epic, logical rebuttal of Linus Torvalds' crazy rant against C++ (which is also worth reading just for how ridiculous it sounds). But that does not in any way diminish Torvalds' awesome programming talent, but just shows that he is human (with all that it entails).

Ok but one thing bugged me. He mentioned on a number of occasions that Torvald only cited one example for his argument, and yet only cited one example himself for his.

>>He mentioned on a number of occasions that Torvald only cited one example for his argument
He mentions that problem in Torvalds' rant one time in the whole rebuttal.

>>only cited one example himself for his.
Actually, if you just do a word-find for "example", you will easily spot 4-5 examples given of where C "promotes" bad-style or at least does not hold up against C++'s features.
Anyways, the claims are very different. Claiming that something is true requires evidence (more than one example). Refuting the claim by saying that there is no evidence to support it, does not require evidence (and giving counter-examples is just a bonus). The main theme of the rebuttal article is that all the strong claims that Torvalds made in his epic rant are not supported by evidence (or, at least, far from enough evidence). I mean, this is just a basic exercise of critical thinking (dissecting an argument for what it is worth). The author of the rebuttal might go over the boundary of an objective treatise here and there, but the main conclusion is what is important: "Claiming that C somehow encourages people to make better, cleaner and more efficient code is simply false." (note: _claiming_ C is better is false, claiming the opposite (that C++ is better) would require a lot of evidence and the author is very careful not to make that claim). I linked it mainly because I found his description of the "C-hacker syndrome" very accurate (as I used to suffer from that, years ago).

The article I linked to was not a rant,

that Torvalds made in his epic rant

Is this a contradiction or sarcasm?

Epic: Surpassing the usual or ordinary, particularly in scope or size.
Rant: To utter or express with violence or extravagance.
I think those terms apply very well to Torvalds' forum post (it's made epic because of the "celebrity" status of Linus Torvalds).

Rebuttal: The act of refuting by offering a contrary contention or argument.
I think this term applies very well to the article I linked to.

Where's the contradiction?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.