0% found this document useful (0 votes)
4 views4 pages

CheetSheetCIS22AFinal

This cheat sheet covers key concepts for the CIS22A Final, including arrays, loops, functions, searching, sorting, and file I/O. It provides examples of each concept, such as array initialization, loop structures, linear and binary search algorithms, and bubble and selection sort methods. Additionally, it includes a sample code demonstrating the use of arrays and file handling in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

CheetSheetCIS22AFinal

This cheat sheet covers key concepts for the CIS22A Final, including arrays, loops, functions, searching, sorting, and file I/O. It provides examples of each concept, such as array initialization, loop structures, linear and binary search algorithms, and bubble and selection sort methods. Additionally, it includes a sample code demonstrating the use of arrays and file handling in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

My cheat sheet for CIS22A Final

1. Arrays:

- Store multiple values in contiguous memory. Example: int arr[5];

- Access with 0-based indexing. Example: arr[0] = 10;

- Partially initialize arrays. Remaining elements default to 0. Example: int arr[5] = {1, 2};

- Compare arrays element-by-element using a loop.

2. Loops:

- while: Pre-test loop. Example: while (i < 5) { i++; }

- do-while: Post-test loop, executes at least once. Example: do { i++; } while (i < 5);

- for: Counter-controlled loop. Example: for (int i = 0; i < 5; i++) { ... }

- Nested loops process 2D data. Example:

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) { ... }

- Sentinel values terminate loops when input is unpredictable.

3. Functions:

- Modular programming breaks tasks into reusable functions.

- Passing arrays by reference modifies original data.

- Use `const` to prevent modifications. Example: void printArray(const int arr[], int size);

- Functions return values or booleans for decision-making. Example: bool isPositive(int n) { return n > 0; }

4. Searching:

- Linear Search: Iterates through all elements.

int linearSearch(int arr[], int size, int value) {

for (int i = 0; i < size; i++) { if (arr[i] == value) return i; }

return -1;

- Binary Search: Divide-and-conquer on sorted arrays.

int binarySearch(int arr[], int size, int value) {

int low = 0, high = size - 1;

while (low <= high) {


int mid = (low + high) / 2;

if (arr[mid] == value) return mid;

if (arr[mid] < value) low = mid + 1; else high = mid - 1;

return -1;

5. Sorting:

- Bubble Sort: Repeatedly swaps adjacent elements.

void bubbleSort(int arr[], int size) {

for (int i = 0; i < size - 1; i++) {

for (int j = 0; j < size - i - 1; j++) {

if (arr[j] > arr[j + 1]) { std::swap(arr[j], arr[j + 1]); }

- Selection Sort: Locates and swaps the smallest elements.

void selectionSort(int arr[], int size) {

for (int i = 0; i < size - 1; i++) {

int minIdx = i;

for (int j = i + 1; j < size; j++) {

if (arr[j] < arr[minIdx]) minIdx = j;

std::swap(arr[i], arr[minIdx]);

6. File I/O:

- Use fstream for input/output operations. Example:

std::ifstream inFile("data.txt");

std::ofstream outFile("output.txt");

std::string data;

while (inFile >> data) { outFile << data; }

- Handle errors with `fail()` and close files using `close()`.


7. Combining Arrays, Loops, and Sorting:

- Example: Searching in a sorted array.

void searchSortedArray() {

int arr[] = {3, 1, 4, 1, 5};

selectionSort(arr, 5);

int index = binarySearch(arr, 5, 4);

if (index != -1) { std::cout << "Found at: " << index; }

8. Some example code

- #include <iostream>

#include <fstream>

#include <string>

using namespace std;

const int MAX_SIZE = 100; // Maximum size for arrays

int main() {

// Arrays to store data

string stringArray[MAX_SIZE];

int intArray[MAX_SIZE];

// Open the file

ifstream inputFile("data.txt");

if (!inputFile) {

cout << "Error: Could not open file." << endl;

return 1;

// Read data into arrays

int strIndex = 0, intIndex = 0;

string data;

while (inputFile >> data) {

if (isdigit(data[0])) { // If data starts with a digit, treat it as an integer


intArray[intIndex++] = stoi(data);

} else { // Otherwise, treat it as a string

stringArray[strIndex++] = data;

// Close the file

inputFile.close();

// Display string array

cout << "String Array:" << endl;

for (int i = 0; i < strIndex; i++) {

cout << stringArray[i] << " ";

cout << endl;

// Display integer array

cout << "Integer Array:" << endl;

for (int i = 0; i < intIndex; i++) {

cout << intArray[i] << " ";

cout << endl;

// Example usage: calculate the sum of integers

int sum = 0;

for (int i = 0; i < intIndex; i++) {

sum += intArray[i];

cout << "Sum of integers: " << sum << endl;

return 0;

You might also like