CIS3355: Data Structures

The University of Texas at El Paso

Professor Kirs

 

Structured Data Objects: Sample Problems and Exercises

 

00010. What advantages does an array of data type struct have over a regular array?? Explain.

 

SEE SOLUTION

 

00020. It was stated that by declaring a structured data object, say:

 

             struct newobject { …} we are creating a new data type. Explain.

 

SEE SOLUTION

 

4.0010  How many bytes of contiguous storage would be required for the array mydata given the following structure template and declaration:

struct mystuff

      {    char classification, title[13];

            int number1, number2, numbers[42];

            float fnumb1, fnumb2[7];

            double doub1[3];

            struct mystuff * mypointer;  };

 

      int main()

      {    struct mystuff mydata[12];

 

      SEE SOLUTION

 

5.0004.   Given the following section of code:

 

struct studinfo { char studentname[30];

                             short   age, credithours;

                             long  weightindrams;

                             float  gpa; };

 

struct studinfo  student[6] = {{“Christie, Agatha”,, 96, 82, 37324, 3.05},

     {“Shakespeare, Bill”, 256,12, 52253, 3.56}, {“Albee, Edward”, 76, 120, 72689, 2.78},

     {“Voltaire”,  56, 82, 64766, 3.75}, {“Chaucer, Geoffry”, 420, 91, 41566, 3.25},

     {“Grisham, John”, 46, 24, 66456, 1.57}} ;

 

The command:    printf(“%lu\n”,&student[3].weightindrams);   yields the output:   7686

 

a.   What is the value of student[2].name?                                          

b.   What is the value of student[1].gpa?                                             

c.   What is the value of student[6].age?                                             

d.   What is the value of  &student[4]?                                                

e.   What the value of &student[0].credithours?                                 

f.   What the value of  &student[85].age

                     SEE SOLUTION TO ALL PARTS                            

 

5.0010.  Given the following sections of C code:

 

struct  emp

{  char lname[7], dept[5];

    short age, class;

    float salary;

};

 

void main()

{   struct emp employee[6];

     printf (“%lu\n”, &employee[2].class);   // The output would be 6420

 

a.  What would be printed out by the command:  printf(“%lu”, employee);

 

      SEE SOLUTION

 

b.  What would be printed out by the command:  printf(“%lu”, &employee[4].dept);

 

      SEE SOLUTION

 

      If the following code were part of the above program:

 

                int i;

                struct emp *rec = employee;

 

FORGET THE ANSWERS YOU OBTAINED FOR Parts a and b.

 

Suppose that after the above the statement printf(“%lu”, &employee[0]); 

yielded the output 7500.

 

Now, inside the program we find the code:

 

                for (i = 0; i < 3; i++)

                        rec++;

 

c.    What is the value of rec at the end of the loop?                    

 

      SEE SOLUTION

 

d.    At what address will variable rec be found?

 

      SEE SOLUTION

 

e.    How many bytes of contiguous storage are required to store the variable rec?

 

      SEE SOLUTION

 

f.    What is the value of variable i at the end of the loop

 

      SEE SOLUTION

 

5.0030.   Given:

           

struct datatemplate

            {   char group[3];

                 int groupclass;

                 struct datatemplate *next;  };

 

                 char main()

                 {   struct datatemplate data[3] =  {{“AB”,76,6741},{“X4”,322,6732},{“6T”,0,6750}};

 

      If we issue the statement:                                                                       printf(“%lu”, data)

      And we receive the output:                      6732

 

A.  Show how the data would be laid out in RAM (in Decimal or in Roman Characters)

 

      SEE SOLUTION

 

B   What would be printed by the statement:               printf(“%d”, (sizeof) data);

 

SEE SOLUTION

 

C. What would be printed by the statement:              printf(“%lu”, &data[1]);

 

      SEE SOLUTION

 

D. What would be printed by the statement:              printf(“%lu”, &data[2].next);

 

SEE SOLUTION

 

E.   What would be printed by the statement:              printf(“%d”,  data[1].groupclass);

 

      SEE SOLUTION

 

F.   What would be printed by the statement:              printf(“%s”,   data[2].group);

 

      SEE SOLUTION

 

G. What would be printed by the statement:              printf(“%d”,   data[0].next->groupclass);

 

      SEE SOLUTION

 

H. What would be printed by the statement:              printf(“%d”,   data[0].next->next->group);

 

      SEE SOLUTION