Example Array Problems

1D integer array examples.  You should be able to write these without looking at a solution.

  1. Write a function called int SumOfSq(int A[], int size) that returns the sum of the squares of the values in A.
  2. Write a function called int Max(int A[], int size) that returns the maximum value in A.
  3. Write a function called int Width(int A[], int size) that returns the difference between the maximum value in A and the minimum value in A.
  4. Write a function called  void Zero(int A[], int size) that zeros the array A
  5. Write a function called void Set(int A[], int size, int value) that sets every element of A to value.
  6. Write a function called void Reverse(int A[], int size) that reverses the order of the integers in A.  IE if A[]={1,4,3,6,5} then the function modifies A so that A={5,6,3,4,1}
  7. Write a function called void RotateR(int A[], int size) that shifts all the values in A to the right placing the int that falls off the rhs in A[0].    IE  if A={5,6,3,4,1} then RotateR will convert A to {1,5,6,3,4};
  8. Do the same as above but RotateL ( rotate left)
  9. Write a function called void PartialSum(int A[], int size) that replaces A with its partial-sum.  IE  A[i] is set to A[0]+A[1]+… + A[i]
  10. Write a function called  bool Natural(int A[] , int size) that returns true if all the values from 1,2,3…size occur in the array A and false otherwise.  IE if A={1,4,3,5,2,6,7,9,8} then Natural() should return true.
  11. Write a function called bool Magic(A[N][N]) that will return true if A is a magic square and false otherwise. You need to know the magic_constant for a square of size NxN.  A magic square is a square filled with the values 1 thru N^2 such that every row,col and diagonal sums to the magic_constant. (ie they are all the same sum)

Comments are closed.