Saturday 31 August 2013

ONE – DIMENSIONAL ARRAY | C Programming Language Tutorial pdf



ONE – DIMENSIONAL ARRAY:

A list of items can be given one variable name using only one subscript and such a variable is called a single – subscripted variable or a one – dimensional array.
Declaration of One-Dimensional Arrays :
=> Like any other variables, arrays must be declared before they are used.
The general form of array declaration is
Syntax:
<datatype> <array_name>[sizeofarray];
=> The datatype specifies the type of element that will be contained in the array, such as int, float, or char.
=> The size indicates the maximum number of elements that can be stored inside the array.
=> The size of array should be a constant value.

Examples:
float height[50];
=>  Declares the height to be an array containing 50 real elements. Any subscripts 0 to 49 are valid.
int group[10];
=>  Declares the group as an array to contain a maximum of 10 integer constants.
char name[10];
=>  Declares the name as a character array (string) variable that can hold a maximum of 10 characters.
Ex: int number[10];
=>  The subscript should begin with number '0'.
i.e. x[0].
=>  To represent a set of five numbers, say (35, 40, 20, 57, 19) by an array variable “number”.
We may declare the variable “number” as int number[5];
=>  Hence the computer reserves five storage locations as shown
=>  The values to the array elements can be assigned as
number[0] = 35;
number[1] = 40;
number[2] = 20;
number[3] = 57;
number[4] = 19;
=>  This would cause the array number to store the values as
Valid Statements :
=>  a = number[0] + 10;
=>  number[4] = number[0] + number[2];
=>  number[2] = x[5] + y[10];
=>  value[6] = number[i] * 3;

Example-1: Write a program to print bytes reserved for various types of data and space required for storing them in memory using array.
# include<stdio.h>
# include<conio.h>
main ( )
{
int a[10];
char c[10];
float b[10];
clrscr( );
printf(“the type „int? requires %d bytes”, sizeof(int));
pirntf(“ \n The type „char? requires %d bytes”, sizeof(char));
printf(“ \n The type „float? requires %d bytes”, sizeof(float));
printf(“ \n %d memory locations are reserved for ten „int? elements”, sizeof(a));
printf (“ \n %d memory locations are reserved for ten „char? elements”,sizeof(c));
printf (“ \n %d memory locations are reserved for ten „float? elements”,sizeof(b));
getch( );
}

Output:
The type 'int' requires 2 bytes
The type 'char' requires 1 bytes
The type 'float' requires 4 bytes
20 memory locations are reserved for ten 'int' elements
10 memory locations are reserved for ten 'char' elements
40 memory locations are reserved for ten 'float' elements.


Example-2: Write a program using a single subscripted variable to read and display the array elements.
# include<stdio.h>
# include<conio.h>
main( )
{
int i ;
float x[10];
printf(“Enter 10 real numbers: \n”); /* reading values into Array */
for (i=0; i <10; i++)
{
scanf(“ %f ”, &x[i]);
}
/* printing of x[i] values */
printf(“The array elements are:”);
for(i=0; i < 10; i++)
{
printf(“%d \t”, x[i]);
}
getch( );
}

Output:
Enter 10 real numbers:
1.1 2.2 3.3. 4.4 5.5 6.6 7.7 8.8 9.9 10.10

The array elements are:
1.1 2.2 3.3. 4.4 5.5 6.6 7.7 8.8 9.9 10.10

Data type and their required bytes
   Data type           Size
     char               1 byte
     int                  2 bytes
     float               4 bytes
     long               4 bytes
     double           8 bytes

Initialization of One – Dimensional Arrays:
=>  After an array is declared, its elements must be initialized. Otherwise they will contain “garbage”.
=>  An array can be initialized at either of the following stages.
(i) At compile time
(ii) At run time.
(1) Compile Time Initialization:
We can initialize the elements of arrays in the same way as the ordinary variables when they are declared.
The general form of initialization of array is datatype array_name[size] = { list of values };
The values in the list are separated by commas.

Example:
int number[3] = {0,0,0};
=>  i.e., we will declare the variable number as an array of size 3 and will assign zero to each element.
=>  If the number of values in the list is less than the number of elements, then only that many elements will be initialized.
=>  The remaining elements will be set to zero automatically.

Ex:
float total[5] = { 0.0, 15.75, -10};
=>  The size may be omitted.
=>  In such cases, the compiler allocates enough space for all initialized elements.

Ex:
int counter [ ] = {1,1,1,1};
=>  This will declare the counter array to contain four elements with initial values 1.
=> Character arrays may be initialized in a similar manner.
char name[ ] = { „J?, „o?, „h?, „n?, „\0?};
=>  This declares the name to be an array of five characters initialized with the string “John” ending with a null character.
=>  Alternative declaration is char name[ ] = “John”;
=>  Compile time initialization may be partial. i.e., the number of initializers may be less than the declared size. In such cases the remaining elements are initialized to zero, if the array type is numeric.And NULL if the type is char.
Ex: int number[5] = {10,20};
=>  Will initialize the first two elements to 10 & 20 respectively and the remaining elements to zero.
Similarly
char city[5] = {'B'};
will initialize the first element to 'B' and the remaining four to NULL.
=>  If we have more initializers than the declared size, the compiler will produce an error.
int number[3] = {10,20,30,40};
will not work. It is illegal in C.

(2) Run Time Initialization:
=>  An array can be explicitly initialized at run time.
=>  This approach is usually applied for initializing long arrays.
Ex:
- - - - - -
- - - - - -
for(i=0; i<100; i++)
{
if (i<50)
sum [i] = 0.0;
else
sum [i]= 1.0;
}
-----------
=>  Here the first 50 elements of the array “sum” are initialized to zero.
=>  While remaining 50 elements are initialized to 1.0 at run time.
=>  We can also use a read function such as 'scanf' to initialize an array.

Ex:
int x[3];
scanf(“ %d %d %d ”, &x[0], &x[1], &x[2]);
=>  This will initialize array elements with the values entered through the key board.
=>  Character arrays are called strings.
=>  There is a slight difference between an integer array and a character array.
=>  In character array NULL („\0?) character is automatically added at the end.
=>  In other types of arrays no character is placed at the end.
=>  Hence by using NULL character compiler detects the end of the character array.

Example-1:Write a program to display character array with their address.
# include<stdio.h>
# include<conio.h>
void main( )
{
char name[10] = {„A?, „R?, „R?, „A?, „Y?};
int i = 0;
clrscr( ) ;
printf(“ \n character memory location \n?);
while(name[i] ! = „\0?)
{
printf(“ \n [%c] \t [%u]”, name[i], &name[i]);
i++;
}
}

Output:
Character Memory Location
[A] 4054
[R] 4055
[R] 4056
[A] 4057
[Y] 4058

Example-2: Program to find out the largest and smallest element in an array.
# include<stdio.h>
#include<conio.h>
main( )
{
int i,n;
float a[50], large, small;
printf(“size of vector/value:”);
scanf(“%d”, &n);
printf(“ \n vector elements are \n”);
for(i=0; i<n; i++)
scanf(“ %f ”, &a[ i ]);
large = a[0];
small = a[0];
for(i=1; i<n; i++)
{
if(a[ i ] > large)
large = a[ i ];
else if(a[ i ] < small)
small = a[ i ];
}
printf(“\n Largest element in vector is %8.2f \n”, large);
printf(“ \n smallest element in vector is %8.2f \n”, small);
getch( );
}

Output:
Size of vector : 7
Vector elements
34.00 – 9.00 12.00 10.00 - 6.00 0.00 36.00
Largest element in vector is 36.00
Smallest element in vector is – 9.00

Example-3:
Program to sort the vector elements in ascending order.
# include<stdio.h>
# include<conio.h>
main( )
{
int i,j,k,n;
float a[50], temp;
printf(“size of vector:”);
scanf(“%d”, &n):
printf(“ \n vector elements are : \n”);
for( i=0; i < n; i++)
scanf(“ %f ”, &a[ i ]);
for( i=0; i < n-1; i++)
for( j=i+1;j<n; j++)
{
if(a[ i ] > a[ j ])
{
temp = a[ i ];
a[ i ] = a[ j ];
a[ j ] = temp;
}
}
printf(“\n vector elements in ascending order : \n”);
for( i=0; i<n; i++)
printf(“ %8.2f ”, a[ i ]);
getch( );
}

Output:
Size of Vector : 8
Vector elements are
91.00 20.00 1.00 7.00 34.00 11.00 -2.00 6.00
Vector elements in ascending order;
-2.00 1.00 6.00 7.00 11.00 20.00 34.00 91.00

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved