Learning C++

Well I started last week my Spring 2 semester.  Finally learning C++.

Ive been doing fairly well.  But I still dont get a lot of the knack it requires to be a successful developer.  I have hit some snags where the homework as made me go into it and actually write the statements by myself.  I have found that by writing them, I can figure it out better.

For example in Problem 11064:

Online Book Merchants offers premium customers 1 free book with every purchase of 5 or more books and offers 2 free books with every purchase of 8 or more books. It offers regular customers 1 free book with every purchase of 7 or more books, and offers 2 free books with every purchase of 12 or more books.

Write a statement that assigns  freeBooks the appropriate value based on the values of the bool variable  isPremiumCustomer and the int variable nbooksPurchased .

The correct answer is:

if (isPremiumCustomer) {
     if (nbooksPurchased >= 8)
          freeBooks = 2;
else if (nbooksPurchased >= 5)
         freeBooks = 1;
else
        freeBooks = 0;
}
else {
          if (nbooksPurchased >= 12)
          freeBooks = 2;
else if (nbooksPurchased >= 7)
         freebooks = 1;
else
        freeBooks = 0;
}
									

On problem 11119:

Given a double variable named areaOfSquare write the necessary code to read in a value, the area of some square, into areaOfSquare and print out the length of the side of that square.

It gave me a little bit of a problem but the solution came to me by changing the values I had wrong until they worked.  Granted not the way it should be, but what works works right?

cin >> areaOfSquare;

if(areaOfSquare >= 0)
{ 
length = sqrt(areaOfSquare);
cout << length;
}
else
{
cout << "INVALID";
}
									

I think I am going to re-read those chapters again.

 

 

Comments are closed.