473,386 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Looking for an example of C++ program

I was hoping to get an example of a C++ program I am working on with my son

need to calculate the average of a series of game scores where we can drop
the lowest score. ie five games get the best 4 out of 5.

He is in c++ and thinks I know about this stuff. I study the book to try to
keep ahead of him, but he is a sponge I am wax.

I would need something with 3 functions if possible getscore find lowest and
average.

if you could help an old guy out so I can put him in his place I would
appreciate it.
thanks in advance

V/R

vato
Jul 22 '05 #1
3 3161
"vato" <aa****@adelphia.net> wrote
I was hoping to get an example of a C++ program
I am working on with my son

need to calculate the average of a series of game
scores where we can drop the lowest score. ie five
games get the best 4 out of 5.

He is in c++ and thinks I know about this stuff. I
study the book to try to keep ahead of him, but he
is a sponge I am wax.

I would need something with 3 functions if possible
getscore find lowest and average.

if you could help an old guy out so I can put him in
his place I would appreciate it.


Here are two out of hundreds of possible options:

Option 1

Step 1: put the scores in an std::vector
Step 2: sort the vector in ascending order (just because it's easier)
Step 3: pull out the N last values from your vector

Option 2

Step 1: put the scores in an std::multiset (keeps them sorted)
Step 2: pull out the N last values from your multiset

Details on how to use std::vector, std::multimap, and std::sort can be found
in practically any good C++ book.

Claudio Puviani
Jul 22 '05 #2
#include <vector>
#include <string>
#include <ostream>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <new>

struct stream_error { };
struct logic_error { };
typedef double score_type;
typedef double average_type;

// Returns a score entered by the user.
score_type getscore (const std::string & prompt)
{
score_type result;
std::string temp;
std::cout << prompt;
while (std::getline (std::cin, temp))
{
std::istringstream sstream (temp);
if (sstream >> result)
{
if (sstream >> temp && ! temp.empty ())
{
std::cout << "Please just enter one score per line.\n";
continue;
}
return result;
}
std::cout << "That's not a valid score. Try again.\n";
}
// Program flow will never reach this point.
throw logic_error ();
}

// Returns an iterator to a minimal element of a sequence.
template <typename iterator>
iterator find_lowest (iterator iter, iterator end)
{
if (iter == end) throw logic_error ();
iterator result (iter);
for (++ iter; iter != end; ++ iter)
if (* iter < * result)
result = iter;
return result;
}

// Returns the mean of all the elements of the score vector.
average_type average (std::vector <score_type> & v)
{
if (v.empty ()) throw logic_error ();
return
static_cast <average_type>
(std::accumulate (v.begin (), v.end (),
static_cast <score_type> (0)))
/ v.size ();
}

int main ()
{
try
{
{
using std::ios_base;
ios_base::iostate exceptions =
ios_base::failbit | ios_base::badbit;
std::cout.exceptions (exceptions); // often a good idea
std::cin.exceptions (exceptions); // often a bad idea
}

std::vector <score_type> scores;

// Ask the user for 5 score_type objects.
for (int i = 0; i != 5; ++ i)
{
std::ostringstream sstream;
sstream << "Enter the score for game " << i + 1 << ".\n";
scores.push_back (getscore (sstream.str ()));
}

// Find and erase a minimal score.
scores.erase (find_lowest (scores.begin (), scores.end ()));

// Compute and display the average of the remaining scores.
std::cout << "The average score was "
<< std::fixed << std::setprecision (4)
<< average (scores) << ".\n";

return 0;
}
catch (logic_error &)
{
std::cerr << "This should never happen.\n";
return 1;
}
catch (std::bad_alloc & e)
{
std::cerr << "There was a fatal memory allocation error.\n";
return 1;
}
catch (std::ios_base::failure & e)
{
std::cerr << "There was a fatal input or output error.\n";
return 1;
}
catch (...)
{
std::cerr << "There was a fatal error.\n";
return 1;
}
}

--
Regards,
Buster.
Jul 22 '05 #3

"Buster" <no***@nowhere.com> wrote in message
news:c5**********@newsg3.svr.pol.co.uk...
[snipped program]
Regards,
Buster.


Nice of you to do all the work for him. Ever think this might have been his
homework?

Howard
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: blockhead | last post by:
We are looking for someone to either complete a php forum program or create one for us. There isn't really anything that is available that suits our needs and we have specific wants. If you are...
4
by: Alexander DEJANOVSKI | last post by:
Hi all, I'm starting a new project to develop an Open-Source EAI server in Python and I'm looking for motivated developpers and testers. It is inspired by Open Adaptor (www.openadaptor.org), but...
15
by: klappnase | last post by:
Hello all, I am trying to internationalize my Tkinter program using gettext and encountered various problems, so it looks like it's not a trivial task. After some "research" I made up a few...
22
by: Martin MOKREJ© | last post by:
Hi, I'm looking for some easy way to do something like include in c or PHP. Imagine I would like to have: cat somefile.py a = 222 b = 111 c = 9
12
by: Charles Law | last post by:
Hi guys A bit of curve ball here ... I have a document (Word) that contains a series of instructions in sections and subsections (and sub-subsections). There are 350 pages of them. I need to...
51
by: Matt | last post by:
Hello, I'm a hiring C++ developer employer looking for existing, online C++ aptitude tests. I have not yet extensively researched this yet, but as an example, I thought this test looked...
3
by: Steve | last post by:
Hi, I'm currently looking a porting our Windows Codewarrior based C++ projects to MinGW and/or Visual Studio and I'm just wondering if there is a 'standard' suite of benchmark programs I can...
6
by: pasmod | last post by:
I hope you could help me to find Microsoft Visual c++ 1.5. I've seen on post archivies and it seems almost impossible to find. bye, pas
8
by: Eric_Dexter | last post by:
I was looking for a simple way to load a simple python program from another python program. I tried os.system(cabel) The file name is cabel.py a csound instrument editor.. The error I am...
6
by: beantaxi | last post by:
Hello all, I'm looking for a very simple code analysis tool. I have a large codebase to analyze, and all I really need to do is to find all uses of all methods in a few interfaces. Many tools...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.