Click here to Skip to main content
15,891,607 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Double average Pin
Tim Dappen17-Sep-10 12:56
Tim Dappen17-Sep-10 12:56 
GeneralRe: Double average Pin
Richard MacCutchan17-Sep-10 22:07
mveRichard MacCutchan17-Sep-10 22:07 
QuestionRe: Double average Pin
David Crow17-Sep-10 15:58
David Crow17-Sep-10 15:58 
AnswerRe: Double average Pin
Maximilien17-Sep-10 16:59
Maximilien17-Sep-10 16:59 
GeneralRe: Double average Pin
David Crow18-Sep-10 9:33
David Crow18-Sep-10 9:33 
AnswerRe: Double average Pin
Tim Craig18-Sep-10 7:08
Tim Craig18-Sep-10 7:08 
AnswerRe: Double average Pin
Stephen Hewitt17-Sep-10 19:19
Stephen Hewitt17-Sep-10 19:19 
AnswerRe: Double average Pin
Aescleal18-Sep-10 8:26
Aescleal18-Sep-10 8:26 
One thing to remember when doing programming assignments: Accademic computer scientists love recursion. You should try and introduce it to everything you do. So the best way to implement the function you're trying to write is:

double sum_array( int size, double values[] )
{
	if( size > 0 ) return values[0] + sum_array( size - 1, values + 1 );
	return 0;
}

double average( int size, double values[] )
{
	return sum_array( size, values ) / size;
}


Another cool thing is that the solution doesn't have any side effects so it's pure within the mathematical idea of a function and the CS ideas of functional programming.

You can test it with something like:

int main()
{
	double values[ 10 ] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 };
	std::cout << average( 10, values ) << std::endl;
}


The I/O should be easy to sort out.

Cheers,

Ash
GeneralRe: Double average Pin
Stephen Hewitt18-Sep-10 20:57
Stephen Hewitt18-Sep-10 20:57 
GeneralRe: Double average [modified] Pin
Stefan_Lang28-Sep-10 2:28
Stefan_Lang28-Sep-10 2:28 
QuestionUsing Windows DataGridViewTool Pin
Danzy8317-Sep-10 4:42
Danzy8317-Sep-10 4:42 
AnswerRe: Using Windows DataGridViewTool [modified] Pin
Cool_Dev17-Sep-10 5:15
Cool_Dev17-Sep-10 5:15 
GeneralRe: Using Windows DataGridViewTool Pin
Danzy8317-Sep-10 5:34
Danzy8317-Sep-10 5:34 
GeneralRe: Using Windows DataGridViewTool Pin
Cool_Dev17-Sep-10 5:38
Cool_Dev17-Sep-10 5:38 
AnswerRe: Using Windows DataGridViewTool Pin
Rolf Kristensen20-Sep-10 10:10
Rolf Kristensen20-Sep-10 10:10 
QuestionNew into parallel programming, looking for advice Pin
Stefan_Lang17-Sep-10 4:20
Stefan_Lang17-Sep-10 4:20 
AnswerRe: New into parallel programming, looking for advice [modified] Pin
Paul Michalik17-Sep-10 22:35
Paul Michalik17-Sep-10 22:35 
GeneralRe: New into parallel programming, looking for advice Pin
Stefan_Lang19-Sep-10 22:37
Stefan_Lang19-Sep-10 22:37 
GeneralRe: New into parallel programming, looking for advice Pin
Paul Michalik21-Sep-10 5:15
Paul Michalik21-Sep-10 5:15 
Questiongood programmer Pin
Stephan A.17-Sep-10 4:11
Stephan A.17-Sep-10 4:11 
QuestionRe: good programmer PinPopular
David Crow17-Sep-10 4:19
David Crow17-Sep-10 4:19 
AnswerRe: good programmer Pin
Stephan A.17-Sep-10 4:24
Stephan A.17-Sep-10 4:24 
AnswerRe: good programmer Pin
CPallini17-Sep-10 5:48
mveCPallini17-Sep-10 5:48 
GeneralRe: good programmer Pin
Stephan A.17-Sep-10 23:27
Stephan A.17-Sep-10 23:27 
JokeRe: good programmer Pin
Richard MacCutchan17-Sep-10 7:37
mveRichard MacCutchan17-Sep-10 7:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.