Monday, January 30, 2012

4.1.1. Defining an array and accessing the data


4. Arrays, structures and strings

4.1. Basic concepts on arrays

4.1.1. Defining an array and accessing the data

A table, vector, matrix or array is a set of elements, all of which are the same type. These elements will all have the same name, and occupy a contiguous space in memory.

For example, if we define a set of numbers, the we will use "int []" as data type. If we know from the beginning how much data we have (eg 4 numbers), we can reserve space with "= new int [4]", like this:

int[] example = new int[4];

We can access each individual value with the name of the variable (example) and the number of the item we want to access. First element is number 0, so in the previous example we would have 4 elements, named example[0], example[1], example[2], example[3].

As an example, let's define a set of 5 integers and calculate their sum:

/*---------------------------*/
/*  C# Example #33:          */
/*  example33.cs             */
/*                           */
/*  Arrays example #1        */
/*                           */
/*  Intro to C#,             */
/*    Nacho Cabanes          */
/*---------------------------*/

using System;

public class Example33
{
  public static void Main()
  {

    int[] number = new int[5];     /* An array for 5 integers */
    int sum;              /* An integer for the sum */

    number[0] = 200;      /* We assign values */
    number[1] = 150;
    number[2] = 100;
    number[3] = -50;
    number[4] = 300;
    sum = number[0] +     /* And we calculate the sum */
        number[1] + number[2] + number[3] + number[4];
    Console.WriteLine("Their sum is {0}", sum);
    /* Note: this an inefficient way. We'll improve it later */
  }
}



Suggested exercises:
·       A program that asks the user for 4 numbers, stores them (using an array), calculates their average and then displays the average and the entered data.
·       A program that asks the user for 5 real numbers and then displayed in the reverse order.
·       A program that asks the user 10 integers, and calculates (and shows) which one is the greatest.

Sunday, January 29, 2012

3.5. The "Boolean" values

C# also includes a data type called "boolean" ("bool"), which can take two values: true or false:

bool found;
   
found = true;

This data type allows writing in a simpler way some conditions that may be complex. So we can make certain portions of our program more readable: not "if ((lives == 0) | | (time == 0) | | ((enemiesAlive == 0) & & (level == lastLevel)))" but simply " if (gameFinished) ... "

The variables "bool" also can be given value from the result of a comparison:

gameFinished = false;
gameFinished = (enemiesAlive ==0) && (level == lastLevel);
if (lives == 0) gameFinished = true;


We will use them from now on sources that use more complex conditions. An example to request a letter and say whether it is a vowel, a numerical figure, or other symbol, using "bool" variables might be:

/*---------------------------*/
/*  C# Example #32:          */
/*  example32.cs             */
/*                           */
/*  Conditions with if (8)   */
/*   bool variables          */
/*                           */
/*  Intro to C#,             */
/*    Nacho Cabanes          */
/*---------------------------*/

using System;

public class Example32
{
  public static void Main()
  {
    char letter;
    bool isVowel, isDigit;

    Console.WriteLine("Enter a letter");
    letter = Convert.ToChar(Console.ReadLine());
   
    isDigit = (letter >= '0') && (letter <='9');

    isVowel = (letter == 'a') || (letter == 'e') || (letter == 'i') ||
      (letter == 'o') || (letter == 'u');
   
    if (isDigit)
      Console.WriteLine("It is a digit.");
    else if (isVowel)
        Console.WriteLine("It is a vowel.");
      else
        Console.WriteLine("It is a consonant or another symbol.");
  }
}

Suggested exercises:
·       Create a program that uses the conditional operator to give a boolean variable named "equal" the value "true" if the two numbers entered by the user are the same, or "false" if they are different.
·       Create a program that uses the conditional operator to give a boolean variable named "bothEven" the value "true" if two numbers entered by the user are the even, or "false" if any of them is odd.