Array in C
Array in C
An array is a collection of data items, all of the same type, accessed using a common name.
A one-dimensional array is like a list. A two dimensional array is like a table. The C language
places no limits on the number of dimensions in an array, though specific implementations may.
For example, if you want to store 10 integers, you can create an array for it.
int arr[10];
For example:
Similarly an array can be of any data type such as double, float etc.
Points to remember:
float mark[5];
Arrays have 0 as the first index, not 1. In this example, mark[0] is the first element.
If the size of an array is n, to access the last element, the n-1 index is used. In this example,
mark[4].
// 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];
#include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0, average;
return 0;
}
#include<stdio.h>
int main()
{
int i, number;
return 0;
}
Homework:
1. Write a C program to print only odd numbers in an array.
2. Write a C program to calculate average of numbers in an array.
3. Write a C program to find the largest number in an array.
4. Write a C program to calculate first 20 Fibonacci numbers.