Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Array

Array is a container which can hold a fix number of items and these items should be
of the same type. Following are the important terms to understand the concept of
Array.
• Element − Each item stored in an array is called an element.
• Index − Each location of an element in an array has a numerical index,
which is used to identify the element.

As per the above illustration, following are the important points to be considered.
• Index starts with 0.
• Array length is 10 which means it can store 10 elements.
Each element can be accessed via its index. For example, we can fetch an element at index 6
as 27.
// Write a program in C to create and access an array element

#include <stdio.h>

int main()

int myNumbers[] = {25, 50, 75, 100};

printf("%d\n", myNumbers[0]);

printf("%d", myNumbers[1]);

return 0;

}
// Write a program in C to change an array element

#include <stdio.h>

int main()

int myNumbers[] = {25, 50, 75, 100};

myNumbers[0] = 33;

printf("%d", myNumbers[0]);

return 0;

}
// Write a program in C to loop through an array

#include <stdio.h>

int main()

int myNumbers[] = {25, 50, 75, 100};

int i;

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

printf("%d\n", myNumbers[i]);

return 0;

}
// Write a program in C to set array size

#include <stdio.h>

int main()

// Declare an array of four integers:

int myNumbers[4];

// Add elements to it

myNumbers[0] = 25;

myNumbers[1] = 50;

myNumbers[2] = 75;

myNumbers[3] = 100;

printf("%d\n", myNumbers[0]);

return 0;

}
Arrays
Arrays are used to store multiple values in a single variable, instead of
declaring separate variables for each value.

To create an array, define the data type (like int) and specify the name of
the array followed by square brackets [].

To insert values to it, use a comma-separated list, inside curly braces:

int myNumbers[] = {25, 50, 75, 100};

We have now created a variable that holds an array of four integers.

Access the Elements of an Array


To access an array element, refer to its index number.

Array indexes start with 0: [0] is the first element. [1] is the second
element, etc.

This statement accesses the value of the first element [0] in myNumbers:

Example
int myNumbers[] = {25, 50, 75, 100};
printf("%d", myNumbers[0]);

// Outputs 25

Change an Array Element


To change the value of a specific element, refer to the index number:

Example
myNumbers[0] = 33;
Example
int myNumbers[] = {25, 50, 75, 100};
myNumbers[0] = 33;

printf("%d", myNumbers[0]);

// Now outputs 33 instead of 25

Loop Through an Array


You can loop through the array elements with the for loop.

The following example outputs all elements in the myNumbers array:

Example
int myNumbers[] = {25, 50, 75, 100};
int i;

for (i = 0; i < 4; i++) {


printf("%d\n", myNumbers[i]);
}

Set Array Size


Another common way to create arrays, is to specify the size of the array, and
add elements later:

Example
// Declare an array of four integers:
int myNumbers[4];

// Add elements
myNumbers[0] = 25;
myNumbers[1] = 50;
myNumbers[2] = 75;
myNumbers[3] = 100;
Using this method, you should know the size of the array, in order for
the program to store enough memory.

You are not able to change the size of the array after creation.
Program to print reverse array in C
#include <stdio.h>

int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int loop;

for(loop = 9; loop >= 0; loop--)


printf("%d ", array[loop]);

return 0;
}

The output should look like this −


0987654321
Input and Output Array Elements
Here's how you can take input from the user and store it in an array
element.

// take input and store it in the 3rd element


scanf("%d", &mark[2]);

// take input and store it in the ith element


scanf("%d", &mark[i-1]);

Array Input/Output
// Program to take 5 values from the user and store them in an array
// Print the elements stored in the array

#include <stdio.h>

int main() {

int values[5];

printf("Enter 5 integers: ");

// taking input and storing it in an array


for(int i = 0; i < 5; ++i) {
scanf("%d", &values[i]);
}

printf("Displaying integers: ");

// printing elements of an array


for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
return 0;
}
Program to calculate sum of array in C
#include <stdio.h>

int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int sum, loop;

sum = 0;

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


sum = sum + array[loop];
}

printf("Sum of array is %d.", sum);

return 0;
}

The output should look like this −


Sum of array is 45.
Calculate Average
// Program to find the average of n numbers using arrays

#include <stdio.h>

int main() {

int marks[10], i, n, sum = 0;


double average;

printf("Enter number of elements: ");


scanf("%d", &n);

for(i=0; i < n; ++i) {


printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);

// adding integers entered by the user to the sum variable


sum += marks[i];
}

// explicitly convert sum to double


// then calculate average
average = (double) sum / n;

printf("Average = %.2lf", average);

return 0;
}
Program to find largest array element in C
#include <stdio.h>

int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int loop, largest;

largest = array[0];

for(loop = 1; loop < 10; loop++) {


if( largest < array[loop] )
largest = array[loop];
}

printf("Largest element of array is %d", largest);

return 0;
}

The output should look like this −


Largest element of array is 9
Second largest array element in C
#include <stdio.h>

int main() {
int array[10] = {101, 11, 3, 4, 50, 69, 7, 8, 9, 0};
int loop, largest, second;

if(array[0] > array[1]) {


largest = array[0];
second = array[1];
} else {
largest = array[1];
second = array[0];
}

for(loop = 2; loop < 10; loop++) {


if( largest < array[loop] ) {
second = largest;
largest = array[loop];
} else if( second < array[loop] ) {
second = array[loop];
}
}

printf("Largest - %d \nSecond - %d \n", largest, second);

return 0;
}

The output should look like this −


Largest - 101
Second - 69
Program to find smallest array element in C
#include <stdio.h>

int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int loop, smallest;

smallest = array[0];

for(loop = 1; loop < 10; loop++) {


if( smallest > array[loop] )
smallest = array[loop];
}

printf("Smallest element of array is %d", smallest);

return 0;
}

The output should look like this −


Smallest element of array is 0

You might also like