Thread: C++ assignment two need help

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    C++ assignment two need help

    The following list of specific requirements are roughly in the order of complexity, with the later items being more challenging in general. It is anticipated that one has to have a solid work for items 1-6 below in order achive a pass grade for this assignment. Item 8 is what a student should be able to do well if he or she aims to achieve a credit or above, while item 9 is for those who aim at distinction or high distinct grade for this assignment.

    1. (1 mark) Function callings are indispensible in just about every C++ program that is supposed to be able to achieve something non-trivial. To refresh ourselves, explain the difference between passing by value and passing by reference in function calling. Use your own concrete C++ code to exemplify your explanations.
    2. (1 mark) Looping is equally indispensible in any decent C++ programs. Explain the similarity and differences between a while loop and a for loop in C++.
    3. (2 mark) The program should be menu-driven, and the menu should look like

    MAIN MENU
    0. Exit
    1. Add a student mark
    2. List all student marks
    3. Calculate the marks average
    4. Calculate the standard deviation
    5. Delete a student mark
    6. Find the number of students via a mark
    7. Display distinct marks and their occurrences

    Your choice ->

    You may utilise any parts of the trivial menu in Tutorial 7 (or the sample menu in Lecture 9), and in particular those clrscr() and pause() functions

    Code:
    void clrscr() 
          {
            system("cls");
          }
    
          void pause() 
          {
            system("echo.");
            system("echo.");
            system("pause");
          }
    This part is to implement a valid selection system for a menu, i.e. the skeleton of a menu system that doesn't do much other than allowing the selections one after another, and displaying the confirmation on which selection you have just made. You have to provide a separate driver program to demonstrate this, unless this functionality will be illustrated in your final program.
    4. (3 marks) This is for the implementation of the menu options 1. to 3.. For simplicity, we assume that the total number of marks to be analysed will not exceed 1000 within the program. When menu option 1. is selected, the program will let you add just one mark before returning you to the menu. When menu option 2. is selected, the program will list all the marks which have been added so far, some of which may be equal in value, then pause the screen for viewing before returning to the menu again. When menu option 3. is selected, the program will calculate and display the average of all the added marks, before returning again to the menu. You are welcome to design your program in whichever way you wish, as long as it is consistent with the general design principles. However, you are also welcome to implement the following prototyped functions or their variants for this purpose

    Code:
    bool addMark(double allMarks[], int &totalRec, double &mark);
          bool displayMarks(double allMarks[], int totalRec);
          bool findMean(double allMarks[], int totalRec, double &mean);
    where the array allMarks is assumed to contain totalRec number of valid marks, mark may be used to pass back the actual mark successfully added, and mean may be used to pass back the average of all the marks. The bool value returned by a function may be used to indicate whether the operation has been successful or not.
    5. (1 mark) For the program that has been developed up to this point, i.e. to the implementation of menu options 0. to 3., draw the Structure Diagram.
    6. (Make-Up Clause) If a student is unable to complete the following advanced features (7 marks in all), the student may choose to do 6 practice quizzes to at least gain a possible additional 3 (in stead of the full 7) marks. If this is the case, the student will have to state explicitly, in a clear highlighted paragraph, that they are opting for doing merely the 6 practice quizzes. In this case, the student work for these advanced features, if any, will not be marked and will thus not receive any marks. If a clear and explicit statement is not made by the student for the choice of doing the practice quizzes within his/her submission of this assignment, then it will be deemed that the student has chosen not to opt for the quizzes. These 6 quizzes will each count for 0.5 mark, giving a maximum total of 3 marks. The 6 practice quizzes can be done as many times as you want (with solutions given at the end) through the quiz tab for the SESSION c01, c02, .., c06, or directly through the following quiz links 1, 2, 3, 4, 5, 6. Ideally, students will do these practice quizzes as soon as possible and can decide at the time of submission of this assignment on whether to take up this Make-Up Clause or not. Please read the section Practice Quizzes below for more details.
    7. The following additional tasks are considered advanced features. As such, your C++ program will also be scrutinized more closely for the consistent coding style, meaningful variable names whenever suitable or necessary, proper spacing and alignment for the statements, active use of const variables for special quantities or values, sufficiently detailed and illuminative comments within the code. The program is also expected to provide clear input prompts and clean/well-formulated output, and to be written under the sound design principles.
    8. (3 marks) This is for the implementation of the menu options 4. and 5.. This part is for students aiming at achieving credit or above. When menu option 4. is selected, the program will calculate and display the standard deviation of all the available marks, before returning to the menu. When menu option 5. is selected, the program will allow you to delete one instance of the mark you want to delete, unless the mark doesn't exist.

    We recall that for a set of n values, x1, x2, x3, ..., xn-1, xn, its average μ (also denoted by an overline like x above the vector name x) or mean and the corresponding standard deviation σ are defined by

    μ = ( x1 + x2 + x3 + ... + xn-1 + xn )/n,
    σ2 = ( (x1 -μ)2 + (x2 -μ)2 + ... + (xn- μ)2 )/n

    For a list of 3 marks 56.5, 64.5 and 57, for instance, then the number n of the marks is 3, the average μ of these marks and the standard deviation σ are

    μ= (56.5 + 64.5 + 57)/3 = 59.33,
    σ = ( ((56.5-59.33)2+(64.5-59.33)2+(57-59.33)2)/3 )1/2 = 3.6591.

    9. (4 marks) This is for the implementation of the menu options 6. and 7.. This part is meant for the advanced students, aiming at achieving distinction or above. When menu option 6. is selected, the program will first read a mark from the user, and then search the available list of marks to see how many occurrences of this mark there are. It will then report this number of occurrences. When menu option 7. is selected, the program will go through all the distinctive marks in the list, and find out how many occurences there are for each of the distinctive marks. For instance, if the current list of marks contains

    56, 67, 86, 56, 56, 67, 77

    then the menu option 7. should generate something similar to

    MARKS OCCURRENCES
    56 3
    67 2
    86 1
    77 1

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Read the homework policy.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    This is want i have done so far

    Sry it works
    Last edited by wblade; 05-29-2011 at 10:26 PM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    ok, so what's your question?

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Well when i run the program no errors popup but only option 1 works

  6. #6
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    you need to post your code, so we can help..
    the usual problem with this (only one option works) is many forget to put a break if using switch statements (if you're using switch statements).
    You ended that sentence with a preposition...Bastard!

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    3
    I can get the options to work, but I can't work out how to only add ONE mark at time to the array in the first option "Add Marks"

    Code:
    void AddMarks(double Marks[]){
         cout << "Enter 10 Marks.\n";
         int next, index = 0;
         cin >> next;
         while (( next >= 0 ) && ( index < MAXSIZE ))
         {
    
         Marks[index]= next;
         index++;
         cin >> next;
         }}

  8. #8
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    next is the next grade to be entered, right?
    so you don't need the cin >> next before the loop
    and next >= 0 in the while condition is wrong since I can have a grade of hundred.
    while(index<MAXSIZE) should be fine..if MAX==length of array
    Code:
    void AddMarks(double Marks[]){
         cout << "Enter 10 Marks.\n";
         int next, index = 0;
        
        while (index < MAXSIZE)
         {
    
          cin >> next;
         Marks[index++]= next;
    
       }
    }
    You ended that sentence with a preposition...Bastard!

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    3
    Thanks, that works better. But what the assignment wants us to do, is when the user selects option 1 "Add Marks", it is supposed to only enter in 1 number then go back to the main menu and if the user selects option 1 again it inputs the next mark into the next position of the array. I am not sure how to do this.

    This is my whole code, so far:

    Code:
    #include<iostream>
    #include<cstdlib>
    #include<cmath>
    
    using namespace std;
    void ListMarks(const double [], int);
    
    const int MAXSIZE =10;
    
    void clrscr(){
      system("cls");
    }
    
    void pause(){
      system("echo.");system("echo.");system("pause");
    }
    
    void AddMarks(double Marks[]){
          
        cout << "Enter 10 Marks.\n";
        
         int next, index = 0;
        
        while (index < MAXSIZE)
         {
    
          cin >> next;
         Marks[index++]= next;
    
       }
    }
         
    void ListMarks(double Marks[]){
         for (int i=0; i < 10; i++)
         cout << Marks[i] << " ";
         cout<< endl;
         }
    
    void AvgMarks(double Marks[]){
         cout << "Average Marks" << endl;
         
         double sum, average;
         for (int i=0; i < 10; i++){
         sum += Marks[i];}
         
         average = sum/10;
         cout << average << endl;
         }
         
    void StdDevMarks(){
         cout << "Standard Deviation Marks" << endl;
         
         }
         
    void DelMarks(){
         cout << "Delete Marks" << endl;
         }
         
    void NumStudMarks(){
         cout << "Number of Students via Marks" << endl;
         }
         
    void DistMarks(){
         cout << "Distinct Marks and their Occurrences" << endl;
         }
    
    void displayMenu(string msg){
      clrscr();
      cout << msg << "\n\nMAIN MENU\
    \n0. Exit\
    \n1. Add a Student Mark\
    \n2. List all Student Marks\
    \n3. Calculate the Marks Average\
    \n4. Calculate the Standard Deviation\
    \n5. Delete a Student Mark\
    \n6. Find the Number of Students via a Mark\
    \n7. Display Distinct Marks and their Occurrences\
    \n\nYour choice -> ";
    }
    
    int main(){;
        double Marks[1000];
        int count;
    
       
    
      int option;
      string word, msg="Menu 1.0 18/5/2011";
    
      do{
        displayMenu(msg);
        cin.clear();
        if(!(cin >> option)){
          if(!cin.eof()){
            cin.clear();
            cin >> word; //consume an invalid word
          }
          continue;
        }
    
        switch(option){
        case 1: AddMarks(Marks); pause(); break;
        case 2: ListMarks(Marks); pause(); break;
        case 3: AvgMarks(Marks); pause(); break;
        case 4: StdDevMarks(); pause(); break;
        case 5: DelMarks(); pause(); break;
        case 6: NumStudMarks(); pause(); break;
        case 7: DistMarks(); pause(); break;
    
        pause();
        default:
          msg='0'+option;
          msg=msg + " was last selected";
        }
      } while (option!=0);
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  10. #10
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    mmn, well you could try and pass a reference of index to Addmarks..so whenever you need to add a mark it would at correct position.
    And you don't need a while loop if that's the case.

    so:
    if(index < maxSize)
    Mark[index++] = next;
    when that function ends it should return to main menu..I'd try it now

    EDIT:
    you could also use static int index..
    Last edited by Eman; 05-31-2011 at 09:40 AM.
    You ended that sentence with a preposition...Bastard!

  11. #11
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    yep, passing by reference works for me..the static index should also work
    You ended that sentence with a preposition...Bastard!

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If I am allowed to be so bold, I would suggest you replace all your arrays with std::vector. Otherwise you risk buffer overruns.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    May 2011
    Posts
    3
    Hey,
    I have changed my code, most of it works fine, except for a few functions:

    Function 5: When I enter say these 4 numbers into the array: 41, 42, 43, 44, 45 and then try to delete for instance 44, it will delete the element at AllMarks[0] (41) every time.
    Function 7: When I search for all marks and their occurrences it will display all the marks in the array and only say each occurs 1 time when it is listed, even when there is more then 1 of it occuring.

    They are the only issues I seem to be having with my complete program.
    Please help me as soon as possible, Thanks.


    Code:
    #include<iostream>
    #include<cstdlib>
    #include<cmath>
    #include<iomanip>
    
    using namespace std;
    
    const int size = 1000;
    
    bool AddMarks(double AllMarks[], int &TotalRec, double &Mark);
    bool DisplayMarks(double AllMarks[], int TotalRec, double& Mark);
    bool CalcAvg(double AllMarks[], int TotalRec, double &Avg);
    bool CalcStdDev(double AllMarks[], int TotalRec, double &Avg, double &StdDev);
    bool DelMarks(double AllMarks[], int TotalRec, double &DelMarks );
    bool FindMarks(double AllMarks[], int TotalRec, double &Occur, double& UserInput);
    bool MarkOccur(double AllMarks[], int TotalRec, double &AllOccur);
    
    int main(){
        
        double AllMarks[size] = {};
        double choice, Mark, Avg, StdDev, deleMark, UserInput, Occur, AllOccur;
        int TotalRec = 0;
        
        while (Mark >= 0 || Mark <= 100){
    
            cout << "MAIN MENU" << endl;
            cout << "0. Exit" << endl;
            cout << "1. Add a Student Mark" << endl;
            cout << "2. List all Student Marks" << endl;
            cout << "3. Calculate the Average Mark" << endl;
            cout << "4. Calculate the Standard Deviation" << endl;
            cout << "5. Delete a Student Mark" << endl;
            cout << "6. Find the number of Students via a Mark" << endl;
            cout << "7. Display Distinct Marks & their Occurrences\n" << endl;
        
            cout << "Your Choice -> ";
            cin >> choice;
                  
            while(choice == 0){
             return 0;
            }
            
            if(choice == 1){
             AddMarks(AllMarks, TotalRec, Mark);        
            }
            else if(choice == 2){
             DisplayMarks(AllMarks, TotalRec, Mark);
            } 
            else if(choice == 3){
             CalcAvg(AllMarks, TotalRec, Avg);
            }
            else if(choice == 4){
             CalcStdDev(AllMarks, TotalRec, StdDev, Avg);
            }
            else if(choice == 5){
             DelMarks(AllMarks,TotalRec, deleMark );
            }        
            else if(choice == 6){
             FindMarks(AllMarks, TotalRec, Occur, UserInput);
            }
            else if(choice == 7){
             MarkOccur(AllMarks, TotalRec, AllOccur);
            }
            else if(cin.eof() || cin.fail()){
             cin.clear();
             cin.ignore();
             cout << "\nOPTION IS INVALID ENTER ANOTHER OPTION!\n" << endl;
            } 
            else{
             cout << "\nOPTION IS INVALID ENTER ANOTHER OPTION!\n" << endl;
            }
            
            system("pause");
            system("cls");
        } 
        return 0;
    }
    
    bool AddMarks(double AllMarks[], int& TotalRec, double& Mark){
         
         int i;                                    
         cout << "\nAdd a Student Mark -> ";       
         cin >> Mark;                              
         AllMarks[TotalRec] = Mark;                
         TotalRec++;                               
         i++;                                      
         if (Mark <= 0 || Mark > 100){                                         
              cin.clear();
              cin.ignore();
              cout << "\nInvalid Student Mark.\n" << endl;
              TotalRec--;
         } 
         else if(cin.eof() || cin.fail()){               
             cin.clear();
             cin.ignore();
             cout << "\nInvalid Value.\n" << endl;
             TotalRec--;
         } 
         cout << "\n";
         return AllMarks[i];      
    }
    
    bool DisplayMarks(double AllMarks[], int TotalRec, double& Mark){
    
     cout << "\nAll STUDENT MarkS:\n " << endl;                     
     for (int i = 0; i < TotalRec; i++){                                                              
         cout << AllMarks[i] << "  ";
         } 
         
         cout << "\n\n";
         return AllMarks;                                            
    }
    
    bool CalcAvg(double AllMarks[], int TotalRec, double &Avg){
    
        double total = 0;                                       
    
        for (int i = 0; i < TotalRec; i++){                      
            total = total + AllMarks[i];                            
        }
        
        Avg = total/TotalRec;                                  
        cout << "\nThe Avg of all Student Marks is " << Avg << endl;
        cout << "\n";
        
        cout << setiosflags(ios::showpoint) 
             << setiosflags(ios:: fixed) 
             << setprecision(2); 
             
        return Avg;                               
    }
    
    bool CalcStdDev(double AllMarks[], int TotalRec, double& StdDev, double &Avg){
         
     float sum = 0;
                                                               
     for(int i = 0; i < TotalRec; i++){                                                                        
             sum += pow((AllMarks[i] - Avg), 2);                                    
             StdDev = sqrt(sum /(TotalRec - 1));
     }
     
     cout << "\nThe standard deviation of all Student Marks is " << StdDev << endl; 
     cout << "\n";
     
     cout << setiosflags(ios::showpoint)
          << setiosflags(ios:: fixed)
          << setprecision(2);
             
     return StdDev;
    }
    
    bool DelMarks(double AllMarks[],int TotalRec, double& deleMark){
         
         cout << "\nEnter the Mark to be deleted: ";              
         cin >> deleMark;
         
         if (deleMark <= 0 || deleMark > 100){
            cin.clear();
            cin.ignore();
            cout << "\nInvalid Student Mark.\n" << endl;
            return 0;
         } 
         else if(cin.eof() || cin.fail()){            
              cin.clear();
              cin.ignore();
              cout << "\nInvalid Value.\n" << endl;
              return 0;
         }
               
         for(int i = 0; i < TotalRec; i++){
                 if(AllMarks[i] == deleMark){
                                AllMarks[i] = deleMark;                                 
                                AllMarks[i] = AllMarks[i+TotalRec];                     
                                AllMarks[i+TotalRec] != '\0';                            
                                TotalRec -= TotalRec;
                                cout << "\nStudent Mark " << deleMark << " is deleted from the database" << endl;
                                cout << "\n";
                                return 0;                                                
                 }
                 else{
                                cout << "Student Mark doesn't exist.\n";
                                return 0;                                               
                 }
         }
    }
    
    bool FindMarks(double AllMarks[], int TotalRec, double& Occur, double& UserInput){
         
       cout << "\nEnter a Student Mark that you want to find: ";
       cin >> UserInput;
       Occur = 0;
       if (UserInput < 0 || UserInput > 100){                                                        
          cin.clear();
          cin.ignore();
          cout << "\nInvalid Student Mark." << endl;
          return 0;
       } 
       else if(cin.eof() || cin.fail()){
          cin.clear();
          cin.ignore();
          cout << "\nInvalid Value." << endl;
          return 0;
       }
       
       for(int i = 0; i < TotalRec; i++){
         if(UserInput == AllMarks[i]){ 
           Occur++;                                               
         }      
       }  
        cout << "\nThere are " << Occur << " student that achieved a Mark of " << UserInput << endl; 
        cout << "\n";        
    }
    
    bool MarkOccur(double AllMarks[], int TotalRec, double& AllOccur){
         
      int indexOfMin, i ,j;                                         
            
      for(i=0; i<TotalRec; i++){
            indexOfMin = i;                                         
            
            for (j=i+1; j<TotalRec; j++)                    
                if (AllMarks[j]<AllMarks[i])                        
                   indexOfMin = j;                                          
            
            swap (AllMarks[i], AllMarks[indexOfMin]);
       }
        
       cout << "\nMarkS     OccurRENCES\n";
                                 
       for(int i = 0; i < TotalRec; i++){
               cout << AllMarks[i] << "        " << "1" << endl;
       }  
    }  
    
    void swap (int &x, int &y ){ 
    
       int temp;                             
       temp = x;                             
       x = y;                                
       y = temp;                             
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Plz help me on my assignment
    By Agent89 in forum C++ Programming
    Replies: 6
    Last Post: 04-05-2002, 03:47 PM
  2. C++ Assignment
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 03-25-2002, 04:15 PM
  3. can someone help me with this assignment
    By sayeem81 in forum C Programming
    Replies: 3
    Last Post: 01-23-2002, 10:56 AM
  4. Im new at this and i need help with assignment
    By bounty hunter in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2001, 07:40 PM
  5. help with assignment
    By arcturus in forum C++ Programming
    Replies: 0
    Last Post: 11-29-2001, 07:32 PM