0% found this document useful (0 votes)
13 views6 pages

Arrays in C

The document provides an overview of data structures, specifically focusing on arrays in the C programming language. It explains the definition, types, declaration, initialization, and accessing of single-dimensional arrays, along with several example programs demonstrating their usage. Additionally, it includes an assignment task related to counting occurrences of a selected integer in an array.

Uploaded by

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

Arrays in C

The document provides an overview of data structures, specifically focusing on arrays in the C programming language. It explains the definition, types, declaration, initialization, and accessing of single-dimensional arrays, along with several example programs demonstrating their usage. Additionally, it includes an assignment task related to counting occurrences of a selected integer in an array.

Uploaded by

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

DATA STRUCTURES

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.

There are two types of Arrays in C


1. Single Dimensional Array / One Dimensional Array
2. Multi-Dimensional Array

Single Dimensional Array


Single dimensional arrays are used to store list of values of same
datatype. In other words, single dimensional arrays are used to store a row
of values.

Here, the compiler allocates 10 bytes of continuous memory locations with


single name 'balance' and tells the compiler to store five different
integer values (each in 2 bytes of memory) into that 10 bytes of memory.
In the above memory allocation, all the five memory locations has a common
name 'balance'. So the accession of individual memory location is not
possible directly. Hence compiler also assigns a numerical reference value
to every individual memory location of an array called "Index" or
"subscript”.

Declaring a single dimensional Array

To declare an array in C, a programmer specifies the type of the elements


and the number of elements required by an array

type arrayName [ arraySize ];

For example, to declare a 10-element array called balance of type double,


use this statement

double balance[10];

Here balance is a variable array which is sufficient to hold up to 10


double numbers.

Initializing a single dimensional Array

There are two ways

1. Initialization with size

double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};

The number of values between braces { } cannot be larger than the number of
elements that we declare for the array between square brackets [ ].

2. Initialization without size

double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};

If you omit the size of the array, an array will be big enough to hold all
the initialized values.

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.

double salary = balance[9];


The above statement will take the 10th element from the array and assign
the value to salary variable.

Initializing and accessing an array of five element

/*a program to initialize and access an array of five element*/


#include <stdio.h>
int main ()
{
//a five element arry of type integer
int numbers[5];

//assigning elements
numbers[0]=20;
numbers[1]=35;
numbers[2]=55;
numbers[3]=100;
numbers[4]=50;

//printing elements using indexes


printf("%d\n", numbers[0]);
printf("%d\n", numbers[1]);
printf("%d\n", numbers[2]);
printf("%d\n", numbers[3]);
printf("%d\n", numbers[4]);

return 0;
}

Array elements can be initialized and accessed using a loop.

Example 1

/*a program to initioalize ten elements an array with


values 100 to 109 and display them*/
#include <stdio.h>
int main () {

int n[ 10 ]; /* n is an array of 10 integers */


int i,j;
/* initialize elements of array n to 0 */
for ( i = 0; i < 10; i++ ) {
n[ i ] = i + 100; /* set element at location i to i + 100 */
}
/* output each array element's value */
for (j = 0; j < 10; j++ ) {
printf("Element[%d] = %d\n", j, n[j] );
}
return 0;
}
Example 2

/*program to capture ten number and display them using array*/


#include <stdio.h>
int main ()
{
int mark [10]; //an array of 10 elements
int i;//loop variable used to represent the array indexes

printf("Enter ten elements\n");


for ( i= 0; i <= 9; i ++ )
{
scanf("%d", &mark[i]);
}
printf("Display elements\n");

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


{
printf ( "%d" ,mark[i]);
}
return 0 ;
}

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;

printf("Enter ten elements\n");


for ( i= 0; i <= 9; i ++ ) //loop through the ten array elements
{
scanf("%d", &n[i]);//reads the ten numbers
}
for (j = 0; j < 10; j++ )//loop through the ten array elements
{
if(n[j]%2==0) //checks whether an element is odd or even
{
even++; //increments even numbers by 1
}
else
{
odd++; //increments odd numbers by 1
}
}
printf("Odd numbers are: %d\n", odd); //displays the number of odd
numbers
printf("Even numbers are: %d\n", even);//displays the number of even
numbers

return 0;
}

Example 4

/*Program to find the largest value in an array*/


#include <stdio.h>
int main()
{
int i, n;
float arr[100];
printf("Enter total number of elements(1 to 100): ");
scanf("%d", &n);
printf("\n");
// Stores number entered by the user
for(i = 0; i < n; ++i)
{
printf("Enter Number %d: ", i+1);
scanf("%f", &arr[i]);
}
// Loop to store largest number to arr[0]
for(i = 1; i < n; ++i)
{
// find the largest element and stores it in index 0
if(arr[0] < arr[i])
arr[0] = arr[i];
}
printf("Largest element = %.2f", arr[0]);
return 0;
}

Example 5

/* Program to find the average of n numbers using arrays*/


#include <stdio.h>
int main()
{
int n, i;
float num[100], sum = 0.0, average;
printf("Enter the numbers of elements: ");
scanf("%d", &n);

while (n > 100 || n <= 0)


{
printf("Error! number should in range of (1 to 100).\n");
printf("Enter the number again: ");
scanf("%d", &n);
}
for(i = 0; i < n; ++i)
{
printf("%d. Enter number: ", i+1);
scanf("%f", &num[i]);
sum += num[i];
}
average = sum / n;
printf("Average = %.2f", average);
return 0;
}

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.

You might also like