473,668 Members | 2,360 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3174
"vato" <aa****@adelphi a.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::istringstr eam 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::accumulat e (v.begin (), v.end (),
static_cast <score_type> (0)))
/ v.size ();
}

int main ()
{
try
{
{
using std::ios_base;
ios_base::iosta te exceptions =
ios_base::failb it | ios_base::badbi t;
std::cout.excep tions (exceptions); // often a good idea
std::cin.except ions (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::ostringstr eam sstream;
sstream << "Enter the score for game " << i + 1 << ".\n";
scores.push_bac k (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::setprecisi on (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.p ol.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
1919
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 interested, we can supply you a link to our unfinished program to see if you could finish it or take a look at it and our current forum program to see what we are looking for and possibly give us a quote. We have an unfinished forum program....
4
2132
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 aims to be easier to use and more powerful. I've developped yet a first alpha that contains several components: File Source
15
1561
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 rules for a concept that I hope lets me avoid further encoding trouble, but I would feel more confident if some of the experts here would have a look at the thoughts I made so far and told me if I'm still going wrong somewhere (BTW, the program is...
22
2274
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
1644
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 translate these instructions into something that can be processed automatically, so I have used the Command pattern to set up a set of commands that correspond to the various instructions in the document.
51
7928
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 pretty good: http://expertrating.com/c++test.asp
3
3001
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 download and compile on the various environments that will give me an idea on the relative performance of each STL? For example - a test program that will run through, say, appending one character to a string 1000000 times, copying vectors,...
6
5411
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
2300
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 getting is
6
2323
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 (e.g. Resharper) obviously do this internally, but I can't seem to find any that expose this simple functionality. Command line would be fine -- in fact I'd prefer that but its not necessary.
0
8459
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8378
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8791
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8577
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8653
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4376
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2786
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.