mardi 23 février 2016

why declare "score[11] = {};" and "grade" as "unsigned" instead of "int'

I'm new to C++ and is trying to learn the concept of array. I saw this code snippet online. My question is, for the sample code below, does it make any difference to declare:

unsigned scores[11] = {};
unsigned grade; 

as:

int scores[11] = {};
int grade; 

I guess there must be a reason why score[11] = {}; and grade is declared as unsigned, but what is the reason behind it? Could someone please help me out? Thanks in advance for any help!

int main() {
    unsigned scores[11] = {};
    unsigned grade;
    while (cin >> grade) {
        if (0 <= grade <= 100) {
            ++scores[grade / 10];
        }
    }
    for (int i = 0; i < 11; i++) {
        cout << scores[i] << endl;
    }
}

Aucun commentaire:

Enregistrer un commentaire