Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Adding numbers using I/O streams


  • Please log in to reply
2 replies to this topic

#1 EricMon

EricMon

  • Members
  • 7 posts
  • OFFLINE
  •  
  • Local time:08:50 AM

Posted 03 November 2013 - 10:31 PM

Hello everyone, I have a question about something I am working on. 

I have a file that contains a list of GPA's called gpas.txt. It looks like this:

f   3.40 
f   4.00  
m   3.56  
m   3.80  
f   2.30  
f   3.95  
m   3.90  
m   4.00  
m   2.00  
f   4.00  
f   2.80  
m   3.70  
m   2.98  
f   3.89  
m   4.00  
f   3.90  
m   1.90  
m   2.90  
f   1.50  
f   2.67 
m   3.80  
m   2.35  
f   2.90  
f   3.70  
f   4.00 
m   3.78 
m   4.00 
f   3.98
 
I have to add the total of male GPAs and female GPAs then have the result output into a file called output.txt.
 
I have to use a function initialize that initializes variables sumMaleGPA and sumFmaleGPA to add. 
 
I also have to find the average and count the total amount of female and male GPAs, but I feel like I could figure that out on my own if I knew how to add multiple numbers at once. 
 
How can I add multiple numbers at once? I tried making each number an individual value like this:
 
double initialize(double f1, double f2, double m1, double m2, double f3, double f4, double m3, double m4, double m5, double f5, double f6, 
double m6, double m7, double f7, double m8, double f8, double m9, double m10, double f9, double f10, double m11, double m12, 
double f11, double f12, double f13, double m13, double m14, double f14);
 
Then adding them, But this is inefficient and the output gave me a weird negative number. 
 
Please help out if you can. Thank you very much. 
 


BC AdBot (Login to Remove)

 


#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:06:50 AM

Posted 04 November 2013 - 01:13 AM

How can I add multiple numbers at once? I tried making each number an individual value like this:

Store the partially calculated value. E.g.

double maleGpaSum = 0.0;
double femaleGpaSum = 0.0;
unsigned int maleGpas = 0;
unsigned int femaleGpas = 0;
while (parse_a_line()) { // You write the parsing code :)
    if (male) {
        maleGpaSum += value;
        ++maleGpas;
    } else {
        femaleGpaSum += value;
        ++femaleGpas;
    }
}

// Calculate the average
Don't try to hard code the number of lines you want to parse -- that is bug prone and less easy to read than writing a loop.
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#3 Romeo29

Romeo29

    Learning To Bleep


  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:08:50 AM

Posted 05 November 2013 - 12:10 PM

I have a feeling that the stress in this homework is on reading and writing data to IO Streams and not on number addition. If you are finding it difficult, then you have skipped basics of programming and the whole chapter on loops.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users