finish up the code: main() readArray (int&, char[]); displayArray(int, char[]);
ID: 3735124 • Letter: F
Question
finish up the code:
main()
readArray (int&, char[]);
displayArray(int, char[]);
deleteElement(int&, char[], int);
insertElement(int&, char[], char, int);
int countOccurences(int, char[], char);
Here it is the started code: https://codeboard.io/projects/77845
2. Add the following function to the project: bool findElement(int, char[], char);
where the first 2 parameters represent the size of the array and the array of char-s, and the 3rd parameter represents the letter that is to be searched in the array. If found the function will return 'true' otherwise it will return 'false'.
Explanation / Answer
#include <iostream>
//#include "MyArrayHeader.h"
using namespace std;
/*
Input: an array of chars and other data needed for
op-s with array: position
Output: results of these op-s
*/
void readArray (int n, char arr[])
{
cout << " Number of elem:";
cin >> n;
cout << "Input all elements:";
for (int i =0; i < n; i++)
cin >> arr[i];
}
void displayArray(int n, char arr[])
{
for (int i =0; i < n ; i++)
cout << arr[i] << " ";
cout << endl;
}
void deleteElement(int n, char arr[], int p)
{
int writer = 0, reader = 0;
char b[n-1];
while (arr[reader])
{
if (reader!=p)
{
b[writer++] = arr[reader];
}
reader++;
}
//b[writer]=0;
}
void insertElement(int n, char arr[], char ch, int p)
{
cout << " Element to insert: " << ch << endl;
cout << " Position to insert: " << p << endl;
for (int i=n-1; i >= p-1; i--)
arr[i+1] = arr[i];
arr[p -1] = ch;
n++;
}
int countOccurences(int n, char arr[], char p)
{
// char ch;
int j=0;
cout << "enter the element to get its occurence" << p <<endl;
for(int i=0;i<n;i++)
{
if(arr[i]=='p')
{
j++;
}
}
return j;
}
bool findElement(int n, char arr[], char p)
{
// char ch;
bool flag=false;
cout << "enter the element to be searched" << p <<endl;
for(int i=0;i<n;i++)
{
if(arr[i]=='p')
{
flag=true;
}
}
return flag;
}
int main()
{
int pos, n;
char p;
char initials[20],elIns;
readArray(n,initials);
cout << " The initial array is:" << endl;
displayArray(n, initials);
cout << "Insert element on place. Give initial and position: ";
cin >> elIns;
cin >> pos;
insertElement(n, initials, elIns, pos);
cout << " The array after insertion is:" << endl;
displayArray(n, initials);
cout << "Give position to delete: ";
cin >> pos;
deleteElement(n, initials, pos);
cout << " The array after deletion is:" << endl;
displayArray(n, initials);
cout << "Give char to get its occurence: ";
cin >> p;
int col1;
col1=countOccurences(n, initials, p);
cout << col1<<endl;
cout << "Give char to check: ";
cin >> p;
bool col2;
col2=findElement(n,initials,p);
if(col2==true)
{
cout<<"True"<<endl;
}
else
{
cout<<"False"<<endl;
}
// call the last fc: countOccurences(...)
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.