Using Associative Arrays..

This is a discussion on Using Associative Arrays.. within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I need some help...the HW description is basically that I have to allow the following program to print ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    46

    Using Associative Arrays..

    Hey guys, I need some help...the HW description is basically that I have to allow the following program to print letter grades for each student following a certain criteria, and then it has to print print the number of A's, B's, (etc.)...

    ok so im having problems with the 2nd part of the description (print the # of A's, B's, etc)

    all it does is output the name of the student, letter grade, and numeric grade, and underneath that it says "Grade Distribution:" and program stops there...

    here is my code:

    main function

    Code:
    #include <algorithm>
    #include <iomanip>
    #include <iostream>
    #include <stdexcept>
    #include <string>
    #include <vector>
    #include "grade.h"
    #include "Student_info.h"
    #include <map>
    
    using std::cin;                     using std::setprecision;
    using std::cout;                    using std::sort;
    using std::domain_error;            using std::streamsize;
    using std::endl;                    using std::string;
    using std::max;                     using std::vector;
    using std::map;
    
    int main()
    {
        vector<Student_info> students;
        Student_info record;
        string::size_type maxlen = 0;       // the length of the longest
    					// name
        map<char, int> counters;         // container that stores letter
                                         //    grades and keeps track of how many
                                         //    times it shows up
                         
    
            
        // read and store all the students' data.
        // Invariant:   students  contains all the student records read so far
        //          maxlen  contains the length of the longest name in  students
        while (read(cin, record)) {
            // find length of longest name
            maxlen = max(maxlen, record.name.size());
            students.push_back(record);
        }
            
        // alphabetize the student records
        sort(students.begin(), students.end(), compare);
        
        // write the names and grades
        for (vector<Student_info>::size_type i = 0;
             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]);
    	    cout << grade(final_grade) << "\t";
    	    streamsize prec = cout.precision();
                cout << setprecision(3) << final_grade
                     << setprecision(prec);
    	    
    // read input and keep track of each letter and how many times we see it
    	    char c = grade(final_grade);
    	    while (cin >> c)
    		++counters[c];
            
    	} catch (domain_error e) {
                cout << e.what();
            }
            cout << endl;
        }
        cout << endl;
        cout << "Grade Distribution:" << endl;
        cout << endl;
       
        // print letters and how many times each one shows up
        for (map<char, int>::const_iterator it = counters.begin();
         it != counters.end(); ++it) {
       
        cout << it->first << ": " << it->second << endl;
        }
    
        return 0;
    }
    implementation file for functions:
    Code:
    mpute 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);
    }
    
    char grade(double num_grade)
    {  
        if (num_grade < 0 || num_grade > 100)
        throw domain_error("numerical grade outside legal range [0, 100]");
       
        if (num_grade >= 90 && num_grade <= 100)
        return 'A';
         
        if (num_grade >= 80)
        return 'B';
         
        if (num_grade >= 70)
        return 'C';
           
        if (num_grade >= 60)
        return 'D';
           
        if (num_grade >= 0 && num_grade < 60)
        return 'F';  
    }
    i think the problem lies within in the end of my main function...can you guys please review it and see if u can find some errors? thanks

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    anyone?

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    sorry I don't want to be bothersome, but this homework is due pretty soon, and I really am not sure what seems to be the problem...

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Heh, a forum isn't express help. You posted a lot of code, so it takes a while for someone to find enough time to look at it.

    As a style question, one overload of the grade function has a completely different meaning than the other three. That's very bad - sets of overloads should always have the same meaning.
    In fact, the first overload also has a different meaning from number two and three, and the second is pretty much redundant.

    This bit makes no sense:
    Code:
    	    char c = grade(final_grade);
    	    while (cin >> c)
    		++counters[c];
    Why do you do any input here?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    You're right..i deleted that portion and it still works fine...the final part, however, still doesn't seem to work..

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Just to be clear, the problem is that it doesn't print anything after "Grade Distribution"?

    Can you post the updated code?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    Quote Originally Posted by CornedBee View Post
    Just to be clear, the problem is that it doesn't print anything after "Grade Distribution"?

    Can you post the updated code?
    yeah it doesn't print anything after Grade Distribution..and here is the updated code:

    Code:
    #include <algorithm>
    #include <iomanip>
    #include <iostream>
    #include <stdexcept>
    #include <string>
    #include <vector>
    #include "grade.h"
    #include "Student_info.h"
    #include <map>
    
    using std::cin;                     using std::setprecision;
    using std::cout;                    using std::sort;
    using std::domain_error;            using std::streamsize;
    using std::endl;                    using std::string;
    using std::max;                     using std::vector;
    using std::map;
    
    int main()
    {
        vector<Student_info> students;
        Student_info record;
        string::size_type maxlen = 0;       // the length of the longest
    					// name
        map<char, int> counters;         // container that stores letter
                                         //    grades and keeps track of how many
                                         //    times it shows up
        
            
        // read and store all the students' data.
        // Invariant:   students  contains all the student records read so far
        //          maxlen  contains the length of the longest name in  students
        while (read(cin, record)) {
            // find length of longest name
            maxlen = max(maxlen, record.name.size());
            students.push_back(record);
        }
            
        // alphabetize the student records
        sort(students.begin(), students.end(), compare);
        
        // write the names and grades
        for (vector<Student_info>::size_type i = 0;
             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]);
    	    cout << grade(final_grade) << "\t";
    	    streamsize prec = cout.precision();
                cout << setprecision(3) << final_grade
                     << setprecision(prec);
    	    	    
    	} catch (domain_error e) {
                cout << e.what();
            }
            cout << endl;
        }
        
        cout << endl;
        cout << "Grade Distribution:" << endl;
        cout << endl;
        
        // print letters and how many times each one shows up
    
        for (map<char, int>::const_iterator it = counters.begin();
    	 it != counters.end(); ++it) {
       
    	cout << it->first << ": " << it->second << endl;
        }
      
        return 0;
    }

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You removed too much. It was only the while() line that didn't make sense. Now you're not filling the map.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    hey, thanks for your help, I figured it out. here is the final update:

    Code:
    #include <algorithm>
    #include <iomanip>
    #include <iostream>
    #include <stdexcept>
    #include <string>
    #include <vector>
    #include "grade.h"
    #include "Student_info.h"
    #include <map>
    
    using std::cin;                     using std::setprecision;
    using std::cout;                    using std::sort;
    using std::domain_error;            using std::streamsize;
    using std::endl;                    using std::string;
    using std::max;                     using std::vector;
    using std::map;
    
    int main()
    {
        vector<Student_info> students;
        Student_info record;
        string::size_type maxlen = 0;       // the length of the longest
    					// name
        map<char, int> counters;         // container that stores letter
                                         //    grades and keeps track of how many
                                         //    times it shows up
        char c;                         // letter grade
            
        // read and store all the students' data.
        // Invariant:   students  contains all the student records read so far
        //          maxlen  contains the length of the longest name in  students
        while (read(cin, record)) {
            // find length of longest name
            maxlen = max(maxlen, record.name.size());
            students.push_back(record);
        }
            
        // alphabetize the student records
        sort(students.begin(), students.end(), compare);
        
        // write the names and grades
        for (vector<Student_info>::size_type i = 0;
             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]);
    	    cout << grade(final_grade) << "\t";
    	    streamsize prec = cout.precision();
                cout << setprecision(3) << final_grade
                     << setprecision(prec);
    
    	    char c = grade(final_grade);
    	    ++counters[c];
    	    	    
    	} catch (domain_error e) {
                cout << e.what();
            }
            cout << endl;
        }
        
        cout << endl;
        cout << "Grade Distribution:" << endl;
        cout << endl;
        
        // print letters and how many times each one shows up
    
        for (map<char, int>::const_iterator it = counters.begin();
    	 it != counters.end(); ++it) {
       
    	cout << it->first << ": " << it->second << endl;
        }
      
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  4. How to use associative arrays in C
    By sreenu.daram in forum C Programming
    Replies: 2
    Last Post: 11-13-2005, 07:38 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21