Arrays and Structures


  1. Write a declaration for an array of 100 floats.

  2. Initialise all elements in this array to have a value of 0

  3. Assign a value of 10.5 to the 5th element in the array you have just declared.

  4. What is wrong with the following?

    
    int array[10],i;
    for (i=0;i < 10;i++)
    	array[i] = array[i+1];
    

  5. What is wrong with the following?

    
    float	index;
    float	array[9];
    
    for (index=0; index < 9; index++)
    	array[index] = index;
    


Answers





























  1. float a[100];

  2. int i;
    for (i=0;i<100;i++)
    	a[i] = 0.0;
    

  3. a[4]=10.5;

  4. The array has 10 elements,with indexes from 0 to 9, but the last time round the loop i = 9, and hence the code tries to do array[9] = array[10], which would result in an error since a[10] does not exist.

  5. The array index must always be an integer and hence the first line should have been

    int	index;


Back to Questions Back to Main Page for Lecture 5