Monday, 25 February 2013

Initialising a 2D array



















Examples
Assuming we have the following array b:
printf ("%d", b[0][0]);  /* prints 1 */
printf ("%d", b[1][0]);  /* prints 3 */
printf ("%d", b[1][1]);  /* prints 4 */



Single vs multidimensional


Dimensionality


—Determined by the number of subscripts of an array.

•x[i]  

•y[i][j]

•z[i][j][k] 

Some more tips..

Single Integer





















Another array example

//Find the lowest number in the array num[10]
main()
{
int num[10]={4,9,11,2,13,77,8,19,21,1};
int min, i;
min = num[0];
for (i=1; i<10; i++) {
  if (num[i] < min)
     min = num[i];
}
printf(“The minimum is %d”, min);
}
Array example
/* which type of integer is valid in array declaration*/
#include <stdio.h>
int main(void)
  {
  int x[5];  /* x[5] is array initialization /
  x[0]=23;  /* valid */
  x[2.3]=5;  /* invalid: index is not an int */
  return 0;
  }

Examples

int x [5] = { 1,2,3,4,5 };  size 10 bytes
creates array with elements 0-4 values 1-5
int x [5] = {4,3};  size 10 bytes
creates array with elements 0-4 values 4,3,0,0,0
int x [ ] = {1,2,3};  size 6 bytes
creates array with elements 0-2 values 1,2,3
char c[4] = {‘M’, ‘o’ , ‘o’ };  size 4 bytes
creates array with elements 0-3 values M o o NULL


1-D Array Declaration


To declare a 1-D Array, we specify 1 size 
  data_type array_name [size];
For Eg.
     int number[30];
     float marks[60];
     char name[15];

Difference Between Ordinary Variable & Arrays


Ordinary variable store one value at a time
Arrays are used for storing more than one value at a time in a single variable name

Arrays

What is an array?

An array is a consecutive series of variables that share one variable name
The individual data items in an array must all be of the same data type, accessed using an index
Often used when dealing with multiple data items possessing common characteristics
Ex. 24 hourly temperature readings might be stored in array named temperature

String functions

Read This: strncpy() strncat() strstr () strlen() — Predefined function defined in string.h . — — Returns the len...