Accelerated C++ Exercise 4-0

Viewed 220

I am working through Accelerated C++ by Andrew Koenig and Barbara Moo (2000) and am stuck on code from Chapter 4. I think this code reads an external data file containing exam, final and homework grades for multiple students and returns a course grade for each student.

I have downloaded solutions for the exercises from Andrew Koenig from GitHub:

https://github.com/bitsai/book-exercises/tree/master/Accelerated%20C%2B%2B

I can run the first example in Chapter 4 (main1.cc) which returns a course grade for a single student when the midterm, final and homework grades are entered from the keyboard. Here is an example:

data: harriet 85 95 88 87 93 45 76 99
student name = harriet
midterm grade = 85
final grade = 95
median of six homework grades (88, 87, 93, 45, 76, 99) = 87.5
final grade = 90
0.2 * 85 + 0.4 * 95 + 0.4 * 87.5 = 90

But I cannot run the second example (main2.cc) in which data are read for multiple students from an external file. No error messages appear when I create or run the main2.exe file. The cursor in the Windows 10 command window simply moves to the next line when I try to run main2.exe and nothing is displayed, not even the directory. Here are the lines returned when I make the executables:

c:\Users\mark_\myCppprograms\mychapter04>make
g++     main1.cc   -o main1
g++     main2.cc   -o main2
g++    -c -o main3.o main3.cc
g++    -c -o grade.o grade.cc
g++    -c -o median.o median.cc
g++    -c -o Student_info.o Student_info.cc
g++   main3.o grade.o median.o Student_info.o   -o main3

The code in main2.cc is long. The third example in Chapter 4 (main3.cc) breaks this code into multiple C++ and header files but I cannot get the main3.exe file to return anything either.

Here is the code for main2.cc. I changed #include "../minmax.h" to #include "minmax.h" and put a file "minmax.h" in the same folder as main2.cc. The file "minmax.h" did not come from the aforementioned GitHub site and I paste its contents below.

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>

#ifdef _MSC_VER
//#include "../minmax.h"
#include "minmax.h"
#else
using std::max;
#endif

using std::cin;
using std::cout;
using std::domain_error;
using std::endl;
using std::istream;
using std::ostream;
using std::setprecision;
using std::setw;
using std::sort;
using std::streamsize;
using std::string;
using std::vector;

struct Student_info {
  string name;
  double midterm, final;
  vector<double> homework;
};  // note the semicolon--it's required

// compute the median of a `vector<double>'
// note that calling this function copies the entire argument `vector'
double median(vector<double> vec) {
#ifdef _MSC_VER
  typedef std::vector<double>::size_type vec_sz;
#else
  typedef vector<double>::size_type vec_sz;
#endif

  vec_sz size = vec.size();
  if (size == 0)
    throw domain_error("median of an empty vector");

  sort(vec.begin(), vec.end());

  vec_sz mid = size/2;

  return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid];
}

// compute a student's overall grade from midterm and final exam grades and homework grade
double grade(double midterm, double final, double homework) {
  return 0.2 * midterm + 0.4 * final + 0.4 * homework;
}

// compute a student's overall grade from midterm and final exam grades
// and vector of homework grades.
// this function does not copy its argument, because `median' does so for us.
double grade(double midterm, double final, const vector<double>& hw) {
  if (hw.size() == 0)
    throw domain_error("student has done no homework");

  return grade(midterm, final, median(hw));
}

double grade(const Student_info& s) {
  return grade(s.midterm, s.final, s.homework);
}

// read homework grades from an input stream into a `vector<double>'
istream& read_hw(istream& in, vector<double>& hw) {
  if (in) {
    // get rid of previous contents
    hw.clear();

    // read homework grades
    double x;
    while (in >> x)
      hw.push_back(x);

    // clear the stream so that input will work for the next student
    in.clear();
  }

  return in;
}

istream& read(istream& is, Student_info& s) {
  // read and store the student's name and midterm and final exam grades
  is >> s.name >> s.midterm >> s.final;

  read_hw(is, s.homework);  // read and store all the student's homework grades

  return is;
}

bool compare(const Student_info& x, const Student_info& y) {
  return x.name < y.name;
}

int main() {
  vector<Student_info> students;
  Student_info record;
  string::size_type maxlen = 0;

  // read and store all the records, and find the length of the longest name
  while (read(cin, record)) {
    maxlen = max(maxlen, record.name.size());
    students.push_back(record);
  }

  // alphabetize the records
  sort(students.begin(), students.end(), compare);
  
#ifdef _MSC_VER
  for (std::vector<Student_info>::size_type i = 0;
#else
       for (vector<Student_info>::size_type i = 0;
#endif
        i != students.size(); ++i) {

     // write the name, padded on the right to `maxlen' `+' `1' characters
     cout << students[i].name
          << string(maxlen + 1 - students[i].name.size(), ' ');

     // compute and write the grade
     try {
       double final_grade = grade(students[i]);
       streamsize prec = cout.precision();
       cout << setprecision(3) << final_grade
        << setprecision(prec);
     } catch (domain_error e) {
       cout << e.what();
     }

     cout << endl;
       }

     return 0;
       }

The code for main1.cc, main2.cc and main3.cc all compiles using a single makefile and make statement. Here are the contents of makefile. I do not believe main2.cc uses any of the header files mentioned in this makefile but I did not make any changes to the makefile. I have not pasted the contents of any of these header files here. If they are relevant to main2.cc I can provide them on request or they are all available at the GitHub site above.

CXX = g++
CC = g++

all:        main1 main2 main3

Student_info.o: Student_info.cc Student_info.h

grade.o:    grade.cc grade.h median.h Student_info.h

main3.o:    main3.cc grade.h median.h Student_info.h

median.o:   median.cc median.h

main3:      main3.o grade.o median.o Student_info.o

test:       all
    ./main1 <../data/single_grade
    ./main2 <../data/single_grade
    ./main2 <../data/grades
    ./main3 <../data/grades

clobber:
    rm -f *.o *.exe core main1 main2 main3

I have created a subfolder called data and put two data files in it: grades and single_grade. Here are the contents of grades:

Moo 100 100 100 100 100 100 100 100
Moore 75 85 77 59 0 85 75 89
Norman 57 78 73 66 78 70 88 89
Olson 89 86 70 90 55 73 80 84
Peerson 47 70 82 73 50 87 73 71

Russel 72 87 88 54 55 82 69 87
Thomas 90 96 99 99 100 81 97 97
Vaughn 81 97 99 67 40 90 70 96
Westerly 43 98 96 79 100 82 97 96


Baker 67 72 73 40 0 78 55 70
Davis 77 70 82 65 70 77 83 81
Edwards 77 72 73 80 90 93 75 90
Franklin 47 70 82 73 50 87 73 71

Jones 77 82 83 50 10 88 65 80
Harris 97 90 92 95 100 87 93 91
Smith 87 92 93 60 0 98 75 90
Carpenter 47 90 92 73 100 87 93 91

Fail1 45 55 65 80 90 70 65 60
Fail2 55 55 65 50 55 60 65 60

Here are the contents of single_grade:

harriet 85 95 88 87 93 45 76 99

The only place I find these two data files mentioned in any of the above code is in the makefile which confuses me, but I guess the makefile associates main2.cc and the two data files.

Here are the contents of "minmax.h":

/**
 * This file has no copyright assigned and is placed in the Public Domain.
 * This file is part of the mingw-w64 runtime package.
 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
 */
#ifndef _INC_MINMAX
#define _INC_MINMAX

#ifndef __cplusplus
#ifndef NOMINMAX
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif

#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
#endif
#endif

#endif

Here is what is returned by c++ -v on my Windows 10 laptop:

c:\Users\mark_\myCppprograms>c++ -v
Using built-in specs.
COLLECT_GCC=c++
COLLECT_LTO_WRAPPER=c:/rtools/MINGW_64/bin/../libexec/gcc/x86_64-w64-mingw32/4.9.3/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-4.9.3/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=/mingw64 --with-sysroot=/home/Jeroen/mingw-gcc-4.9.3/x86_64-493-posix-seh-rt_v3-s/mingw64 --with-gxx-include-dir=/mingw64/x86_64-w64-mingw32/include/c++ --enable-static --disable-shared --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --disable-isl-version-check --disable-cloog-version-check --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --with-system-zlib --with-gmp=/home/Jeroen/mingw-gcc-4.9.3/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/home/Jeroen/mingw-gcc-4.9.3/prerequisites/x86_64-w64-mingw32-static --with-mpc=/home/Jeroen/mingw-gcc-4.9.3/prerequisites/x86_64-w64-mingw32-static --with-isl=/home/Jeroen/mingw-gcc-4.9.3/prerequisites/x86_64-w64-mingw32-static --with-cloog=/home/Jeroen/mingw-gcc-4.9.3/prerequisites/x86_64-w64-mingw32-static --enable-cloog-backend=isl --with-pkgversion='x86_64-posix-seh, Built by MinGW-W64 project' --with-bugurl=http://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -I/home/Jeroen/mingw-gcc-4.9.3/x86_64-493-posix-seh-rt_v3-s/mingw64/opt/include -I/home/Jeroen/mingw-gcc-4.9.3/prerequisites/x86_64-zlib-static/include -I/home/Jeroen/mingw-gcc-4.9.3/prerequisites/x86_64-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -I/home/Jeroen/mingw-gcc-4.9.3/x86_64-493-posix-seh-rt_v3-s/mingw64/opt/include -I/home/Jeroen/mingw-gcc-4.9.3/prerequisites/x86_64-zlib-static/include -I/home/Jeroen/mingw-gcc-4.9.3/prerequisites/x86_64-w64-mingw32-static/include' CPPFLAGS= LDFLAGS='-pipe -L/home/Jeroen/mingw-gcc-4.9.3/x86_64-493-posix-seh-rt_v3-s/mingw64/opt/lib -L/home/Jeroen/mingw-gcc-4.9.3/prerequisites/x86_64-zlib-static/lib -L/home/Jeroen/mingw-gcc-4.9.3/prerequisites/x86_64-w64-mingw32-static/lib '
Thread model: posix
gcc version 4.9.3 (x86_64-posix-seh, Built by MinGW-W64 project)

c:\Users\mark_\myCppprograms>

I am aware that other people have uploaded solutions to Accelerated C++ exercises. I have looked at several. But I have not been able to get any of them to run with main2.cc or main3.cc and return the expected results.

1 Answers

I'm not sure how you actually run it, but in my case it works perfectly fine. If you take a closer look at main in main2.cc, you'll notice that there's a blocking read call, which causes the program to wait for user input. Take a look at test target from makefile:

test:           all
        ./main1 <../data/single_grade
        ./main2 <../data/single_grade
        ./main2 <../data/grades
        ./main3 <../data/grades

< means that we'll be redirecting what's in single_grade to the process being run. It's implicitly assumed that in folder above to the executable binary will be a data directory with single_grade data file. So, when you run $ make test, you'll notice that everything works as expected (assuming the directory structure is unchanged). If you simply run $ ./main2, nothing is gonna happen as the program awaits for input. Or, assuming your data file is next to main2 binary, you could run $ main2 < single_grade. Or, alternatively, you could omit the files at all:

$ ./main2
$ Moo 100 100 100 100 100 100 100 100
$ Moore 75 85 77 59 0 85 75 89
$ EOF (in bash shell it's CTRL + D, on windows cmd: CTRL + Z)

Which yields:

Moo   100
Moore 79.4
Related