Arrays in C
Arrays in C
These refer to groups of data elements that are organized in a single unit
so that they can be used more efficiently as compared to the simple data
types such as integers and strings. An example of a data structure is the
array. Ordinary variables store one value at a time while an array will
store more than one value at a time in a single variable name.
Data structures are important for grouping sets of similar data together
and passing them as one. For example, if you have a method that prints a
set of data but you don't know when writing the procedure how large that
set is going to be, you could use an array to pass the data to that method
and loop through it.
Arrays
Arrays a kind of data structure that can store a fixed-size sequential
collection of elements of the same type.
When we work with large number of data values we need that many number of
different variables. As the number of variables increases, the complexity
of the program also increases and so the programmers get confused with the
variable names.
There may be situations where we need to work with large number of similar
data values. To make this work more easily, C programming language provides
a concept called "Array".
Definition
An array is a special type of variable used to store multiple values of
same data type at a time.
double balance[10];
The number of values between braces { } cannot be larger than the number of
elements that we declare for the array between square brackets [ ].
If you omit the size of the array, an array will be big enough to hold all
the initialized values.
//assigning elements
numbers[0]=20;
numbers[1]=35;
numbers[2]=55;
numbers[3]=100;
numbers[4]=50;
return 0;
}
Example 1
Example 3
/*program to capture ten integer number, cound and display odd and even
numbers using array*/
#include <stdio.h>
int main ()
{
int n[10]; //an array of 10 elements
int i,j;//loop variables used to represent the array indexes
int odd=0, even=0;
return 0;
}
Example 4
Example 5
Assignment
Write a C program that accepts ten integer numbers. Allow the user to select a value from the list through
the keyboard and determine the number of times the selected value appears in the list. The program should
then display all entered integers, selected value and number of times the selected integer appears in the list
using arrays.