0% found this document useful (0 votes)
2 views

7 Arrays

C-Programming
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

7 Arrays

C-Programming
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

ARRAYS

Definition
 An array is a fixed-size sequenced collection of
elements of the same data type ie, a grouping
of like-type data that share a common name.
 It can be used to represent a list of numbers or
names.
• Since an array can represent a list of items,
the individual values of the items are
referred to as elements. Example Salary[10]
represent the tenth element (10th employee
salary in a list of organization employees
salaries).
We can use the arrays to represent
not only a simple list of values (one-
dimension) but also tables of data in
two and three dimensions.
One-Dimensional Arrays
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

• Arraysmust be declared like other variable


before they are used so that the computer can
allocate space for them in memory. The
declaration takes the following form;
type arrayName [ arraySize ];

• Typespecifies the type of elements that will be


contained in the array such as int, float, char etc.
• Sizeindicates the maximum number of elements
that can be stored inside the array.
examples
• To declares an array height to contain 5 real (floating
point) numbers, float height[5]
•C language treats character strings simply as arrays of
characters.
e.g. char name[9]
• Example of an array int number[6] diagrammatically ;

23 56 12 58 70 94 Elements
0 1 2 3 4 5 Subscript or index
Cell
• To represent 35, 40, 20, 57, 19 by an array variable
number, the array variable number is declared as
int number[5]; the computer will reserve five
storage spaces as;
Number[0]

Number[1]

Number[2]

Number[3]

Number[5]
Initialization of One-Dimensional Arrays
Format:
• type array-name[size] = { list of value };

• The values in the list are separated by commas Example int num[3]
= {0,0,0};

or to initialize array number[5] with the following values 35, 40, 20,
57, 19 :
Number[0] = 35;

Number[1] = 40;

Number[2] = 20;

Number[3] = 57;
• These elements may be used in programs
just like any other C variable. Example

number[4] = number[2] + number[3];


Working with arrays
• Arrays just like any other variable allows inputs via keyboard,
processing of data stored in arrays and display array content.

Declare int number[5];

• Input an array element e.g:

scanf(“%d”, &number[0]);

• Display array element e.g.:

printf(“%d”, number[0]);

• Process array element:

tot=tot+number[1];
Working with arrays
• The for statement enhances processing of array elements in
sequence from the first element to the last element.

• E.g. to input elements to an array number[5]:

for (i=0;i<5;i++)

scanf(“%d”,&number[i]);

• E.g. to output elements from array number[5]:

for (i=0;i<5;i++)

printf (“%d”,number[i]);
Examples

1. Write a program to input a name and display the name


in reverse.

2. Write a program to input 8 numbers to an array and


calculate their total value.

3. Write a program to input 8 numbers to an array and


display the smallest and the largest number.
Program to input a name and display the name in reverse

int main() printf(“name in reverse:”);

{ for(i=9;i>=0;i--)

int i; char name[10]; printf(“%c”,name[i]);

printf(“enter name:”); }

for(i=0;i<10;i++)

scanf(“%c”,&name[i]);
Program to input 8 numbers to an array and calculate
their total value

# include<stdio.h> printf(“total is %d”,tot);


int main() }
{ int i,num[8],tot; return 0;
tot=0; }
for(i=0;i<8;i++){
printf(“enter numbers:”);
scanf(“%d”,&num[i]);
tot=tot+num[i];
}
2-dimensional arrays
• Two dimensional arrays can be used to store a table
of values.

• The table can be thought of as a Matrix containing


rows and columns.

• Two dimensional arrays are declared as follows;

Type array_name[row_ size][column_size];


Program to input numbers to a 3
by 4 array and display the result

You might also like