The document discusses arrays in C programming. It defines arrays as groups of same data types that can store integer, float, character, or other data. Arrays allow storing multiple values in a single variable and accessing elements using indexes. The document provides examples of one-dimensional and two-dimensional arrays, and explains how to initialize, declare, and access array elements. It also discusses using for loops and nested loops to iterate through arrays.
2. An array is a group of same data type.
For instance, An int array has the ements of
int types.
A float array holds the elements of float
types.
3. Let’s Consider a scenario where we need to find
out the average of 100 integer numbers entered
by user.
In C, we have two ways to do this:
1) We can define 100 variables with int data type
and then perform 100 scanf() operations to
store the entered values in the variables and
then at last calculate the average of them.
2) We can have a single integer array to store all
the values, loop the array to store all the
entered values in array and later calculate the
average.
Surely, it is convenient to store same data types in
one single variable and later access them using
array index.
4. 1. int num[35];
[It means, An integer array of 35 elements]
2. char ch[10];
[It means, an array of characters for 10
elements]
5.
6.
7. 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.
8. Example 1: C Program to calculate the
average of N (N<10) using Array.
Example 2: C Program to assign values to
one dimensional array.
Example 3: C program to Display Largest
Element of an array
Example 4: C program to check whether a
character is palindrome or not
9. An array of arrays is known as 2D array. The
two dimensional (2D) array in C
programming is also known as matrix. A
matrix can be represented as a table of rows
and columns. Before we discuss more about
two Dimensional array lets have a look at the
following C program example.
Two dimensional (2D) arrays in C
programming with example
10.
11. #include <stdio.h>
const int CITY = 2;
const int WEEK = 7;
int main()
{
int temperature[CITY][WEEK];
// Using nested loop to store values in a 2d array
for (int i = 0; i < CITY; ++i)
{
for (int j = 0; j < WEEK; ++j)
{
printf("City %d, Day %d: ", i + 1, j + 1);
scanf("%d", &temperature[i][j]);
}
}
printf("nDisplaying values: nn");
// Using nested loop to display vlues of a 2d array
for (int i = 0; i < CITY; ++i)
{
for (int j = 0; j < WEEK; ++j)
{
printf("City %d, Day %d = %dn", i + 1, j + 1, temperature[i][j]);
}
}
return 0;
12. For loop: for loop in C programming with example. A loop is used for
executing a block of statements repeatedly until a given condition
returns false.
Structure:
for (initialization; condition test; increment or decrement)
{
//Statements to be executed repeatedly
}
Nested loop: A nested loop is a loop within a loop. The most common
applications of loops are for matrix data (e.g., looping through the rows
and columns of a table). You can nest any type of loop inside any other
type; a for loop can be nested in a while loop.
Structure:
Outer-Loop
{
// body of outer-loop Inner-Loop
{
// body of inner-loop
}
}