Function Basic C++
Function Basic C++
Function Basic C++
Objective(s):
Topic Arrays With Functions
Lab Description:
This lab is basically designed for the revision of C-String, Arrays, functions.
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:
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:
No. of Alphabets: 13
No. Of Digits: 3
Program:
#include <iostream>
if ((arr[i] >= 'a' && arr[i] <= 'z') || (arr[i] >= 'A' && arr[i] <= 'Z'))
++alphabets;
++spaces;
cout << "No. Of White Spaces: " << spaces << endl;
int main() {
char arr[MAX_SIZE];
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.
Program:
#include <iostream>
int main() {
const int SIZE = 5;
int source[SIZE];
int destination[SIZE];
cout << "Enter " << SIZE << " elements for the source array: ";
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:
Output:
A B D E G I K L M N O P R
Program:
#include <iostream>
int i = 0, j = 0, k = 0;
result[k++] = arr1[i++];
result[k++] = arr2[j++];
resultSize = k;
int main()
cout << "Enter " << size2 << " elements for the 2nd array: ";
result[i] = result[j];
result[j] = temp;
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>
int temp=0;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
int main()
int size;
int array[100];
cin>>size;
cin>> array[i];
cout<< array[i]<<endl;
cout<<endl;
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>
int temp=0;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
int descending_order(int arr [], int size)
int temp=0;
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;
cin>> array[i];
cout<<endl;
cout<<endl;
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>
destination[i] = source[i];
int main() {
int source[SIZE] ;
int destination[SIZE];
int output[SIZE];
cin>>source[i];
printArray(source, SIZE);
printArray(destination, SIZE);
printArray(output, SIZE);
return 0;
OUTPUT: