Hw 3 - Due 03/21-before 9:00a.m. Mark Complete Instructions: Create a project, p
ID: 3730961 • Letter: H
Question
Hw 3 - Due 03/21-before 9:00a.m. Mark Complete Instructions: Create a project, prjHw3, that uses a programmer defined library (tvro more extra-files: the header file: MyArrayLibrary.h, and the actual library file: MyArrayLibrary.cpp) containing functions for arrays vith elements of type char. The functions that you should implement are: readArray (int&,char [1) displayArray (int, char[]) deleteElement (int&, char, int); insertElement (int&, chart, int) int countoccurences (int, char, char)Explanation / Answer
void readArray(int &n, char data[]){
cout << "Enter number of elements(< MAX_ELEMENTS): ";
cin >> n;
while(n <= 0 || n > MAX_ELEMENTS){
cout << "Invalid value.Please enter again ";
cin >> n;
}
cout << "Enter " << n << " elements :";
for (int i = 0; i<n; i++){
cin >> data[i];
}
}
void displayArray (int n , char data[]){
for (int i = 0; i<n; i++)
cout << data[i];
cout << endl;
}
void deleteElement (int &n , char data[], int ind){
if (ind < 0 || ind > n-1){
cout << "Index out of range ";
return;
}
for (int i = ind+1; i<n; i++){
data[i-1] = data[i];
}
cout << "Element deleted ";
return;
}
void insertElement (int &n , char data[], int ind){
if (n == MAX_ELEMENTS){
cout << "Array is full ";
return;
}
if (ind < 0 || ind > n){
cout << "Index out of range ";
return;
}
cout << "Enter number to be inserted:";
int a;
cin >> a;
for (int i= n; i<= ind; i--){
data[i] = data[i-1];
}
data[ind] = a;
n++;
cout << "Element inserted ";
return;
}
int countOccurence (int n , char data[], char a){
int count = 0;
for (int i = 0; i<n; i++){
if (data[i] == a){
count++;
}
}
return count;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.