Arrays and Structures

Additional Exercises


  1. Write a program to read N integers into each of two arrays X and Y of size 20. The program will prompt the user to enter the values into each of the arrays. The program will then compare each of the elements of X to the corresponding element in Y. Then, in the corresponding element of a third array Z, store:-

    1
    if X is larger than Y
    0
    if X is equal to Y
    -1
    if X is less than Y

    Then print out a three column table displaying the contents of the arrays X, Y and Z, followed by a count of the number of elements of X that exceed Y, and a count of the number of elements of X that are less than Y. Make up your own test data.

    Declare your arrays to be of a size greater than you will require, (larger than N).

  2. Define a structure to store the x and y coordinates of a point. The user should then be prompted to enter the x and y coordinates for two points, and the program should make use of the structure. The program will then calculate the distance on the x axis between the two points and the distance on the y axis between the two points.


Back to Main Page for Lecture 5