-3

I'm trying to create this student calculator, however, I'm having a problem on how would I compute the overall. and also the solution for total number of passers(totalnpa) & total number of failures(totalnof).

enter image description here

#include <iostream>
using namespace std; 
int main(){
    int studNo[] = {1111,2222,3333,4444,5555};

    float overall, spr;
    float prelim[5] = {80, 70, 85, 90, 95};
    float midterm[5] = {90, 65, 85, 90, 85};
    float finals[5] = {70, 80, 90, 90, 85};
    float totalspr=0, totalnpa=0, totalnof=0;

    cout << "     Student Grades 2020" << endl; 
    cout << "\n  Student No." << "\t        Prelim" << "\tMidterm" << "    Finals" << "    Overall" << "    Remark"<<endl; 

    for (int i=0; i <=4; i++ ) {
        overall= 0.20 * prelim + 0.30 * midterm + 0.50 * finals;
        spr=5;
        totalspr+=spr;
        totalnpa+=
        totalnof
    
        if(overall >=75)
        {
        cout << "You Passed";
    }
    else if(overall <=75)
    {
        cout << "You Failed";
        }   
    
            cout << studNo << "\t" << prelim[i] << "\t" << midterm[i] << "\t    " << finals[i] << "\t" << overall << "\t" << "remark" << "\t" <<endl;
    }
cout << "\n   Summary : " << endl;
   cout << "\tTotal Students Printed : " << totalspr << endl;
   cout << "\tTotal Number of Passers : " << totalnpa << endl;
   cout << "\tTotal Number of Failures : " << totalnof << endl;


cout << "\nProgrammed by: ";
          
    return 0;
}   

This is my code so far, it is still incomplete as I'm having a hardtime on my "for (int),totalspr, totalnpa, totalnof" and the overall computations(can't make the operators right somehow).

tadman
  • 208,517
  • 23
  • 234
  • 262
Cedric22
  • 3
  • 5
  • 1
    Tip: Instead of a bunch of arrays that have no relationship, create an array of a single `struct` or `class` that represents all those properties, then add *N* of those to an array. It's important to note that `std::vector` is the go-to "array" in C++, so `std::vector` vs. the code here. – tadman Dec 16 '20 at 02:40
  • No home work,but OK for question is the SO's aim. So what you question?You should pick out the problem disturbed you. – ElapsedSoul Dec 16 '20 at 02:40
  • There's some problematic syntax errors in here you need to address. `totalnpa+=` and then nothing? Then a keyword on a new line with no `;`? – tadman Dec 16 '20 at 02:41
  • im sorry, i know its incomplete, im having a hardtime on the logic of how or what should I put on "totalnpa, totalspr, totalnof" so it would end up showing in the summary line. – Cedric22 Dec 16 '20 at 02:49
  • *"I'm having a problem"* -- what specifically is this problem? You have not given enough details to go beyond essentially "please do my homework for me". Ideally, your question should have enough focus and precision that your homework assignment fades into nothing more than a footnote, if that. – JaMiT Dec 16 '20 at 03:41
  • Thank you guys very much !! it is a huge help. – Cedric22 Jan 06 '21 at 08:54

1 Answers1

0

You have taken-off in somewhat an awkward direction for handling your student data. As mentioned in the comments, whenever you need to coordinate multiple values and/or differing types of data as one unit, you should be thinking struct or class. Say you want to create a simple:

struct student {
    unsigned id, prelim, midterm, final;
};

Now each student you create will have an id, a prelim grade as well as grades for midterm and final. Now when you create an array of student, each element will have each of the variables as a member-variable. (and with a struct the member access is public: by default, so you may access the values directly).

You can also declare and initialize all values in an array of student with simple brace-initializers, e.g.

int main (void) {
    
    student students[]  =  {{ 1111, 80, 90, 70 }, { 2222, 70, 65, 80 },
                            { 3333, 85, 85, 90 }, { 4444, 90, 90, 90 },
                            { 5555, 95, 85, 85 } };

Now all you need to do is calculate overall and output the results. A complete program providing output formatted similar to what is needed could be:

#include <iostream>

struct student {
    unsigned id, prelim, midterm, final;
};

int main (void) {
    
    student students[]  =  {{ 1111, 80, 90, 70 }, { 2222, 70, 65, 80 },
                            { 3333, 85, 85, 90 }, { 4444, 90, 90, 90 },
                            { 5555, 95, 85, 85 } };
    size_t n = sizeof students / sizeof *students;
    
    for (size_t i = 0; i < n; i++) {
        double overall = .2 * students[i].prelim + 
                         .3 * students[i].midterm + 
                         .5 * students[i].final;
        std::cout << '\t' << students[i].id << "\t\t" << 
                students[i].prelim << '\t' << 
                students[i].midterm << '\t' << 
                students[i].final << '\t' << 
                overall << '\t' << 
                (overall < .6 ? "Failed" : "Passed") << '\n';
    }
}

Example Use/Output

$ ./bin/students_overall_c
        1111            80      90      70      78      Passed
        2222            70      65      80      73.5    Passed
        3333            85      85      90      87.5    Passed
        4444            90      90      90      90      Passed
        5555            95      85      85      87      Passed

Making Your Struct Smarter

Now if you like, and if you need the overall value more than once, it would make sense to make that as a double member-value of your struct and you could write a short constructor for your struct to automatically calculate the overall value when initialized.

struct student {
    unsigned id, prelim, midterm, final;
    double overall;
    
    student (unsigned i, double p, double m, double f) : 
            id(i), prelim(p), midterm(m), final(f) {
                overall=.2 * prelim + .3 * midterm + .5 * final;
            }
};

Then all you need to do is initialize your array and print it:

#include <iostream>

struct student {
    unsigned id, prelim, midterm, final;
    double overall;
    
    student (unsigned i, double p, double m, double f) : 
            id(i), prelim(p), midterm(m), final(f) {
                overall=.2 * prelim + .3 * midterm + .5 * final;
            }
};

int main (void) {
    
    student students[]  =  {{ 1111, 80, 90, 70 }, { 2222, 70, 65, 80 },
                            { 3333, 85, 85, 90 }, { 4444, 90, 90, 90 },
                            { 5555, 95, 85, 85 } };
    size_t n = sizeof students / sizeof *students;
    
    for (size_t i = 0; i < n; i++)
        std::cout << '\t' << students[i].id << "\t\t" << 
                students[i].prelim << '\t' << 
                students[i].midterm << '\t' << 
                students[i].final << '\t' << 
                students[i].overall << '\t' << 
                (students[i].overall < .6 ? "Failed" : "Passed") << '\n';
}

(same output)

Let Your Struct Format Your Output

Since in C++ a struct and class have the same properties, you have the ability to overload the << operator within your struct to handle output formatting for you and not have to clutter up the logic of main() will the details. You can add an overload of << to your struct as follows:

struct student {
    unsigned id, prelim, midterm, final;
    double overall;
    
    student (unsigned i, double p, double m, double f) : 
            id(i), prelim(p), midterm(m), final(f) {
                overall=.2 * prelim + .3 * midterm + .5 * final;
            }
    
    friend std::ostream& operator<< (std::ostream& os, const student& s) {
        std::cout << '\t' << s.id << "\t\t" << s.prelim << '\t' << s.midterm << '\t' << 
            s.final << '\t' << s.overall << '\t' << (s.overall < .6 ? "Failed" : "Passed");
        return os;
    }
};

Now all you do is initialize your array and output each element:

#include <iostream>

struct student {
    unsigned id, prelim, midterm, final;
    double overall;
    
    student (unsigned i, double p, double m, double f) : 
            id(i), prelim(p), midterm(m), final(f) {
                overall=.2 * prelim + .3 * midterm + .5 * final;
            }
    
    friend std::ostream& operator<< (std::ostream& os, const student& s) {
        std::cout << '\t' << s.id << "\t\t" << s.prelim << '\t' << s.midterm << '\t' << 
            s.final << '\t' << s.overall << '\t' << (s.overall < .6 ? "Failed" : "Passed");
        return os;
    }
};

int main (void) {
    
    student students[]  =  {{ 1111, 80, 90, 70 }, { 2222, 70, 65, 80 },
                            { 3333, 85, 85, 90 }, { 4444, 90, 90, 90 },
                            { 5555, 95, 85, 85 } };
    size_t n = sizeof students / sizeof *students;
    
    for (size_t i = 0; i < n; i++)
        std::cout << students[i] << '\n';
}

(same output)

By using a struct to coordinate all of your values as a single object, you eliminate having to try and manually synchronize between multiple arrays by index. This dramatically reduces your programming complexity (and potential for error).

Try it both ways. Go ahead and write out the remainder of your code using multiple arrays. (you can basically initialize your arrays as you have, and then just draw from the output formatting above). The compare the complexity to using a single struct and decide what works best for you. If you haven't got to using a struct yet in your class, then consider this a preview of things to come.

Additionally, See: Why is “using namespace std;” considered bad practice?. Look things over and let me know if you have further questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • thank you for thoroughly explaining it to me sir, it is a huge help. I am slowly beginning to understand our lesson step by step :) – Cedric22 Jan 06 '21 at 08:55
  • Glad to help. Learning C++ isn't a race, it's moire of a journey, so just slow-down, pay attention to the details and enjoy the journey. After a semester you should have the basics down, but there will still be a whole lot of the language you will not have covered. After a year, you should a least recognize most of what C++ can offer, it will take a couple more to become truly proficient. (you never really get to the end of the journey, features are added, removed, the standard changed, etc..) Good luck with your coding! – David C. Rankin Jan 06 '21 at 09:07
  • oh I'm sorry, it's my first time doing stackoverflow. will do it :D – Cedric22 Jan 13 '21 at 09:15
  • No worries. You will do well. Just keep working at it -- at a reasonable pace. – David C. Rankin Jan 13 '21 at 09:20