You are on page 1of 6

C++  Review  

MAIN INFO
Calling for Functions & Namespace
#include
#include
#include
#include
#include

<iostream>
<string>
<cmath>
<cstdlib>
<time.h>

<——<——<——<——<——-

Input & Outputs
Calling for Variable String
Math Functions
Random Number
Random Number Addon (Time)

using namespace std;

---------------------------------------------------------------------

Variables

else{
return 0;}
}
int main()
{
cout<<"From (x= 499.98, y= 499.97, z= 499.99) The Maximum is:
"<<maximum(499.98, 499.97, 499.99)<<endl;

int soccerNum = 9;

return 0;
}
---------------------------------------------------------------------

double decimals = 9.4502;

Calling a Function in Main

char apple='a';

string myName = "Juan Pablo Garza”;

---------------------------------------------------------------------

Cout
cout<<“Hello World!”<<endl;

---------------------------------------------------------------------

Cin
int Num1;
cout<<“Enter A Number: “;
cin>> Num1

---------------------------------------------------------------------

Else

void converttime(int seconds, int& hours, int& min, int& sec)
{
sec = seconds % 60;
min = seconds % 3600;
min = min / 60;
hours = seconds / 3600;
}
int main()
{
int seconds,h,m,s;

if (TRUE){
// Execute these statements if TRUE
}

cout<<"Type in your seconds: ";
cin>>seconds;

else {
// execute these statements if FALSE
}

converttime(seconds,h,m,s);

---------------------------------------------------------------------

Else If
if (<condition>){
// Execute these statements if TRUE
}
else if (<another condition>){
// execute these statements if <another condition> is TRUE
// and <condition> is FALSE
}

---------------------------------------------------------------------

Switch
switch (code) { //add as many cases.
case 1:
cout<<"3M Corporation"<<endl;
break;
case 2:
cout<<"Maxell Corporation"<<endl;
break;
default:
break;
}

---------------------------------------------------------------------

While Loop
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double amount=1000,interest=.0325;
int count = 1;

cout<<"From "<<seconds<<" seconds we get "<<h<<" hours, “ <<m<<"
minutes, and "<<s<<" seconds."<<endl;
return 0;
}
---------------------------------------------------------------------

Random Numbers
int i=0, j=0, k=0;
int main()
{
for (i=0; i<10;i++) {
int v1,v2,answer;

v1 = rand()% 10;
v2 = rand()% 100 + 1;
v3 = rand()% 30 + 1985;

// v1 in the range 0 to 9
// v2 in the range 1 to 100
// v3 in the range 1985-2014

cout<<"How much is "<<v1<<" times "<<v2<<"? "<<endl;
cin>>answer;
if (v1*v2==answer) {
cout<<"CORRECT!\n";
k++;
}

while (count<=10) {
amount= amount +(amount*interest);
cout<<"Amount at the end of the year"<<count<<"is ($): "<<amount<<endl;
count++;
}

else{
cout<<"INCORRECT!\n<<";
j++;}

}

}

---------------------------------------------------------------------

For Loop
#include <iostream>
using namespace std;
int main()
{
double balance=1000.0;
(initialization,limit,count)
for (int i=0;i<10;i+=1){
//balance = balance + (balance*.08)
//balance = balance*1.08
balance *= 1.08;
cout<<"For year "<<i+1<<" your balance is $ <<balance<<endl;
}
return 0;
}

---------------------------------------------------------------------

Functions
double square(double a)
{
return a*a;
}
int main()
{
cout<< "9 squared is " << square(9) << endl;
return 0;
}
double maximum (double x, double y, double z)
{

double percentage=k*10;
if (percentage<80) {
cout<<"You got "<<percentage<<"% correct, Please ask your instructor
for extra help."<<endl;
}
else { cout<<"Good Job! you got "<<percentage<<"% Correct!"<<endl;
return 0;
}
}
---------------------------------------------------------------------

MATH
Add
—> +
Remainder —> %
Or
—> ||
Subtract
—> x^2
—> pow(x,2)
And —> &&
Divide
—> /
e^2
—> exp(2)
Multiply
—> *
Equality
—> ==
---------------------------------------------------------------------

EXAMPLES
int main() {
int month,day;
string season;
cout<<"Enter numerical Month: "<<endl;
cin>>month;

if (x>y && x>z) {
return x;
}

cout<<"Enter Date: "<<endl;
cin>>day;

if (y>x && y>z) {
return y;
}

if (month == 1 || month == 2 || month == 3)
season = "Winter";

if (z>x && z>y) {
return z;
}

else if (month == 4 || month == 5 || month == 6)
season = "Spring";

y. void max_and_avg(double& first. cin>>z. y. // s = how many spaces per row for (int a=1.m.C++  Review   else if (month == 7 || month == 8 || month == 9) season = "Summer". double& max. double& third. a++) // a = how many asterixs per row cout<<star. s<=r-i. } ———————————————------— Display Downward Triangle ——————---—————————— return 0.num. cout<< "Season: " << season << endl. } //-------------------------REVIEW QUESTION 2------------------------#include <iostream> #include <cmath> using namespace std..int& year. p++. int main() { double x.} void yrtime(int& days. cout<<"Enter first number: ". cin>>y. if (second>max) { max=second. char space=' '. int main() { double i. int& day) { year=days/365. a++) { for (int b = 1. else season = "Fall".tot=0. avg). z=10 is: "<<maximum(3. avg. i<=r. cout<<endl. } return 0. avg). month=month/30. } //-------------------------REVIEW QUESTION 1------------------------#include <iostream> #include <string> #include <cmath> #include <math. z. c <= 500. cout<<"Type the total number of data values to be averaged: ". c= "<<c<<endl. return 0. using namespace std. } //-------------------------REVIEW QUESTION 4------------------------#include <iostream> using namespace std. return 0.double& second. int main() { for (int a=1. a++) // a = how many asterixs per row cout<<star. } ————————---------------———Display Triangle—————------------—————————— int main() { int r=5. double z) { double max=x. ————————————————————-------——— 2nd Part ———————————————————————————— if (month % 3 == 0 && day >= 22) { if (season == "Winter") season = "Spring". max. cin>>x. } } int main() { cout<<"The maximum in x=3. cin>>days. —————————---————— void yrtime(int& days. b ++) { for (int c = 1. i>0.y. } int main() { int days. } for (int s=1. day=days%365. int& day). cin>>num. } } } return 0. y=8.h> using namespace std. i--) first { // i = the row where that I want to show for (int s=1. a <= 500. max. double y. for (int i=1. if (y>max) { max=y. double mult(double x. day=day%30. month=days%365.avg1. s<=r-i. cout<<days<<" days since the turn of the last century (1/1/1900) makes the day to be ("<<d+1<<"/"<<m+1<<"/"<<y+1990<<")"<<endl. return 0.h> #include <cstdlib> return max. else if (season == "Summer") season = "Fall". c ++) { if ((a*a + b*b) == (c*c)) cout<<"a= "<<a<<". max_and_avg(x. if (third>max) { max=thir } cout<<"Enter Days: ".int& year. double& avg) { max=first. else season = "Winter". } return 0. int& month. a<=2*i-1. double maximum(double x.2)<<endl. } //-------------------------REVIEW QUESTION 3------------------------#include <iostream> for (int i=4. z.y. } —————————————--------———— Pythagoras Theorem —————————-------——————— int main() { for (int a = 1. max. else if (season == "Spring") season = "Summer". cout<<"The Highest number you entered is: "<<max<<endl. b= "<<b<<". cout<<"Your Average is = "<<avg1<<endl. s++) cout<<space. i++) // i = the row where that I want to show first { return 0. int& month. // r = the last row char star='*'. } —————————————-----—— Function to find dates after. #include <time.d. a<=2*i-1. cout<<"Enter third number: ". y. 8. tot=tot+num. cout<<endl. while (p<i) { cout<<"Enter Number: ". } avg1=tot/i. b <= 500. } if (z>max) { max=z.d). yrtime(days. 10)<<endl.m. z. cout<<"Enter second number: ". cout<<"Your Number in entry "<<p + 1<<" is "<<num<<endl. cout<<"And the Average is: "<<avg<<endl. s++) // s = how many spaces per row cout<<space. double y) { return x*y. } avg=(first+second+third)/3.} . max_and_avg(x. cin>>i. cout<<"2*2 = "<< mult(2. p=0.

} int grades[GRADES] = {89. i++) { total[i] = 0. } display (a. } ------------------Alternating Sums (w/ Random Numbers)--------------#include<iostream> #include<cmath> #include<cstdlib> using namespace std. int).15. i++) cout << *(price + i) << endl. } cout << " Average = " << total[i] / COL << endl. cin >> grade[i][j]. void display(int[]. deviation[GRADES]. return 0. cout<<alternating_sum[i]<< ' '. display (a.73.3. i++){ //display array ----------------------------Arrays and Shifts------------------------ alternating_sum[i] = rand() % 20. j<COLS. 17. i < VALUES. } } system("pause"). for (int i = 0. j++) { cout << " Enter a grade: " << endl.69}. j<COLS.7). i < GRADES. i < GRADES. for (int j = 0.75. void zeropadshift(int[]. 15. "<<locj+1<<" in the list of numbers"<<". cout<<"This is the element with reference "<<loci+1<<". system("pause").17. cout << "(" << deviation[i] << ". j < COL. average. i < ROW. } } average = total / GRADES. and then a matrix of 3 by 3 from the 2nd for loop. i < GRADES. 12. total[i] = total[i] + grade[i][j]. return 0. for(i = 0. cout<<"\nThe maximum value of row "<<i<<" is: "<<max<<". 30. // "\t" is like clicking on the tab key. grade[ROW][COL]. 26.13.75. i++){ //initialize array //display before shifting //shifting //display after shifting return 0. i<ROWS.loci=0. " << grades[i] << ")" << endl. int i. i++) { deviation[i] = grades[i] . zeropadshift(a. int i. return 0.59. int main() { int const GRADES = 15. int fmax[ROWS][COLS]. cout << "(Deviation. ". 31.locj=0.99. for(i = 0. locj=j. 28. return 0. } // The VARIANCE is obtained by squaring each individual deviation and dividing the sum of the squared deviations by the number of deviations. j++) { cout << fmax[i][j] << "\t". int. 31. total = 0.11.18. 30.j++) { if (max<fmax[i][j]) { max=fmax[i][j].i++) { max=0. COL = 2. cout<<endl. for(int i=0. } --------------------------Arrays and Pointers-----------------------int main() { double *pt. 30. pt = price. j<COLS.82.29. i++) { total = total + (deviation[i] * deviation[i]).72. i = i + 3) cout << prices[i] << "\t" << prices[i+1] << "\t" << prices[i+2] << endl. int main() { int a[7] = {1. } --------------------------------------------------------------------#include <iostream> using namespace std. } //you can use max=INT_MIN to get the minimum value of the array --------------------------------------------------------------------int main() // This example shows the a column of 9 arrays in first for loop. for(int i=0.95. 6.75.15. 31. 7. Arrays for (int i = 0. 2 ).43. for (int i = 0. 31.C++  Review   ---------------------------Grades and average------------------------ MAIN INFO Calling for Functions & Namespace int main() { int const ROW = 3.2. cout << "Variance = " << variance << endl.average.i) * alternating_sum[i]. i < 7. for(int i=0. } #include <iostream> using namespace std. } s+= pow(-1. i++) cout << prices[i] << endl.32.45.59}."<<endl.99. 31 }. i < VALUES. Grade)" << endl. } ----------------------------Max value per Row------------------------ for(i = 0. for(int i=0. i<ROWS. 6. i < ROW. for (int i = 0. for(int j=0. for(int i=0.92. j++) { cout << grade[i][j] << ". return 0. const int COLS=20. } return 0.7).22. int main() { const int ROWS=10.7}. 16.54. number[0] number[1] number[2] …………………………………………………………………………………………………………………………………………………………… number[11] Number : &Number : &number[0] : 1245020 1245020 1245020 int *pNumber = number number[0] *pNumber : : 31 31 Number+1 : &Number[1] : 1245024 1245024 // first initialize the pointer for (int i = 0. j < COL.83.5.73. } CONTINUED------------------------Next Page----------> . 17. 31. double prices[VALUES] = {10. total = 0.54. 30. s=0. } variance = total / GRADES. i<10. // DEVIATION is obtained as the element value less the average of all the data.6. --------------------------------------------------------------------- int total[ROW]. system("pause"). int main(){ int alternating_sum[10]. { const int VALUES = 9.4. loci=i. i++) { total = total + grades[i]. i<10. } int max=0.69."<<endl. i++) { for (int j = 0.95.j++) { fmax[i][j]=(rand() % 101). } } int number[] = { 31.9. variance. i++) { for(int j=0. double price[]={12.i++) { for(int j=0. //"s+" makes the first value positive } cout<<s<<endl.98.44. system("pause"). } cout << endl. int). i<ROWS.79.86.87}.

//array length system("pause"). i++) { cout<<"intValue["<<i<<"] contains the value --> "<<intValues[i]<<endl.mpg = s[i]. for(int i = 0. i++) { cout << "Enter the car number: ". cycle= "pm".name. count = 0. } int main() { string a. } --------------------------------Average MPG-------------------------struct car { int num. return 0. --------------------------File to another File----------------------- int intValues[]={}. i++) { cout << s[i]. i < distance. getline (cin. i++) array[i] = 0. cout << "Enter the estimated price-to-earnings ratio " << i+1 << ": ". for(int i = 0. while(file. int minutes. cout << "Enter the gallons used: ". total = total + s[i]. }   .mpg << endl. pvalues. int count = 0. getline (cin. cin >> s[i].ratio. int hour. int main() { string text.bak"). system("pause").gallons. system("pause"). cin >> s. cout << " Enter a Name: ". cin >> s[i]. s[i]. int size) { cout<<"The array is: ". for(int i = 0. count ++. } return 0. cout << " Enter a string: " << endl.num. i < 5.1). int main() { struct time militaryTime. i++) { if(a[i] == 'e') a[i] = 'x'. } for(int i = 0. for (int i=0.txt"). struct stock { char name[10]. cin>>militaryTime.i . file. cout<<"Input the minutes (0-59):".ratio. temp++. cout << endl. cin >> s[i]. while(s[i] != '\0'){ in the for loop i++. int main() { stock s[5]. ---------------------------------Stocks----------------------------- cout<<"Input the hour in military form (0-23):". } // This way it gets to array[0].hour<<".length() .pvalues = s[i].hour-12. for(int i = 0. ratio. } cout << "Average MPG total is: " << total / 5 << endl. i++) { cout<<"Enter desired value for array " <<i+1<<": ". if (distance > 0 && distance <= size) { for (int i = size . pt = *intValues.1' #include #include #include #include <iostream> <fstream> <cstdlib> <string> using namespace std.mpg. i < a. } cout<<"for hour= "<<militaryTime.1]. } second. return 0. cin >> s[i].length(). cin >> s[i].close().open("employee. s[i]. } --------------------Giving Array values with pointers---------------int main() { double pt. for (int i=0.pvalues << endl. } double total = 0. return 0.length(). i < 5. we need that '. cout << "Enter the estimated earnings per share of stock " << i+1 << ": ". return 0. ifstream file. the display is "<<hour<<":"<<militaryTime. miles. int size. } }. cout << " MPG: " << s[i]." } -----------------------------Counts the spaces----------------------void display(int array[]. a). } cout<<endl. } --------------------Changing string Backwards-----------------------void reverse(char s[]) { int i = 0. i < a. cin>>militaryTime.1. second<<text. double mpg.text). int distance) { int temp=0. cout << "Enter the miles driven: ".C++  Review   void zeropadshift(int array[]. minute= "<<militaryTime. a).num. i < 5. i--) { array[i] = array[size . i < 5.hour>=12){ hour=militaryTime. i < count. for (int i = 0. i++) {cout<<*(pt + count . double pshare.distance . } } int main() { char s[100]. } cout<<endl. cout << s[i]. } ------------------------Change the e’s in string--------------------int main() { string a. cin >> s[i]. ofstream second. i++) { cout << "Enter the name of the stock " << i+1 << ": ". }. cin>>*(intValues+i). reverse(s). } system("pause"). for (int i = 0.gallons. } } //same as "cin>> a. return 0.pshare * s[i]. struct time{ int hour. cout << " Enter a string: " << endl.miles. gallons. } else { cycle= "am". } cout << a << endl. file.1. int main() { car s[5]. } char *pt.miles / s[i]. string cycle. i++) { cout<<array[i]<<" ". for(int i = 0.pshare. second. //These loop gives me the value for the COUNT that im using for(int i = 0.open("employee. i < 5. }.temp . i < size. } if (militaryTime.1] == ' ') cout << count << endl. i < 5.minutes<<cycle<<endl. pt = s. using namespace std. i++) { if(a[i] == ' ' && a[i + 1] != ' ') count++. return 0.close().hour. else cout << count +1 << endl.minutes<<". } ------------------------Display Time differently--------------------#include <iostream> #include <string> if(a[a.name.good()){ getline(file. i > distance .minutes. i++) { cout << "Car number: " << s[i]. return 0.

Date start[1].30) + (1 * 1) + ((1900 * 360) .month)* 30) . } employee[0].Number = 6985. cin >> start[0]. cin >> x.Hours = 40. " << endl.Name = "Timmins".Rate = 6. }.year.Number = 3462. cout << "Please enter a starting day. int y.day)+(((days[i]. //all years have 360 days and each month consists of 30 days Date start[1].day)+(((start[0].month. cin >> start[0].Hours. cin >> days[0]. employee[4]. cin >> z. " << endl. cout << "Please enter Person B's birthday day. bdays=0. cin >> days[i]. employee[4].C++  Review   } ------------------------------Who is Older?-------------------------#include <iostream> #include <string> cout << '\n'.Hours = 38. using namespace std.75. " << endl. cout << a[i]. total+= a[i]. cin >> days[i]. cout << "Please enter a starting month. } void older(int x. " << endl.year. i++){ a[i]. employee[2]. int adays=0.//declare void days(int x. employee[0]. cin >> days[0]. " << endl. cout << "Please enter a range-to day." << days[i].day. -----------------------------Total Gross Pay------------------------#include <string> #include <iostream> using namespace std. i < 5. cin. Date days[1]. } cout<<"The total gross pay of all employees is: "<<total<<endl. " << since[i] << " days have passed. employee[3]. int z) { cout << "Please enter the current month: " << endl.Rate = 5. employee[4]. int NUM=6. max = a[i]. employee[0]. cin >> y. cin >> start[0]. int day.Number = 7834. adays = ((((start[0]. float Rate.day << ".month)* 30) . }.(((1 * 30) .30) + (start[0].Name = "Jones".year << " to the date " << days[i].Rate = 4. } employee[3].Rate*a[i]." << days[i].Number = 6793. cin >> start[0].year)* 360) 360)).30) + (days[0]. bdays = ((((days[0]. cin >> y.month << ".month.Grosspay. int year. older(adays. employee[0]. for (int i = 0. bdays).Grosspay = a[i]. return 0. cout << "Please enter the current year " << endl. i < NUM.year)* 360) 360)).Name = "Smith".Name = "Swain". i < NUM. i++) { since[i] = ((((days[i]. cout << "Please enter a starting year.Number = 8867.Rate = 6. } } int main() { struct Date { int month. cout << "Please enter Person A's birthday month.Number = 9002.year << ". cout << "Please enter Person A's birthday day. float Grosspay. " << endl. float Hours.89. return max. " << endl. for (int i = 0. " << endl.Rate = 4. string Name. i++) { cout << "Please enter a range-to month. employee[3]." << start[0]. int z) { cout << "Please enter the current month: " << endl. cin >> start[0]. Date days[6].day)+(((days[0].month << ". int main() { struct Date { int month.Grosspay << endl. cout << "Please enter the current (day) date: " << endl. struct Employee{ int Number.Grosspay. for (int i = 0. for (int i = 0. cout << "\n Since the date " << start[0]. " << endl. //all years have 360 days and each month consists of 30 days employee[1]. int y.Rate = 5. void payroll(Employee *a){ float total=0. cout << "Please enter Person B's birthday year. //system("pause").43. int y) { if(x<y) { cout<<"Person A is older"<<endl. cout << "Please enter the current (day) date: " << endl. system("pause").day. cin >> x." << endl.Name << "\t " << a[i].year)* 360) 360)) . cin >> days[i].year. }.Grosspay. cin >> start[0]. } void days(int x. cin >> z. { Employee employee[6].Name = "Robbins".62.Hours = 40.month.30) + (days[i]. } } #include <iostream> #include <string> int main() using namespace std.month. employee[2]. cout << "Please enter Person A's birthday year. cin >> days[0]. } return 0.Grosspay) cout << '\n'. employee[1].Hours = 45.day.get(). int since[5].Hours = 35.Number << "\t " << a[i].month)* 30) . i < 5. i++){ if (max < a[i].22.Name = "Williams". int year. employee[5].360)).year. cout << "Please enter a range-to year.day << ". cout << "\nPlease enter Person B's birthday month. . " << endl. employee[3]. ---------------------How many days have passed?---------------------- employee[1]. employee[5]. } else { cout<<"Person B is older"<<endl. employee[1]. employee[2].83. } float highest(Employee *a){ float max = a[0]. employee[4]. int day. " << endl. " << endl. employee[5]. cout << "Please enter the current year " << endl." << start[0]. employee[2].day.

5. } cout << endl.15. return 0. i < ROW. line)){ //deleted_file = const_cast<char*>(line.16. *mp.3. ofstream outfile("modified_verse. //the name of the array is the first address of the element. for (i = 0. } int main(int argc. //char* deleted_file.5 + grades[i][1]*.C++  Review   employee[5]. } -----------------------------------More on Pointers-------------------------------#include <iostream> using namespace std. } //Test(). "hello").300.txt"). } system("pause").8.Hours = 42. grades[ROW][COL].4}.15.h" #include "string" #include "stdlib.3. return 0. i++) { for (j = 0. grades[i][j] = (rand() % 61 + 40). system("pause"). strcpy_s(str. cout << str << endl. j < COL.h" #include "sstream" #include "iostream" #include "fstream" using namespace std. char str[7].3. for(int i = 0.189. i < 10.//the name of the array is the first address of the element. means employee //is the pointor points to employee[0] -----------------------------------2nd Part---------------------------------------float maxpay = highest(employee).7. pos).3}.6. j < 3. } -------------------------------Random Cols and Rows-------------------------------int main() { int const ROW = 4.6. mp = mpg. int i.6.4. "hello world"). mpg[10]. g = gallons. return 0. double *m.280. while (getline(infile. i++) { *(mp + i) = *(m + i) / *(g + i). trim(line). ifstream infile("verse.192. strcpy_s(str1. } -----------------------Skips leading Whitespaces in Each Line---------------------#include "stdafx.txt"). j++) { cout << grades[i][j]<<"\t".} payroll(employee).4. int main() { double miles[10] = {240.14.9. . m = miles. char* argv) { string line.7.9. pos = str. cout << "strlen(str1):" << strlen(str1) << endl << "strlen(str):" << strlen(str) << endl. cout << *(mp + i) << endl.get(). if (pos == string::npos) return str.h" #include "stdio.206.3.177. j++) { // srand(time(0)).14. gallons[10] = {10. i < ROW. j.10.160. printf("\n").erase(pos. outfile<<line.3. string::size_type pos = 0) { static const string delim = " \t". *g. str1 = new char[20]. cout << "The highest gross pay is "<<maxpay << endl. } void Test(void) { char *str1 = NULL. COL = 4.310. return 0. } system("pause").2 + grades[i][2]*.7. cin. i++) { for (j = 0.find_first_of(delim.199. 1)).8.c_str()). string& trim(string &str. printf(str1).7. 20. //From "40" to 61+40=101 so "100" } grades[i][3]= grades[i][0]*.8. } for (i = 0. return trim(str.