Homework 4 – Programming Languages

 

1. Some languages allow a heterogeneous array in which elements may be of different types.  For example,  the following creates an array containing an integer, floating point number and a string:

A := {1, 2.0, “bob”};

A[0] = 1

A[2] = “bob”

One can use such arrays as records, by simply indexing into the appropriate element instead of using field names.

            #define NAME 2

     A[NAME] = “bob”;  vs. A.NAME

Describe the implications of using this approach instead of record types as defined in C or Ada. What are the implications for type safety?  What about program readability and maintenance?

 

2.  You are designing a new language, which is to be used in your office by people with little or no training in programming, but efficiency is important. You must decide how your language will support arrays. For each question, please indicate the advantages or disadvantages of each choice and the rationale for your decision.

    a. Should arrays be dynamic or declared with a fixed size?

    b. Should arrays contain attributes such as their ranges?

    c. Should array bounds be checked at compile time or at run time?

   

 

3. What is(are) the problem(s) with the following C function? It takes two character strings and returns a two character array containing the first character in each string if that character is greater than ‘A’. It also replaces the first character in each of the original strings with a blank.

char * first(char * p, char * q){

   char first[2] = {‘x’,’x’);

  

   if (p[0] >= ‘A’) {

       first[0] = p[0];

   }

   p[0] = ‘ ‘;

   if (q[0] >= ‘A’) {

       first[1] = q[0];

    }

   q[0] = ‘ ‘;      

   return first;

}

Write a version of the function that corrects the problem(s).