Thursday, July 14, 2011

Array And Strings : Introduction to Arrays

INTRODUCTION OF ARRAY

Introduction to Arrays

Arrays are a data structure that is used to store a group of objects of the same type sequentially in memory. All the elements of an array must be the same data type.
An array is a collective name given to a group of similar quantities. These similar quantities could be percentage marks of 100 students, number of chairs in home, or salaries of 300 employees or ages of 25 students. Thus an array is a collection of similar elements. These similar elements could be all integers or all floats or all characters etc. Usually, the array of characters is called a "string", where as an array of integers or floats is called simply an array. All elements of any given array must be of the same type i.e we can't have an array of 10 numbers, of which 5 are ints and 5 are floats.
Some important points about Array:
  • First element of an array has zero index
  • All the data items in array are always stored in consecutive memory locations.
  • Arrays are always store under a common heading or a variable name
  • An array either be an integer, character, or floating point data item but initialized only on declaration time not afterwards.
  • An array can always be read or write through loops
Syntax:
datatype arrayName[size];
Examples:
int ID[30];
/* Could be used to store the ID numbers of students in a class */
 
float temperatures[31];
/* Could be used to store the daily temperatures in a month */
 
char name[20];
/* Could be used to store a character string. Character strings in C are terminated by the null character, '\0'. This will be discussed later in the this lesson. */

Advantage and Disadvantage of Array

Advantages:

1.       You can use one name for similar objects and save then with the same name but different indexes.
2.       Arrays are very useful when you are working with sequences of the same kind of data (similar to the first point but has a different meaning).
3.       Arrays use reference type and so.

Disadvantages:

1.       Sometimes it's not easy to operate with many index arrays.
2.       C environment doesn't have checking mechanism for array sizes.
3.       An array uses reference mechanism to work with memory which can cause unstable behavior of operating system (unless special methods were created to prevent it) and often even "blue screen" and so on.

Using Arrays

when we declare
int x[50];










.......

0
1
2
3
4
5
6
7
8
....... .....  49
if we assign as follows
x[7]=57;
then our block look as follows







57

.......

0
1
2
3
4
5
6
7
8
.......      49
Example 1: Here is a sample program that calculates and stores the squares from 1 to 100.
#include<stdio.h>
#include<conio.h>
void main()
{
    int x[100];
    int i, k;
   
    /* Calculate the squares */
    for (i = 0; i < 100; i++)
    {
k= i + 1;
x[i] = k*k;
printf("The square of %d is %d\n", k, x[i]);
      }
}
Example 2: Write a program to find out the greatest and smallest number entered in array.
#include<stdio.h>
#include<conio.h>

main()
{
    int x[100];
    int i, lrg, sml;
 
    printf("Enter 100 integer number in an array");
    for(i=0; i<100; i++)
          scanf("%d", &x[i]);
 
    //initially assigning the value in lrg and sml variables
    lrg = x[0];
    sml = x[0];
 
    //process of find out the largest and smallest number in an array
    for(i=0; i<100; i++)
    {
if(lrg < x[i])   
                lrg=x[i];
if(sml < x[i])
                sml=x[i];
    }
    printf("Smallest = %d and Largest = %d", sml, lrg);
     getch() ;
}
Example 3: Write a program asking any 10 numbers from the user. Sort them in ascending order and display them.
#include<stdio.h>
#include<conio.h>
void main()
{
    int x[10];
    int i, j, temp;
 
    printf("Enter 10 numbers in array");
    for(i=0; i<10; i++)
    scanf("%d",&x[i]);
    //reordering numbers in ascending order
    for(i=0; i<10; i++)
          for(j=i+1; j<10; j++)
          {
                if(x[i] > x[j])// for descending order just change > sign to <
                {
                      //swapping
                      temp = x[i];
                      x[i] = x[j];
                      x[j] = temp;
                }
          }
    //displaying the reordered numbers
    for(i=0; i<10; i++)
          printf("%d", x[i]);
 
    getch();
}

0 comments:

Post a Comment