Exercises

Pages: 12
Hey everybody, I have a beginner's C++ book that I am very happy with, but it doesn't really have any exercises. I was wondering if anybody could point me in the direction of some good ones. Thanks all.
Wow. Thank you Null.
Ok, I just finished the first exercise(grading machine). It works, but did I do it the hard way?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <stdlib.h>
using namespace std;

int main(int argc, char *argv[])
{
  int grade; /* The user's grade */
	cin >> grade; /* I think I should be using cin.getline or something like that? Could someone clarify?*/
	while (grade <0 || grade > 100) { /*If the user enters a grade that is less than 0 or more than 100 the program repeats.*/
        cout << "Please enter a valid grade.\n";
        cin >> grade;
    }
	if (grade >= 90 && grade <= 100) { /* This series of if and else if statements tells the program how to determine what the correct grade is.*/
		cout << "You got an A. \n";
	}
	else if (grade >= 80 && grade <= 89) {
		cout << "You got a B. \n";
	} 
	else if (grade >= 70 && grade <= 79) {
	    cout << "You got a C. \n";
    }
    else if (grade >= 60 && grade <= 69) {
        cout << "You got a D. \n";
    }
    else if (grade >= 0 && grade <= 59) {
        cout << "You got an F. \n";
    }
    
    system("PAUSE");	
    return 0;
}
The if/else if statements don't need the &&'s after them. If you get into those statements, its assumed that you're already less than or equal to the latter statements.
Oh groovy. That's awesome. Now, if I want the program to take multiple grades from a user and store them as variables, do I need to declare them at the start of the program(int grade1, grade2, etc.) or is there a way to tell the program to automatically create variables as the user enters them? I'm looking through my book, but I'm just not seeing it. Maybe I'm looking in the wrong section...
Use a vector. Then push another integer onto the end after every input. You can cycle through the vector vec.length() times and each time, evaluate one of the grades.
No way you'll be able to do that with arrays or with static variables.
... *scratches head, thumbs through pocket guide and humongous book* I'm not quite following, but I found a section on vectors in my books, so I'm gonna fool around with this for awhile. I'll probably be back with questions sooner or later, haha. Thanks for the tip.
Why couldn't s/he do something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int grades;
cout << "Enter how many grades you want to enter: ";
cin >> grades;

int gradeArray[grades];

for (int i=0; i < grades; i++)
{
    cout << "Enter the next grade: ";
    cin >> gradeArray[i];
}

for (int i=0; i < grades; i++)
{
    cout << gradeArray[i] << endl;
}


That's without using vectors.
Last edited on
But it's not indeterminate. Vectors are the only container that are freely resizable. Arrays are fixed. That's why I consider it an inelegant solution.
But s/he's a beginner and its his/her first program... I don't think introducing a concept like vectors that soon would be fruitful. Its far too advanced for someone JUST starting out....

At least, I think.
closed account (jwC5fSEw)
@oghmaosiris

You do understand that you can't declare an array like that, right? You'd have to use dynamic memory with new, which I think is more complicated than vectors.

Besides, learning how to use the standard containers is a valuable skill, and the basics of using them is easier than going through the pains of resizing dynamically allocated arrays.
I declared an array like that and it worked for me in the test program I did.
closed account (jwC5fSEw)
Huh, I wasn't aware that it would even compile.

However, it does provide a very telling warning:

C:\CodeBlocks\Exercises\test.cpp|9|warning: ISO C++ forbids variable length array 'gradeArray'|


If ISO C++ forbids it, it's not a very good idea to do it.
I don't get that warning with my compiler...
closed account (jwC5fSEw)
Do you compile with -pedantic? It enables warnings based on ISO C++.

EDIT: That is, assuming you use g++. If you use VC++, I'm sure there is a similar flag.
Last edited on
DevC++

Or Borland C++ when I'm at home.
closed account (jwC5fSEw)
Okay, Dev-C++ uses g++. I'd recommend turning on -pedantic (for standard compliance-related warnings) and -Wall (for basically everything else). It's sorta sloppy to leave in anything that causes warnings, especially if the warnings result in non-compliant code.
Well, I didn't know it was non-compliant, lol...

And I usually fix all my warnings when I get them. I don't like sloppy code.

But now I know, and knowing is half the battle.
closed account (jwC5fSEw)
This got me curious, as I wasn't aware that variable length arrays would even compile, so I did a bit of research. Variable length arrays were added to C in the C99 standard, and gcc supports it. I'm sure it was trivial to make g++ support it as well.

That being said, it should be considered implementation-specific, and therefore incredibly non-portable. Play it safe and stick to dynamically-allocated arrays in C++ (or better yet, use vectors!).
Pages: 12