Docsity

C Structures Worksheet, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
C Structures Worksheet
Do these problems in sequence as each builds on the previous.
1. Declare a structure called a Wombat that contains a float f, a
long integer L, and a pointer to another Wombat called Next. It
should also contain a 20 character array called Name.
2. Declare a Wombat called Ralph. Declare an array of Wombats
called Munroe with 10 members.
3. For problem 2, write the code to set Ralph’s float to 13.7 and
his name field to “The Doctor”.
4. Declare a ten element linked list of Wombats. Make sure that
you link them together.
5. Declare a pointer to a Wombat. Write the code to set it to
point to the 0th Munroe of problem 2, and set the f field to
17.1. Also, set the name to “Burgermeister”.
6. Write function to print out all of the Name fields for the
list of problem 4.
ET283 Worksheet: C Structures 1
pf3

Partial preview of the text

Download C Structures Worksheet and more Study notes Data Structures and Algorithms in PDF only on Docsity!

C Structures Worksheet

Do these problems in sequence as each builds on the previous.

  1. Declare a structure called a Wombat that contains a float f, a long integer L, and a pointer to another Wombat called Next. It should also contain a 20 character array called Name.
  2. Declare a Wombat called Ralph. Declare an array of Wombats called Munroe with 10 members.
  3. For problem 2, write the code to set Ralph’s float to 13.7 and his name field to “The Doctor”.
  4. Declare a ten element linked list of Wombats. Make sure that you link them together.
  5. Declare a pointer to a Wombat. Write the code to set it to point to the 0th Munroe of problem 2, and set the f field to 17.1. Also, set the name to “Burgermeister”.
  6. Write function to print out all of the Name fields for the list of problem 4.

C Structures Worksheet Answers

  1. Declare a structure called a Wombat that contains a float f, a long integer L, and a pointer to another Wombat called Next. It should also contain a 20 character array called Name. struct Wombat { float f; long int L; struct Wombat *Next; char Name[20]; };
  2. Declare a Wombat called Ralph. Declare an array of Wombats called Munroe with 10 members. struct Wombat Ralph; struct Wombat Munroe[10];
  3. For problem 2, write the code to set Ralph’s float to 13.7 and his name field to “The Doctor”. Ralph.f = 13.7; strcpy( Ralph.name, “The Doctor” );
  4. Declare a ten element linked list of Wombats. Make sure that you link them together. /* All data fields are filled with 0 except for Name and Next / struct Wombat A = {0,0,0,“Khalid”}; / last one in list / struct Wombat B = {0,0,&A,“Joey”}; struct Wombat C = {0,0,&B,“Frank”}; / continue in this manner until: / struct Wombat J = {0,0,&I,“Gail”}; / first one in list */
  5. Declare a pointer to a Wombat. Write the code to set it to point to the 0th Munroe of problem 2, and set the f field to 17.1. Also, set the name to “Burgermeister”. struct Wombat wp; wp = &Munroe[0]; / wp = Munroe also works as this is the 0th */ wp->f = 17.1; strcpy( wp->Name, “Burgermeister” );