0% found this document useful (0 votes)
27 views

Programming Fundamentals Assignment: 1. Program For Array Comparison

The document contains 4 C++ programs: 1. A program to compare two character arrays and check if they are equal. It takes in two arrays as input, checks if they are the same size, and then compares the elements to see if they are equal. 2. A program implementing selection sort and bubble sort on an integer array to sort it in ascending order. 3. A program to search for a given integer in an array and check if it is found. 4. A program to find the maximum value in a given integer array.

Uploaded by

MuhammadShafique
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Programming Fundamentals Assignment: 1. Program For Array Comparison

The document contains 4 C++ programs: 1. A program to compare two character arrays and check if they are equal. It takes in two arrays as input, checks if they are the same size, and then compares the elements to see if they are equal. 2. A program implementing selection sort and bubble sort on an integer array to sort it in ascending order. 3. A program to search for a given integer in an array and check if it is found. 4. A program to find the maximum value in a given integer array.

Uploaded by

MuhammadShafique
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1

Programming Fundamentals Assignment


Wasif Shah(MCS-I)
15-01-2016
__________________________________________________________________________________

1. Program for Array Comparison


#include <iostream>
using namespace std;
int SizeofArray(char arr1[]);
bool CompareArray(char arr1[],char arr2[]);
int main(){
char arr1 [10], arr2 [10];
cin>> arr1;
cin>> arr2;
if(SizeofArray(arr1)==SizeofArray(arr2)){
bool flag = CompareArray(arr1, arr2);
if(flag==0)
cout<<"Array Not Equal.";
else
cout<<"Array Equal.";
}else{
cout<<"Array Not Equal.";
}
cout << "\n";
system("pause");
}
int SizeofArray(char arr1 []){
int i=0;
while(arr1 [i]!='\0'){
i++;
}
return i;
}
bool CompareArray(char arr1 [],char arr2 []){
int i=0;
bool flag=1;
while(arr1 [i]!='\0'){
if(arr1 [i]!= arr2 [i])
flag = 0;
i++;
}
return flag;
}

2. Selection Sort, Bubble Sort (Ascending Order)


#include <iostream>
using namespace std;
void SelectionSorting(int arr1 []);
void BubbleSorting(int arr1 []);
int main(){
int arr1 [10];
for (int i = 0; i < 10; i ++)
{
cin >> arr1 [i];
}
SelectionSorting(arr1);
BubbleSorting(arr1);
for (int i = 0; i < 10; i ++)
{
cout << arr1 [i] << " ";
}
cout << "\n";
system("pause");
}
void SelectionSorting(int arr1 [])
{
int i, j, first, temp;
for (i= 9; i > 0; i--)
{
first = 0;
for (j=1; j<=i; j++)
{
if (arr1 [j] > arr1 [first])
first = j;
}
temp = arr1 [first];
arr1 [first] = arr1 [i];
arr1 [i] = temp;
}
return;
}

void BubbleSorting(int arr1 [])


{
int i, j, flag = 1;
int temp;
for(i = 1; (i <= 10) && flag; i++)
{
flag = 0;
for (j=0; j < (9); j++)
{
if (arr1 [j+1] < arr1 [j])
{
temp = arr1 [j];
arr1 [j] = arr1 [j+1];
arr1[j+1] = temp;
flag = 1;
}
}
}
return;
}

3. Search Integer in Array


#include <iostream>
using namespace std;
bool SearchinArray(int arr1 [],int);
int main(){
int arr1[10],b;
cout << "Enter array of 10 Integer : "<<endl;
for (int i = 0; i < 10; i ++)
{
cin >> arr1[i];
}
cout << "Enter an Inter to find in array : "<<endl;
cin>>b;
bool flag = SearchinArray(arr1,b);
if(flag==0)
cout<<"Not Found";
else
cout<<"Found in array";
cout << "\n";
system("pause");
}

bool SearchinArray(int arr1[],int b){


int i=0;
bool flag=0;
for (int i; i < 10; i ++){
if(arr1 [i]==b)
flag = 1;
i++;
}
return flag;
}

4. Maximum Integer in Array


#include <iostream>
using namespace std;
int MaxValue(int arr1 []);
int main(){
int arr1 [10],b;
cout << "Enter array of 10 Integer : "<<endl;
for (int i = 0; i < 10; i ++)
{
cin >> arr1 [i];
}
cout << "\n";
int max = MaxValue(arr1);
cout << "Maximum Value in array is : "<<max<<endl;
cout << "\n";
system("pause");
}
int MaxValue(int arr1 []){
int i=0;
int max = arr1 [i];
for (int i; i < 10; i ++){
if(arr1 [i]>max)
max = arr1 [i];
i++;
}
return max;
}

You might also like