Arrays
Arrays
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()
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()
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 i;
printf("%d\n", myNumbers[i]);
return 0;
}
// Write a program in C to set array size
#include <stdio.h>
int main()
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 [].
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
Example
myNumbers[0] = 33;
Example
int myNumbers[] = {25, 50, 75, 100};
myNumbers[0] = 33;
printf("%d", myNumbers[0]);
Example
int myNumbers[] = {25, 50, 75, 100};
int i;
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;
return 0;
}
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];
int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int sum, loop;
sum = 0;
return 0;
}
#include <stdio.h>
int main() {
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];
return 0;
}
int main() {
int array[10] = {101, 11, 3, 4, 50, 69, 7, 8, 9, 0};
int loop, largest, second;
return 0;
}
int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int loop, smallest;
smallest = array[0];
return 0;
}