0% found this document useful (0 votes)
17 views6 pages

Arry, String and Function in C++

Uploaded by

kamebmt555
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

Arry, String and Function in C++

Uploaded by

kamebmt555
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1. simple example of C++ array, where we are going to create, initializes and traverse array.

#include <iostream>

using namespace std;

int main()

int arr[5]={10, 0, 20, 0, 30}; //creating and initializing array

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

cout<<arr[i]<<"\n";

2. Example creating and accessing elements of array

#include <iostream>

#include <string>

using namespace std;

int main() {

string arry[4] = {"Volvo", "BMW", "Ford", "Mazda"};

cout << arry[0];

return 0;

Exercise 1. change elements arry[0]=’opel’ of the program above in example2

3 .Consider the following c++ program of array with for loop

#include <iostream>
include <string>
using namespace std;
int main()
{
int myarray[5] = {10, 20,30,40,50};
int sum = 0;
for(int i = 0;i<5;i++)
{
sum += myarray[i];
}
cout<<"Sum of elements in myarray:\n "<<sum;
4. Sample program that shows declaration, initialization and accessing of a two-dimensional array.
#include <iostream>
using namespace std;
int main() {
int myarray[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; //declaration and initialization of array
for(int i=0;i <3;i++)
{
for(int j=0;j<3;j++)
{
cout<<myarray[i][j]<<" ";
}
}
return 0;
}
5. Example of C++ array which prints minimum number in an array using function.
#include <iostream>
using namespace std;
void printMin(int arr[5]);
int main()
{
int arr1[5] = { 30, 10, 20, 40, 50 };
int arr2[5] = { 5, 15, 25, 35, 45 };
printMin(arr1);//passing array to function
printMin(arr2);
}
void printMin(int arr[5])
{
int min = arr[0];
for (int i = 0; i > 5; i++)
{
if (min > arr[i])
{
min = arr[i];
}
}
cout<< "Minimum element is: "<< min <<"\n";
}
Exercise write aC++ program which prints maximum number in an array using function

6.simple example of C++ string.


#include <iostream>
using namespace std;
int main( ) {
string s1 = "Hello";
char ch[] = { 'C', '+', '+'};
string s2 = string(ch);
cout<<s1<<endl;
cout<<s2<<endl;
}
7. simple example of string comparison using strcmp() function. .
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char key[] = "mango";
char buffer[50];
do {
cout<<"What is my favourite fruit? ";
cin>>buffer;
} while (strcmp (key,buffer) != 0);
cout<<"Answer is correct!!"<<endl;
return 0;
}
8. simple example of string concatenation using strcat() function.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char key[25], buffer[25];
cout << "Enter the key string: ";
cin.getline(key, 25);
cout << "Enter the buffer string: ";
cin.getline(buffer, 25);
strcat(key, buffer);
cout << "Key = " << key << endl;
cout << "Buffer = " << buffer<<endl;
return 0;
}
9. Simple c++ example of copy the string using strcpy() function .
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char key[25], buffer[25];
cout << "Enter the key string: ";
cin.getline(key, 25);
strcpy(buffer, key);
cout << "Key = "<< key << endl;
cout << "Buffer = "<< buffer<<endl;
return 0;
}
10. Simple example of finding the string length using strlen() function.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char ary[] = "Welcome to C++ Programming";
cout << "Length of String = " << strlen(ary)<<endl;
return 0;
}
///C++Function example
1 simple example1 of C++ function.
#include <iostream>
using namespace std;

void greet() {// declaring a function

cout << "Hello there!";


}

int main() {

greet(); // calling the function

return 0;
}
2 .Example 2: Function with Parameters to print a text
#include <iostream>
using namespace std;
void displayNum(int n1, float n2) {// display a number

cout << "The int number is " << n1;


cout << "The double number is " << n2;
}

int main() {

int num1 = 5;
double num2 = 5.5;
displayNum(num1, num2); // calling the function

return 0;
}

3 .Example 3: C++ Function Prototype


// use function definition after main() function
// function prototype is declared before main()

#include <iostream>

using namespace std;

int add(int, int); // function prototype


int main() {
int sum;
sum = add(100, 78); // calling the function and storing

cout << "100 + 78 = " << sum << endl;

return 0;
}
int add(int a, int b) {// function definition

return (a + b);
}

4.Eample 4 :c++ program to Overload c++ function Using Different Types of Parameter
#include <iostream>
using namespace std;
float absolute(float var){ // function with float type parameter
if (var < 0.0)
var = -var;
return var;
}
int absolute(int var) {// function with int type parameter
if (var < 0)
var = -var;
return var;
}

int main() {

cout << "Absolute value of -5 = " << absolute(-5) << endl;


cout << "Absolute value of 5.5 = " << absolute(5.5f) << endl;
return 0;
}
5. Example 5 :C++ program to overload c++ function Using Different number of Parameter
#include <iostream>
using namespace std;
void display(int var1, double var2) {
cout << "Integer number: " << var1;
cout << " and double number: " << var2 << endl;
}

// function with double type single parameter


void display(double var) {
cout << "Double number: " << var << endl;
}

// function with int type single parameter


void display(int var) {
cout << "Integer number: " << var << endl;
}

int main() {

int a = 5;
double b = 5.5;

display(a); // call function with int type parameter

display(b); // call function with double type parameter

display(a, b);//call a function 2 parameter

return 0;
}

You might also like