Looking for an example of C++ program

V

vato

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
 
C

Claudio Puviani

vato said:
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
 
B

Buster

#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::eek:stringstream 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;
}
}
 
V

vato

Howard said:
[snipped program]
Regards,
Buster.

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

Howard

Howard

Thank you buster, for a visual, of what I was looking for it is a little
more than I had anticipated but it gives me something to refer too.

Howard I understand your concern, believe it is not my homework. I am
trying to help my son but this stuff is getting over my head now. I am just
a military man, trying to learn a thing or two while helping my son, I feel
it is two fold I get to help and at the same time I can learn a little
something. He is only starting out in this course and what was provided is
more advanced then what he would be able to produce. I do appreciate the
help buster and sorry for the concerns Howard.
 
D

David Harmon

On 13 Apr 2004 11:36:25 EDT in comp.lang.c++, "Howard"
[snipped program]
Regards,
Buster.

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

If he turns that in for homework his instructor will microsoft a brick.
(Infantile and inappropriate content just for Julie.)
Likewise for my version:

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <numeric>
using namespace std;
int main()
{
typedef double T;
vector<T> scores;
cout << "Enter scores, EOF when done: ";
copy(istream_iterator<T>(cin), istream_iterator<T>(),
back_inserter(scores));
cout << "\nAnswer: "
<< ((accumulate(scores.begin(), scores.end(), T(0))
- *(min_element(scores.begin(), scores.end())))
/ (scores.size() - 1))
<< '\n';
}
 
B

Buster

David said:
If he turns that in for homework his instructor will microsoft a brick.
(Infantile and inappropriate content just for Julie.)
Likewise for my version:

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <numeric>
using namespace std;
int main()
{
typedef double T;
vector<T> scores;
cout << "Enter scores, EOF when done: ";
copy(istream_iterator<T>(cin), istream_iterator<T>(),
back_inserter(scores));
cout << "\nAnswer: "
<< ((accumulate(scores.begin(), scores.end(), T(0))
- *(min_element(scores.begin(), scores.end())))
/ (scores.size() - 1))
<< '\n';
}

Love it. And to think I didn't submit my version that used
template template parameters.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top