Visual C# Guide
Arrays Of Records

You may have noticed that, in multi-dimensional arrays, the elements of each dimension must be the same data type. Bear in mind that we have already come across a way of storing more than type of information with a single identifier. Reread the section on Structures to remind yourself of the basics.

The following program creates an array of structures to store the details of products for sale.

static TProduct[] products = new TProduct[4];
public struct TProduct
   public string Description;
   public double Price;
static void Main(string[] args)
   for (int i = 0;i<products.Length;i++)
      Console.Write("Enter the description for product {0}: ", i);
      products[i].Description = Console.ReadLine();
      Console.Write("Enter the price for product {0}: ", i);
      products[i].Price = System.Convert.ToDouble(Console.ReadLine());
   Console.Write("Enter the number of the product to search: ");
   int prod = System.Convert.ToInt32(Console.ReadLine());
   Console.WriteLine("Product {0}, Description: {1}, Price: £{2}", prod, products[prod].Description, products[prod].Price);

Having A Go

  1. Design a record structure to store multiple choice questions for a small quiz program. Each record must include the question text, the text for 3 possible answers and the letter that corresponds to the correct answer.

    Use an array to store 3 questions using the record structure. Use a loop to output the questions to the screen and accept the user's answers in the form a, b, or c. Store these answers in an array.

    Use a loop to check whether each answer is correct and store the running total of marks in an integer variable.

    End the program by telling the user how many questions they got right.