0% found this document useful (0 votes)
6 views10 pages

CUITM114 Arrays Revision

Uploaded by

convincenemapare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

CUITM114 Arrays Revision

Uploaded by

convincenemapare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Arrays

Single Dimension and


Multidimensional
What is an Array
• An array is a group of contiguous memory locations
that all have the same data type.
• Array is the collection of similar data types or
collection of similar entity stored in contiguous
memory location.
• Each data item of an array is called an element.
• And each element is unique and located in separated
memory location.
• Each of elements of an array share a variable but each
element having different index number
Types of Arrays
1. Single dimensional array

2. Multidimensional array
Single Dimensional Array Declaration
• An array is declared in the same manner as ordinary variables, except that
each array name must be accompanied by a size specification.

• type array_name[array_size];

• where array_name is the name of an array of n elements of the type


specified. The size of an array must be an integer constant.
• The integer array declaration int x[100], creates an array that is 100 elements
along with the first element being at index 0 and the last being at index 99.
• Example of array declarations are :
int i[100];
char text[80];
float n[12];
Array initialisation
• An array can be initialized when declared by specifying
the values of some or all of its elements.
• Arrays can be initialized at the time of declaration when
their initial values are known in advance.
• The values to initialize an array must be constants never
variables or function calls
• Examples of Array initialisation
int array[5]={4,6,5,7,2};
int array[] = {23,40,10};
float x[6]={2.25,0.25,0,-0.50,5.2,0.75};
Array Initialisation using User input
• An array can be initialised from input entered using standard
input(from keyboard) with the help of a loop
• Example
int arr[5],i;
for(i=0;i<5;i++)
{
printf(“enter a value for arr[%d] \n”,i);
scanf(“%d”,&arr[i]);
}
The above code will create an array arr with a size of 5 and the
array is initialised with values entered by the user
Accessing Array Elements
• An element is accessed by indexing the array
name. This is done by placing the index of the
element within square brackets after the
name of the array. For example:
double rate = array[7];
The above statement will take 8th element from
the array and assign the value to rate variable
Example Program
/*Write a program to input values into an array and display them*/
#include<stdio.h>
int main()
{
int arr[5],i;
for(i=0;i<5;i++)
{
printf(“enter a value for arr[%d] \n”,i);
scanf(“%d”,&arr[i]);
}
printf(“the array elements are: \n”);
for (i=0;i<5;i++)
{
printf(“%d\t”,arr[i]);
}
return 0;
}
Example 2
/* Write a program to add 10 array elements */
#include<stdio.h>
void main()
{
int i ;
int arr [10];
int sum=o;
for (i=0; i<=9; i++)
{
printf (“enter the %d element \n”, i+1);
scanf (“%d”, &arr[i]);
}
for (i=0; i<=9; i++)
{
sum = sum + a[i];
}
printf (“the sum of 10 array elements is %d”, sum);
}
Exercise
• Using an Array write a program that accepts 5
values from a user and it then displays the
average of the 5 values entered.

You might also like