0% found this document useful (0 votes)
2 views5 pages

CPP Important Code Examples

The document provides important C++ code examples for exam preparation, covering various concepts such as void functions, value returning functions, pass by value and reference, function overloading, arrays, loops, sorting algorithms, multidimensional arrays, and static variables. Each example includes code snippets demonstrating the functionality of the concepts. These examples serve as practical references for understanding C++ programming fundamentals.

Uploaded by

raihan74800
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)
2 views5 pages

CPP Important Code Examples

The document provides important C++ code examples for exam preparation, covering various concepts such as void functions, value returning functions, pass by value and reference, function overloading, arrays, loops, sorting algorithms, multidimensional arrays, and static variables. Each example includes code snippets demonstrating the functionality of the concepts. These examples serve as practical references for understanding C++ programming fundamentals.

Uploaded by

raihan74800
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

Important C++ Code Examples for Exam Preparation

1. Void Function
#include <iostream>
using namespace std;

void greet()
{
cout << "Hello World";
}

int main()
{
greet();
return 0;
}

2. Value Returning Function


#include <iostream>
using namespace std;

int add(int a, int b)


{
return a + b;
}

int main()
{
int result = add(4,5);
cout << result;
}

3. Pass by Value
#include <iostream>
using namespace std;

void change(int x)
{
x = 10;
}

int main()
{
int a = 5;
change(a);
cout << a;
}

4. Pass by Reference
#include <iostream>
using namespace std;

void change(int &x)


{
x = 10;
}

int main()
{
int a = 5;
change(a);
cout << a;
}

5. Function Overloading
#include <iostream>
using namespace std;

int add(int a, int b)


{
return a + b;
}

double add(double a, double b)


{
return a + b;
}

int main()
{
cout << add(3,4) << endl;
cout << add(2.5,3.1);
}

6. Array Example
#include <iostream>
using namespace std;

int main()
{
int numbers[5] = {10,20,30,40,50};

cout << numbers[0] << endl;


cout << numbers[3];
}

7. Loop with Array


#include <iostream>
using namespace std;

int main()
{
int arr[5] = {1,2,3,4,5};

for(int i=0; i<5; i++)


{
cout << arr[i] << endl;
}
}

8. Linear Search
#include <iostream>
using namespace std;

int main()
{
int arr[5] = {2,4,6,8,10};
int value = 6;

for(int i=0;i<5;i++)
{
if(arr[i] == value)
{
cout << "Found";
}
}
}

9. Bubble Sort
#include <iostream>
using namespace std;

int main()
{
int arr[5] = {5,3,4,1,2};

for(int i=0;i<5;i++)
{
for(int j=0;j<4;j++)
{
if(arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}

for(int i=0;i<5;i++)
{
cout << arr[i] << " ";
}
}

10. Selection Sort


#include <iostream>
using namespace std;

int main()
{
int arr[5] = {5,2,4,1,3};

for(int i=0;i<4;i++)
{
int min = i;

for(int j=i+1;j<5;j++)
{
if(arr[j] < arr[min])
{
min = j;
}
}

int temp = arr[i];


arr[i] = arr[min];
arr[min] = temp;
}

for(int i=0;i<5;i++)
{
cout << arr[i] << " ";
}
}

11. Multidimensional Array


#include <iostream>
using namespace std;

int main()
{
int matrix[2][3] =
{
{1,2,3},
{4,5,6}
};

cout << matrix[1][2];


}

12. While Loop


#include <iostream>
using namespace std;

int main()
{
int i = 1;

while(i <= 5)
{
cout << i << endl;
i++;
}
}

13. Do-While Loop


#include <iostream>
using namespace std;

int main()
{
int i = 1;

do
{
cout << i << endl;
i++;
}
while(i <= 5);
}

14. Nested Loop


#include <iostream>
using namespace std;

int main()
{
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
cout << "* ";
}
cout << endl;
}
}

15. Static Variable


#include <iostream>
using namespace std;

void counter()
{
static int count = 0;
count++;
cout << count << endl;
}

int main()
{
counter();
counter();
counter();
}

You might also like