LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-14-2008, 10:20 AM   #1
rec3g
LQ Newbie
 
Registered: Mar 2008
Posts: 12

Rep: Reputation: 0
help with arrays


ok so I have this program that calculates the total of all the numbers in an array and then prints out all the numbers in that array. My only problem is that I'm supposed to calculate the number's percentage of the sum. Here's what I have so far...

Code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{

	int examScores[10];
	float percentage;
	ifstream myin;
	int sum=0;

	//open file
	myin.open("exam.dat");

	//read in scores
	for(int i=0; i <10; i++)
		myin >> examScores[i];

	//calculate sum	
	for(int count=0; count<10; count++)
		sum += examScores[count];
	cout << "Sum Equals: " << sum << endl;
	
	
	
	
	for(int count2=0; count2<10; count2++)
	{
		percentage = examScores[count2]/sum * 100;
		
		cout << examScores[count2] << '\t' << percentage <<
		endl;
	}
	
	myin.close();
}

Since the percentage part isn't working (it prints all zeros), I'm pretty sure my problem is with my percentage formula or format. originally I had the last output statement as follows...

cout << examScores[count2]<< '\t' <<examScores[count2]/sum * 100 <<endl;

but neither this or assigning the formula into the variable percentage is working. Any ideas??

oh and I also have to round the percentages to the nearest tenth.

Last edited by rec3g; 04-14-2008 at 10:21 AM.
 
Old 04-14-2008, 10:52 AM   #2
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
I would say the best idea is to do your own homework and step thru the program with a debugger.
 
Old 04-14-2008, 11:08 AM   #3
Randux
Senior Member
 
Registered: Feb 2006
Location: Siberia
Distribution: Slackware & Slamd64. What else is there?
Posts: 1,705

Rep: Reputation: 55
Quote:
Originally Posted by SciYro View Post
I would say the best idea is to do your own homework and step thru the program with a debugger.
How selfish! You forgot to tell him to post his finished project so others with the same assignment can use it
 
Old 04-14-2008, 11:27 AM   #4
rec3g
LQ Newbie
 
Registered: Mar 2008
Posts: 12

Original Poster
Rep: Reputation: 0
man you guys are mean. it's cool though I figured it out after sitting here for a few hours
 
Old 04-14-2008, 12:28 PM   #5
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
How does it go? Genius is 1% inspiration and 99% perspiration, I believe.
 
Old 04-14-2008, 12:54 PM   #6
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Anything to do with dividing by an integer that is bigger than any number in the array, without casting it to double or float? Especially before multiplying by 100? Just a thought..

Last edited by b0uncer; 04-14-2008 at 12:55 PM.
 
Old 04-15-2008, 07:04 AM   #7
Randux
Senior Member
 
Registered: Feb 2006
Location: Siberia
Distribution: Slackware & Slamd64. What else is there?
Posts: 1,705

Rep: Reputation: 55
Quote:
Originally Posted by rec3g View Post
man you guys are mean. it's cool though I figured it out after sitting here for a few hours
Ahhhhh, now doesn't that feel a lot better now that you did it yourself?

Congratulations! Keep up the good work.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Arrays of Structures to Arrays of Classes knobby67 Programming 1 01-01-2008 01:39 PM
Arrays in C rubadub Programming 1 07-30-2007 02:26 PM
Arrays baks Programming 2 03-19-2007 12:32 AM
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 05:40 AM
Arrays in C++ kalleanka Programming 5 02-07-2004 12:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration