Home >  T.A >  cs10 ( Fall 2001 ) >  Lab 8

  CS10 Sec#24 : Topics covererd in Lab #8
T.A: Demetris Zeinalipour
(Fall 2001)

Print this Page   
 [  Pre-packaged Functions  ::   Formatting Output  ::   Strings in C++  ::   Exercise #8 ]



Attention:
1) Please come on time and bring a #2 Pencil with You, since you 'll be asked to fill out the TA evaluation scantron.
2) You will receive your mid-terms along with the grade.
3) I will go through the solutions of the midterm and explain any points that you haven't understand.

Top


Pre-packaged Functions

When given a problem, you many time see that you need a specific function in order to accomplish your task.
Such a function may be the sin of an angle. For this reason we can use Pre-packaged Functions. The only thing we need to do is to scan the documentation of a particular library and try to find the function that meets our needs. After that we have to include the library that contains this function and then we can call it directly.
Example using (sin) from (math.h).

/* sin example */
#include <stdio.h>
#include <math.h>


#define PI 3.14159265

main ()
{
double param, result;
param = 45;
result = sin (param*PI/180);
printf ("Sine of %lf degrees is %lf\n", param, result );
return 0;
}
Output:
Sine of 45.000000 degrees is 0.707107

C Standard Libraries

( Use these libraries and add powerful functionality to your programs )

stdio.h :: stdlib.h :: string.h :: time.h :: iomanip.h :: math.h  ]
Top


Formatting Output - Take a course (at home)

#include <iomanip>

//Sets the format flags. 2 decimal points precision.
cout << setprecision(2) << setiosflags(ios::fixed | ios::showpoint);

/* Print first column - add spaces required to align 2nd column */
cout << label << setw(30-label.size()) << "end";

//Reset the format flags.
cout << resetiosflags (ios::fixed | ios::showpoint);

Top


Strings in C++

/* returns true if the given character is an integer (look at the table below)*/
bool isDigit(char c)
{
return (c>=48 && c<=57);
}


/* returns true if the given string is a real number */
bool isReal(string instring)
{
int size = instring.size();
int dotcount = 0;
char c;
for (int i=0; i<size; i++)
{
    c = instring[i];
    if (c=='.')
    { dotcount++; }
    else if (!isDigit(c))
    { return false; }
}

/* check if this number has more than 1 dots '.' */
if (dotcount<=1)
{ return true; }
else
{ return false; }

}

The ASCII Table
Top


Lab 8 Assignment - Exercise
New! - Download lab8.exe (win32) here and see how your program should correspond to various inputs,
See Solution - C++ solution here (will be available on Saturday 11/24/01)


Write a program that will compute the monthly payment of a fully amortized loan.
This program will allow the user to input two different scenarios and then use this information compare between two different loans. Your company strives to make the user feel important and that they care more than any other company in the effort of securing their loan. Thus, at the beginning of their program, ask for their first and last name. Use their name throughout the program to create a more personal atmosphere.

The user should provide the principal (amount borrowed), the annual interest rate, and the length of the loan in years for both loans being compared. Use the following formula where mpr is the monthly percentage (interest) rate and numMonths is the length of the loan in months



Output the information of both loans in the following format, be sure to format each amount as money (2 digits to the right of the decimal point. Be polite and use their name. Assuming a $5000 loan of .05 annual interest rate for 2 years and a second loan of $5000 of .055 annual interest rate for 2 years:



Provide error checking for your program so that it can handle incorrect types. If the user enters an incorrect type or an invalid value, keep making them input the value until it makes sense.

Use  both pre-packed functions and your own functions  otherwise your program won't be readable!

Notice: Be sure to put all your information (login, lab section, …) at the top of your program. Your lab programs DO NOT get turned in electronically. I will come around and check you off individually.

Top