0% found this document useful (0 votes)
130 views3 pages

Array in C

An array is a collection of data items of the same type that are accessed using a common name. Arrays can have multiple dimensions like a list (1D) or table (2D). In C, arrays are declared with the data type, array name, and size in square brackets. Individual elements are accessed via an index with 0 being the first element. Arrays can be initialized during declaration. Loops are used to input or output array elements. Example programs demonstrate taking input into an array, calculating the sum of array elements, and printing even numbers between 1 to N. Homework includes writing programs to print odd numbers in an array, calculate average, find largest number, and calculate Fibonacci numbers.

Uploaded by

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

Array in C

An array is a collection of data items of the same type that are accessed using a common name. Arrays can have multiple dimensions like a list (1D) or table (2D). In C, arrays are declared with the data type, array name, and size in square brackets. Individual elements are accessed via an index with 0 being the first element. Arrays can be initialized during declaration. Loops are used to input or output array elements. Example programs demonstrate taking input into an array, calculating the sum of array elements, and printing even numbers between 1 to N. Homework includes writing programs to print odd numbers in an array, calculate average, find largest number, and calculate Fibonacci numbers.

Uploaded by

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

Array

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];

How to declare an array?


Array variables are declared identically to variables of their data type, except that the variable
name is followed by one pair of square [ ] brackets for each dimension of the array.
dataType arrayName[arraySize];

For example:

int num[35]; /* An integer array of 35 elements */


char ch[10]; /* An array of characters for 10 elements */

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].

How to initialize an array in C?


It is possible to initialize an array during declaration. For example,
int mark[5] = {19, 10, 8, 17, 9};
You can also initialize an array like this.
int mark[] = {19, 10, 8, 17, 9};
Here, we haven't specified the size. However, the compiler knows its size is 5 as we are
initializing it with 5 elements.
Change Value of Array elements
int mark[5] = {19, 10, 8, 17, 9}

mark[2] = -1; // make the value of the third element to -1

mark[4] = 0; // make the value of the fifth element to 0

Input and Output Array Elements


Here's how you can take input from the user and store it in an array element.
scanf("%d", &mark[2]); // take input and store it in the 3rd element

scanf("%d", &mark[i-1]); // take input and store it in the ith element

Here's how you can print an individual element of an array.


printf("%d", mark[0]); // print the first element of the array

printf("%d", mark[2]); // print the third element of the array

printf("%d", mark[i-1]); // print ith element of the array

Example 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;
}

Example 2: Calculate sum of numbers


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

#include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0, 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];
}

printf("Sum = %d", sum);

return 0;
}

Example 3: Print only even numbers from 1 to N.


/* C Program to Print Even Numbers from 1 to N using For Loop and If */

#include<stdio.h>

int main()
{
int i, number;

printf("\n Please Enter the Maximum Limit Value : ");


scanf("%d", &number);

printf("\n Even Numbers between 1 and %d are : \n", number);


for(i = 1; i <= number; i++)
{
if ( i % 2 == 0 )
{
printf(" %d\t", i);
}
}

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.

You might also like