Exercises using pointers

Note: In many of the problems below you could use either an array or a pointer; you should try to do the exercises in both ways.

1. In the following program, complete the function definitions.

#include<iostream>
using namespace std;

double total_salary(double a[], double b[], int );
double total_salary2(double*, double*,int);

int main () {
  int n=4;
  double salary;
  double a[4]={10, 7,8, 12};
  double b[]={12,9,11,22};
  salary=total_salary(a,b,n); //returns a[0]*b[0]*a[1]*b[1]+...+a[3]*b[3]
  cout <<"Salary-> "<<salary<<endl;
  salary=total_salary2(a,b,n); //returns a[0]*b[0]*a[1]*b[1]+...+a[3]*b[3]
  cout <<"Salary-> "<<salary<<endl;
}

double total_salary(double a[],double b[], int n){
//FILL IN
}

double total_salary2(double *a, double *b,int n){
//FILL IN
}
\end{verbatim
2. What's wrong in the following program? Correct it.
\begin{verbatim}
#include<iostream>
using namespace std;


char* fun(char*, int);

int main(){
  char* MyChar;
  char A[]={'h', 'e','l','l','o'};
  MyChar=fun(A,5);
  for (int i=0;i<5;i++){
    cout<<MyChar[i]<<" ";
  }
  cout<<endl;
}

char* fun(char* a, int n){
  char b[n];
  for (int i=0;i<n;i++){
    a[i]=toupper(a[i]);
  }
  return b;
}
3. Write a function, isAlpha, which returns true if all characters in the parameter are alphabetic (ie, if there are no non-alphabetic characters), and false otherwise. You can use the library isalpha function to test the individual characters. If there are no characters in the argument, this function should succeed. Assume the prototype is

bool isAlpha(char a[]);

4. Write a function, reverse, which reverses the characters in the argument. Assume the prototype is

void reverse(char a[]);

5. Write a function, count, which counts the occurrences in the first argument, a, of the single character which is the second argument, c. Assume the prototype is

int count(char a[], char c);

For example,

i = count("abracadabra", 'a');

would return 5.