Puzzles G01 ... G10

Part G — Struct Problems

A struct is a bundle of variables that has a name. The variables of a struct are called its members. For example, the following struct has the name bulb and contains two members, watts and lumens. Think of the struct as representing a light bulb that has a rating of watts and lumens.

struct
{
  int watts;
  int lumens;
} bulb;

The declaration allocates memory for the struct and provides for a way to access its members using dot notation: bulb.watts and bulb.lumens. The syntax of struct declaration is this:

struct tag { member-list } variable-list;

The tag is the name of the type of struct being declared. The tag is optional if there is a variable-list. The member-list is a list of items that make up this struct. The variable-list is a list of identifiers. Each identifier is the name of a variable of this type of struct. The variable-list is optional if there is a tag. Here is a declaration of three variables:

struct
{
  int watts;
  int lumens;
} bulb20, bulb60, bulb100;

Here is a declaration of a struct with a tag, but no variables.

struct BulbType
{
  int watts;
  int lumens;
};

Variables now can be declared elsewhere in the program by using the struct's tag:

struct BulbType bulb20, bulb60, bulb100;

Puzzle G01 — Dot Notation

Here is a declaration of a variable bulbA and a main() that uses it:

#include <stdio.h>

struct
{
  int watts;
  int lumens;
} bulbA;

int main(int argc, char *argv[])
{
  /* set values for bulbA */

  /* print values of bulbA */
  
  system("PAUSE");	
  return 0;
} 

Edit the example so that bulbA gets 100 watts and 1710 lumens.

Note to Java programmers: The above declaration allocates memory for bulbA. You don't need to use new as you might expect in Java programming.


Puzzle G02 — Struct Copy

Add a second identifier, bulbB, to the identifier list of the previous program. Now there are two structs that follow the memory layout of the declaration. Initialize bulbA as before, and then copy the data in bulbA to bulbB.

#include <stdio.h>

struct
{
  int watts;
  int lumens;
}  bulbA, ???? ;

int main(int argc, char *argv[])
{
  /* set values for bulbA */

  /* copy values from bulbA to bulbB */

  /* print values of bulbB */
  
  system("PAUSE");	
  return 0;
} 


Puzzle G03 — Call by Value

Use the tag part of a struct declaration to declare a type Bulb for light bulbs. Write a function that prints out the data in a Bulb. Declare and initialize two Bulbs in main and print out their data.

Write printBulb() so that it gets a complete copy of the Bulb it is to print.

#include <stdio.h>

/* declare the type struct Bulb (Notice the tag) */

struct Bulb
{
  int watts;
  int lumens;
};

/* function to print a Bulb */
void printBulb( struct Bulb b )
{
}

int main(int argc, char *argv[])
{
  /* declare and initialize two Bulbs */

  /* print values of both Bulbs */
  
  system("PAUSE");
  return 0;
} 


Puzzle G04 — Call by Vaue (using address)

Modify the answer to Puzzle 3 so that the parameter to the printBulb() function is the address of the Bulb it is to print.

#include <stdio.h>

/* declaration of type Bulb */
struct Bulb
{
  int watts;
  int lumens;
};

/* function to print a Bulb */
void printBulb( struct Bulb *b )
{
}

int main(int argc, char *argv[])
{
  /* declare and initialize two Bulbs */

  /* print values of both Bulbs */
  printBulb( &bulbA );
  printBulb( &bulbB );

  system("PAUSE");	
  return 0;
} 

Hint: If you have a pointer to a struct, say ptr then the members of the struct can be accessed by (*prt).member where (*prt) follows (dereferences) the pointer to the struct, and then the usual dot notation, .member accesses the member. This combination is so common that there is a shorthand notation for it:

ptr->member       is the same as       (*ptr).member

For example, if blb is a pointer to a struct Bulb, then b->watts and b->lumens accesses its members.


Puzzle G05 — Array Declaration

Write a main() that declares an array of 10 Bulb structs and initializes the first several to interesting values. Initialize the members of the remaining bulbs to zero. Print the array to show that your initializations have worked.

#include <stdio.h>

struct Bulb
{
  int watts;
  int lumens;
};

/* function to print a Bulb */
void printBulb( struct Bulb b )
{
}

int main(int argc, char *argv[])
{
  /* declare an array of 10 Bulbs */

  /* zero all Bulbs */

  /* initialize several Bulbs */

  /* print values of Bulbs */
  
  system("PAUSE");	
  return 0;
} 


Puzzle G06 — Function to print an array

Write a function printArray() that prints an entire array of Bulb structs.

#include <stdio.h>

struct Bulb
{
  int watts;
  int lumens;
};

#define length 10

/* function to print the array */
void printArray( ????, ??? )
{
}

int main(int argc, char *argv[])
{
  /* declare and initialize an array of length Bulbs */

  /* print the array */
  
  system("pause");	
  return 0;
} 

Use an initializer list to initialize the array. Decide on the parameters for printArray(). Hint: remember that the name of an array stands for the address of the array. It is likely that the function you write will have the array address as one of its parameters.


Puzzle G07 — Function to change a struct

As bulbs age, their light output decreases. Write a function that changes the number of lumens of a Bulb by decreasing it by ten percent.

#include <stdio.h>

struct Bulb
{
  int watts;
  int lumens;
};

/* function to print a Bulb */
void printBulb( struct Bulb* b )
{
}

void dimBulb( struct Bulb* b )
{
}

int main(int argc, char *argv[])
{
  /* declare and initialize a Bulb */

  /* print the Bulb */

  /* decrease its light output */

  /* print the Bulb */
  
  system("pause");	
  return 0;
} 

Here there is no choice. The parameter of the dimBulb() function must be the address of the struct.


Puzzle G08 — Apply a function to each element of an array

Write a main() that declares and initializes an array of Bulbs. Apply the lumen diminishing function (puzzle 07) to each Bulb of the array. Print out the array.

#include <stdio.h>

struct Bulb
{
  int watts;
  int lumens;
};

/* function to print a Bulb */
void printBulb()
{
}

void dimBulb( struct Bulb* b )
{
}

int main(int argc, char *argv[])
{
  /* declare and initialize a Bulb array */
  #define length 5
  struct Bulb lights[length] = { {100,1710}, {60,1065}, {100,1530}, {40,505}, {75,830} };

  /* print the Bulbs */

  /* decrease light output of each Bulb */

  /* print the Bulbs */
  
  system("pause");	
  return 0;
} 


Puzzle G09 — Allocate Memory for a struct

Write a main() that has a variable that can point to struct Bulb. Allocate memory for a struct, assign the address to the variable, initialize and print the struct.

Use void *malloc(size_t size) for allocating memory.

Use void free(void *address) for deallocating memory.

#include <stdio.h>

struct Bulb
{
  int watts;
  int lumens;
};

/* function to print a Bulb */
void printBulb()
{
}

int main(int argc, char *argv[])
{
  /* declare and initialize a Bulb pointer */
  struct Bulb *bptr;

  /* allocate memory for the Bulb */

  /* print the Bulb */

  /* deallocate memory for the Bulb */
  
  system("pause");	
  return 0;
} 


Puzzle G10 — Create an array of struct pointers

Create an array of pointers to struct Bulb. Allocate memory for some of the array elements and initialize them. Print out the array. Deallocate memory.

#include <stdio.h>

struct Bulb
{
  int watts;
  int lumens;
};

/* function to print a Bulb */
void printBulb()
{
}

int main(int argc, char *argv[])
{
  /* declare and initialize an array of Bulb pointers */

  /* allocate memory for selected elements of the array */

  /* print the array */

  /* deallocate memory for the Bulb */
  
  system("pause");	
  return 0;
} 


— Return to the main contents page