Function Basic C++

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

Bahria University, Lahore Campus

Department of Computer Sciences


Lab Journal 03
(Spring 2024)

Course: Object Oriented Programming Lab Date: 28/02/2024


Course Code: CSC-210 Max Marks: 30
Faculty’s Name: Rizwan Khalid

Name: MUHAMMAD AHSAN Enroll No:

Objective(s):
Topic Arrays With Functions

 Arrays with Functions.


o Passing array into a function.
o Importance of array size in functions.
o Difference between size and occupy.
o Passing C-String into a function.
o Discuss why size is not required when we pass C-String to a function.
Objective o Problem solving involving function and array/C-String.

Lab Description:

This lab is basically designed for the revision of C-String, Arrays, functions.

Passing array into a function:

We can pass an array into a function. As we discussed earlier array name represent the base address of
an array. So we can pass an array by its name along with its size. As we discussed earlier size and occupy
are two different things. Size represent the capacity of array and occupy represent the usage of available
resources. You must use occupy instead of size for generic use.
Prototype:

Calling:

Definition:

Passing C-String into a function:

As we discussed above we can pass a c-string using same method as an array. But the difference is
passing size is not required in c-string because of null termination.

Prototype:

Calling:

Definition:
Task 1:

Write a C++ Program that inputs a character array form the user and you have to pass that
array to a function named find_digits_alphabets_Spaces(char arr[]); that Count Digits,
Alphabets and Spaces

Sample Output:

Enter array of characters = I live 456 in Lahore

No. of Alphabets: 13

No. Of Digits: 3

No. Of White Spaces: 4

Program:

#include <iostream>

using namespace std;

void find_digits_alphabets_Spaces(char arr[]) {

int alphabets = 0, digits = 0, spaces = 0;

for (int i = 0; arr[i] != '\0'; ++i) {

if ((arr[i] >= 'a' && arr[i] <= 'z') || (arr[i] >= 'A' && arr[i] <= 'Z'))

++alphabets;

else if (arr[i] >= '0' && arr[i] <= '9')


++digits;

else if (arr[i] == ' ')

++spaces;

cout << "No. of Alphabets: " << alphabets << endl;

cout << "No. Of Digits: " << digits << endl;

cout << "No. Of White Spaces: " << spaces << endl;

int main() {

const int MAX_SIZE = 100;

char arr[MAX_SIZE];

cout << "Enter array of characters: ";

cin.getline(arr, MAX_SIZE);

find_digits_alphabets_Spaces(arr);

return 0;

OUTPUT:
Task 2:

Write a function based C++ program that takes two array called source and destination array.
It copies source array into destination array in reverse order.

Sample Input in source Array: 1 2 3 4 5

Sample Output in destination Array: 5 4 3 2 1

Program:
#include <iostream>

using namespace std;

void copyReverse(int source[], int destination[], int size) {

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

destination[i] = source[size - i - 1];

int main() {
const int SIZE = 5;

int source[SIZE];

int destination[SIZE];

cout << "Enter " << SIZE << " elements for the source array: ";

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

cin >> source[i];

copyReverse(source, destination, SIZE);

cout << "Source Array: ";

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

cout << source[i] << " ";

cout << endl;

cout << "Destination Array: ";

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

cout << destination[i] << " ";

cout << endl;

return 0;
}

OUTPUT:

Task 3:

Write a function based C++ program in which two character arrays are accepted from the
user. Your program then merges the two arrays in a third array but the entries must be done in
alphabetical order.

Sample Output:

Enter 1st array:


D R P I E A N

Enter 2nd array:


B G K Q M O L

Output:
A B D E G I K L M N O P R

Program:
#include <iostream>

using namespace std;


void mergeArrays(char arr1[], int size1, char arr2[], int size2, char result[], int &resultSize)

int i = 0, j = 0, k = 0;

while (i < size1)

result[k++] = arr1[i++];

while (j < size2)

result[k++] = arr2[j++];

resultSize = k;

int main()

const int MAX_SIZE = 100;

char arr1[MAX_SIZE], arr2[MAX_SIZE], result[MAX_SIZE * 2];

int size1, size2, resultSize;

cout << "Enter size of 1st array: ";

cin >> size1;


cout << "Enter " << size1 << " elements for the 1st array: ";

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

cin >> arr1[i];

cout << "Enter size of 2nd array: ";

cin >> size2;

cout << "Enter " << size2 << " elements for the 2nd array: ";

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

cin >> arr2[i];

mergeArrays(arr1, size1, arr2, size2, result, resultSize);

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

for (int j = i + 1; j < resultSize; ++j)

if (result[i] > result[j])


{

char temp = result[i];

result[i] = result[j];

result[j] = temp;

cout << "Output: ";

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

cout << result[i] << " ";

cout << endl;

return 0;

}
OUTPUT:

Task 4:
Write a C++ program to implement the Bubble Sort algorithm using a functions
Bubble_sort(int arr []);. The program should accept an array of integers, sort it using the
Bubble Sort algorithm, and then print the sorted array. Utilize a function to perform the sorting
process.
Sample Output:

Program:
#include <iostream>

using namespace std;

int Bubble_sort(int arr [], int size)

int temp=0;

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

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

if (arr[i] > arr[j])

temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}
int main()

int size;

int array[100];

cout<<"Enter the size of Array :>";

cin>>size;

cout<<"Enter the Elements :>"<<endl;

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

cin>> array[i];

cout<<"Array before sorting :>"<<endl;;

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

cout<< array[i]<<endl;

cout<<endl;

cout<<"Array after sorting :>"<<endl;

Bubble_sort( array , size);

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

cout<< array[i]<<endl;
}

cout<<endl;

return 0;

Task -5
Write a program in C++ to read an array containing n elements and sort this array in
ascending order and descending order using user defined function and display sorted array
from main function.
Sample Output:

Program:
#include <iostream>

using namespace std;

int ascending_order(int arr [], int size)

int temp=0;

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

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

if (arr[i] > arr[j])

temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}
int descending_order(int arr [], int size)

int temp=0;

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

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

if (arr[i] < arr[j])

temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

int main()

int size;

int array[100];
cout<<"Enter the size of Array :>";

cin>>size;

cout<<"Enter the Elements :>"<<endl;

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

cin>> array[i];

cout<<"Array before sorting :>"<<endl;;

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

cout<< array[i]<<" ";

cout<<endl;

cout<<"Array Acending order :>"<<endl;

ascending_order( array , size);

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

cout<< array[i]<<" ";

cout<<endl;

cout<<"Array Descending order :>"<<endl;

descending_order( array , size);


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

cout<< array[i]<<" ";

cout<<endl;

return 0;

OUTPUT:

Task 6:

Write a function based C++ program that takes two arrays called source and destination
array. It copies source array into destination array and then store the output of destination
array into a third array. You can use name of the function as you wish but keep in mind that
you have to pass those two arrays to some function first then you have to copy that arrays to
each other.
Sample Input in source Array: 1 2 3 4 5
Sample Output in text File: 1 2 3 4 5

Program:
#include <iostream>

using namespace std;

void copyArrays(int source[], int destination[], int size) {

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

destination[i] = source[i];

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

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

std::cout << arr[i] << " ";

cout << endl;

int main() {

const int SIZE = 5;

int source[SIZE] ;

int destination[SIZE];
int output[SIZE];

cout<<"Enter Five numbers :>";

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

cin>>source[i];

copyArrays(source, destination, SIZE);

copyArrays(destination, output, SIZE);

cout << "Source Array: ";

printArray(source, SIZE);

cout << "Destination Array: ";

printArray(destination, SIZE);

cout << "Output Array: ";

printArray(output, SIZE);

return 0;

OUTPUT:

You might also like