Show Menu
Cheatography

the not-yet optimal algorithms Cheat Sheet by

some algorithms that i came up with, they are certainly not in-place nor perfect

void findMin (int a[], int n)

#include <iostream>
void findMin (int a[], int n)
{
    int min = a[0];
    for (int i = 1; i < n; i++)
    {
        if (a[i] < min)
        {
            min = a[i];
        }
    }
    std::cout << "The minimun element is" << min << std::endl;
}

Arrays­/fi­ndIndex

#include <iostream>
void findIndex(int a[], int n, int key)
{
    int start = -1, end = -1;
    for (int i = 0; i < n; i++)
    {
        if(a[i] == key)
        {
            start = i;
            break;
        }
    }
    for (int j = n - 1; j >= 0; j--)
    {
        if(a[j] == key)
        {
            end = j;
            break;
        }
    } 
    if (start == -1 || end == -1)
    {
        std::cout << -1;
    }
    else
    {
        std::cout << start << " " << end;
    }
}

int main()
{
    int n;
    std::cin >> n;
    int a[n];
    for (int i = 0; i < n; i++)
    {
        std::cin >> a[i];
    }
    int key;
    std::cin >> key;
    findIndex(a, n, key);
    return 0;
}

đếm số lần xuất hiện của giá trị x

#include <iostream>
int countOccurrences(int a[], int n, int x)
{
    int count = 0;
    for (int i = 0; i < n; ++i)
    {
        if(a[i] == x)
        {
            ++count;
        }
    }
    return count;
}

kiểm tra mảng có tăng dần không

#include <iostream>
bool isAscendingOrder(int a[], int n)
{
    for (int i = 0; i < n - 1; ++i)
    {
        if(a[i] > a[i + 1])
        {
            return false;
        }
    }
    return true;
}

concat­Two­Arrays

#include <iostream>
void concatTwoArrays(int a[], int n, int b[], int m, int c[])
{
    int k = m + n;
    int j = n;
    for (int i = 0; i < n; ++i)
    {   
        c[i] = a[i];
    }
    for (int g = 0; j < k; ++g)
    {
       c[j] = b[g];  
       ++j;
    }
}

xuất chuỗi theo thứ tự ngược

#include <iostream>
void reverseCharArray (char a[], int n)
{
    char tmp;
    for (int i = 0; i < n / 2; ++i)
    {
        tmp = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = tmp;
    }
}

kiểm tra chuỗi đối xứng

 #include <iostream>
bool checkSymmetricArray(char a[], int n)
{
    for (int i = 0; i < n / 2; i++)
    {
        if(a[i] != a[n - i -1])
        {
            return false;
        }
    }
    return true;
}

đếm số lần xuất hiện của kí tự c

int countOccurrencesChar(char a[], int n, char c)
{
    int count = 0;
    for (int i = 0; i < n; ++i)
    {
        if (a[i] == c)
        {
            ++count;
        }
    }
    return count;
}

Hàm nhập mảng m dòng n cột

void set2dArray(int a[][MAX], int &m, int &n)
{
    for(int i = 0; i < m; i++)
    {
        for(int j = 0; j < n; j++)
        {
            std::cin >> a[i][j];
        }
    }
}

h tổng các phần tử của mảng 2d

int sumArray(int a[][MAX], int m, int n)
{
    int sum = 0;
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            sum = sum + a[i][j];
        }
    }
    return sum;
}

Hàm tính tổng đường chéo chính

#include <iostream>
int sumDiagonal (int a[][MAX], int m, int n)
{
    int sum = 0;
    for(int i = 0; i < m; i++)
    {
        sum = sum + a[i][i];
    }
    return sum;
}

đếm mảng có bao nhiêu số nguyên tố

#include <iostream>
#include <cmath>
bool checkPrime(int n)
{
    if (n <= 1)
    {
        return false;
    }
    else if (n == 2)
    {
        return true;
    }
    else if (n % 2 == 0)
    {
        return false;
    }
    else if (n > 2)
    {
        for (int i = 3; i <= sqrt(n); i += 2)
        {
            if (n % i == 0)
            return false;
        }
    }
    return true;
}
int countNumberofPrime (int a[], int n)
{
    int count = 0;
    for(int i = 0; i < n; i++)
    {
        if(checkPrime(a[i]))
        {
            ++count;
        }
    }
    return count;
}

longer­Syl­(word, len, syllable)

#include<iostream>
#include<string>

bool isVowel (char t)
{
	return (t =='a' || t =='e' || t =='i' || t =='o' || t =='u' || t =='y' ||
			t =='A' || t =='E' || t =='I' || t =='O' || t =='U' || t =='Y');
}
std::string findLongestSyl(std::string sentences){
	// sentences = "hoc mon nhap mon lap trinh rat vui"
	// return "inh"
	
	int i = 0, max_index, max_len = 0, current_index, current_len = 0;
	while (!isVowel(sentences[i])) i ++;
	max_index = i;
	// Assign max to first syllabus
	while(sentences[i] != ' ' && sentences[i] != 0)
	{
		i++; 
		max_len++;
	}
	while(sentences[i] != 0)
	{
		current_len = 0;
		while(!isVowel(sentences[i])) i++;
		current_index = i;
		while((sentences[i] != ' ' && sentences[i] != 0))
		{
			i++;
			current_len++;
		}
		if(current_len > max_len)
		{
			max_len = current_len;
			max_index = current_index;
		}
	}
	// Copy to string
	char sys[20] = {0};
	for(int j = 0; j < max_len; j++)
	{
		sys[j] = sentences[j + max_index];
	}
	return (std::string) sys;
}
int main()
{
	char sentences[] = "hooooooc mon nhap mon lap trinh rat vuaWQWVViqw";
	std::string output = findLongestSyl(sentences);
	std::cout << output;
}

vd cua convert char array to string

#include <iostream>
#include <string>
int main()
{
    char cstr[] = "clc03";
    std::string s(cstr);
    std::cout << s;
    return 0;
}

void Remove­Phone

#include <iostream>
struct Phone
{
int ID;
int version;
int year;
};

void RemovePhone(Phone listPhone[], int &nPhone, int curYear)
{
    int elementsDeleted = 0;
    for (int i = 0; i < nPhone; i++)
    {
        if (curYear - listPhone[i].year > 4)
        {
            for(int j = i; j < nPhone; j++)
            {
            listPhone[j] = listPhone[j + 1];
            }
            i--;
            nPhone--;           
        }
    }
}

int main()
{


    Phone listPhone[100] = {{10, 9, 2019}, {20, 10, 2020}, {21, 11, 2021}, {22, 12, 2022}, {9, 8, 2018}};
    int nPhone = 5;
    int curYear = 2024;
    RemovePhone(listPhone, nPhone, curYear);
    // In ra ID cua cac dien thoai con lai
    for (int i = 0; i < nPhone; i++)
    std::cout << listPhone[i].ID << " ";
    return 0;
}

findext lam dai dfskdh­fkjsfh

#include <iostream>
#include <string>
#include <cstring>

void FindExt( std::string paths[], int nPath, std::string extName, std::string results[], int &nRes)
{
    for (int i = 0; i < nPath; i++)
    {
        if(extName == paths[i])
        {
            for (int j = 0; j < nPath; j++)
            {
                results[j] = paths[i];
            }
            nRes++;
        }
    }
}
_______________
poe
#include <iostream>
#include <string>

void FindExt(std::string paths[], int nPath, std::string extName, std::string results[], int& nRes)
{
    for (int i = 0; i < nPath; i++)
    {
        if (paths[i].find(extName) != std::string::npos)
        {
            results[nRes] = paths[i];
            nRes++;
        }
    }
}

int main()
{
    std::string paths[] = { "D:/Others/us/b.pdf", "C:/Users/c.txt", "C:/Users/adm/a.txt", "E:/temp/d.exe" };
    int nPath = 4;
    std::string results[4];
    std::string extName = "txt";
    int nRes = 0;
    FindExt(paths, nPath, extName, results, nRes);
    for (int i = 0; i < nRes; i++)
    {
        std::cout << results[i] << " ";
    }
    return 0;
}

Hàm xoá các phần tử có giá trị x

#include <iostream>
void deleteItems (int a[], int &n, int x)
{
    for (int i = 0; i < n; i++)
    {
        if (a[i] == x)
        {
            for (int j = i; j < n; j++)
            {
                a[j] = a[j + 1];
            }
            i--;
            n--;
        }     
    }
}

void delete­Ite­msF­rom­Index

#include <iostream>
void deleteItemsFromIndex(int a[], int &n, int index, int no_items)
{
    for(int i = index ; i < index + no_items; ++i)
    {
        if (i + no_items < n)
        {
            a[i] = a[i + no_items];
        }
        else
        {
            break;
        }
    }
    n = n - std::min(no_items, std::max(n - index, 0));
}

struct sinhvien

#include <iostream>
#include <cstring>
#include <iomanip>
#pragma pack(1)

struct SINHVIEN
{
    char MSSV[9];
    char Ho_va_ten_lot[31];
    char Ten[9];
    float mon1 = 0;
    float mon2 = 0;
    float mon3 = 0;
};

void print(SINHVIEN sv[], int n)
{
    std::cout << "MSSV" << std::setw(10) << "HO" << std::setw(10) << "Ten" << std::setw(10) << "D1" << std::setw(10) << "D2" << std::setw(10) << "D3\n";
    for(int i = 0; i < n; i++)
        std::cout << sv[i].MSSV << std::setw(10) << sv[i].Ho_va_ten_lot << std::setw(10) << sv[i].Ten << std::setw(10) << sv[i].mon1 << std::setw(10) << sv[i].mon2 << std::setw(10) << sv[i].mon3 << "\n";
}

void sortByName(SINHVIEN sv[], int n)
{
    for(int i = 0; i < n; i++) {
        for(int j = n - 1; j > i; j--) {
            if(strcmp(sv[i].Ten, sv[j].Ten) > 0)
                std::swap(sv[i], sv[j]);
        }
    }
}

int main()
{
    SINHVIEN sv[]={{"23127489", "Nguyen Ngoc Minh", "Thu", 5.0, 9.0, 6.5}, {"23127237", "Le Truong Bao", "Ngoc", 7.0, 4.5, 8.7}, {"23127525", "Tran Ngoc", "Dieu", 9.7, 1.2, 5.6}};
    int n = sizeof(sv)/sizeof(SINHVIEN);
    std::cout << "Danh sách ban đầu: \n";
    print(sv, n);
    sortByName(sv, n);
    std::cout << "Danh sách sau khi sắp xếp theo tên: \n";
    print(sv, n);
    return 0;
}

tìm min của mảng 2d

#include <iostream>
int findMin(int a[][MAX], int m, int n)
{
    int min = a[0][0];
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if(a[i][j] < min)
            min = a[i][j];
        }
    }
    return min;
}

delete­Items

void deleteItems(int a[][MAX], int& m, int& n, int x)
{
    int elementsDeleted = 0;  // Variable to keep track of the number of elements deleted

    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (a[i][j] == x)
            {
                // Shift elements to the left starting from the next position
                for (int k = j; k < n - 1; k++)
                {
                    a[i][k] = a[i][k + 1];
                }
                elementsDeleted++;
                // Decrement the number of columns
                n--;
                // Decrement j to recheck the new element at the current position
                j--;
            }
        }
    }

    // Update the number of rows after deletion
    m -= elementsDeleted;
}

vd cua convert string to char


#include <iostream>
#include <cstring>
#include <string>
int main()
{
   std::string s = "clc3";
   char cstr[50];
   strcpy(cstr, s.c_str());
   std::cout << cstr;
   return 0;
}
   
       
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Numeric Formats Cheat Sheet
          C# CheatSheet Cheat Sheet
          C program Cheat Sheet