Show Menu
Cheatography

CS 102 Exam 2 Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Header

#include <io­str­eam>
#include <io­man­ip>
#include <st­rin­g>
#include <cm­ath>
#include <cs­tdl­ib>
#include <fs­tre­am>
#include <ss­tre­am>
#include <ve­cto­r>

Function Prototypes

void functi­onN­ame­(const double[], int, int&);
int anothe­rNa­me(int scores­[][3], int size);

Function Calls

functi­onN­ame­(te­mpe­rat­ures, size, high);
anothe­rNa­me(­scores, SIZE);

Problems

Subscript out of bounds
Too many or few loops
No decimals
Multiply by 1.0 or change double
error: expected unqual­ifi­ed-id before
Use instance instead of type

String Manipu­lation

str.su­bst­r(pos, len)
returns string from pos
str.fi­nd(­str1)
returns location index
str.fi­nd(­str1, pos)
starts at pos then finds
str.in­ser­t(pos, str1)
inserts str1 at pos
str.er­ase­(pos, length)
removes length at pos

Functions

Random #
(rand() % (max-m­in+­1))+min
Power
pow(base, exp)
Abs Value Int
abs(num)
Abs Value Double
fabs(num)

OOP

vector
.push_­back()
 
.resize()
fstream
.open()
 
.fail()
inserting filename
.c_srt()
 

Seeding the Function

cin>>seed;
srand(seed);

Switch

switch(trigger){
   case 0: break;
   case 1: break;
   default: break;
]

Bubble Sort for Array

int temp;
bool sorted = 0;
while(!sorted){
   sorted=1;
      for (i=1;i<num_elements;i++){
         if(arr[i-1]>arr[i]){
            temp=arr[i-1];
            arr[i-1]=arr[i];
            arr[i]=temp;
            sorted=0;
         }
      }
}

Infinite Loop

while (true){
   break;
}

Insert and Erase / Fix

void fixSentence(string& sentence){
    int i = sentence.find(" ");
    string firstnoun;
    firstnoun = sentence.at(i+1);
    
    if (firstnoun == "a"&&"e"&&"i"&&"o"&&"u"){
        sentence.insert(1,"n");
    }
    
    else{
    int j = sentence.find(" ", i + 1);
    int k = sentence.find(" ", j + 1);
    int verblength = (k - (j+1));
    sentence.erase(j+1, verblength);
    sentence.insert(j+1, selectWord(VERBS, NUM));
    }
}

Extract from string stream

watch = "";
string tempstr;
sin>>day>>month>>year>>clouds>>temp>>wind>>watch;
while (sin>>tempstr){
   watch = watch + " " + tempstr;
}