Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 8th, 2007, 3:23 AM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
I'm getting the wrong answer

This program I've got uses an array and calls a function to find the average of the integers in the array. The integers in the array are 9, 4, 6, 8, 10 and when you add them up you get 37. When you divide by 5 you get 7.4.

I do not want to lose the fractional part of the answer so that's why I'm using the static_cast< >operator. I also always want the answer to be output with a fixed decimal point and two digits of precision to the right of the decimal point.

But my output says "The average is 7.00" instead of "7.40" like it should.

I'm not sure where I'm going wrong. Here is the code:

int findAverage(int j[]);

int main()
{
	int numbers[] = {9, 4, 6, 8, 10};
	double average = 0.0;
	
	cout << "The values in the array are: "; 

	for (int i = 0; i < 5; i++)
		cout << numbers[i] << " ";

	cout << endl;

	average = findAverage(numbers);

	cout << "The average of the values is: " << setprecision(2)
		 << setiosflags(ios::fixed | ios::showpoint)
		 << average;

    cout << endl;
	return 0;
}

int findAverage(int j[])
{
    int total = 0;

	for (int i = 0; i < 5; i++)
		total += j[i];

	return static_cast<double>(total) / 5;

}

Last edited by 357mag; Jun 8th, 2007 at 3:25 AM. Reason: Mistake
357mag is offline   Reply With Quote
Old Jun 8th, 2007, 4:08 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5 grumpy will become famous soon enough
A function that returns int can never return a value that is not an integer. Try making findAverage() return double, and return from it using;
return total/5.0;
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Code compiled without error, but gets the wrong answer; Calculating Volume Fall Back Son C 15 Oct 21st, 2006 7:51 PM
Wrong answer Edgar Assembly 6 Jul 12th, 2006 8:17 PM
[Python] Simple Comment Stripper UnKnown X Show Off Your Open Source Projects 0 Feb 10th, 2006 8:57 AM
First Python Programme: Fibonacci Finder UnKnown X Python 2 Dec 15th, 2005 7:19 PM
What is wrong with this code? c0ldshadow Visual Basic .NET 5 Dec 5th, 2005 6:37 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:52 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC