Skip to main content
What is array in C language - What is 2D and 3D arrays in C language - Write a program of 2D array in c language - Write a program of 3D array in c language
What is array in C language
In the C programming language, an array is a data structure
that allows you to store a collection of elements of the same data type under a
single name. These elements are stored in contiguous memory locations and can
be accessed using an index, which starts at 0 for the first element.
Here's a basic syntax for declaring an array in C
data_type array_name[array_size];
- data_type represents the data type of the
elements in the array (e.g., int, float, char).
- array_name is the name of the array.
- array_size specifies the number of elements the
array can hold. It must be a non-negative integer.
For example, to declare an integer array of size 5
int myArray[5];
You can then access and manipulate individual elements in
the array using their index. In C, array indices start from 0, so to access the
first element, you would use myArray[0], and to access the fifth element, you
would use myArray[4].
Here's an example of how to initialize and access elements
in an array:
int myArray[5]; // Declare an integer array with 5 elements
myArray[0] = 10; //
Assign a value to the first element
myArray[1] = 20; //
Assign a value to the second element
int sum = myArray[0] + myArray[1]; // Access and compute values from the array
Arrays are an essential part of C and are used in various
programming tasks, such as storing collections of data, implementing data
structures, and processing large sets of information.
What is 2D and 3D arrays in C language
In the C programming language, you can create arrays with
multiple dimensions, such as 2D arrays and 3D arrays, to represent tables or
grids of data in two or three dimensions, respectively.
2D Arrays: - A 2D array is an array of arrays. It can be thought of as a
table with rows and columns.
- In C, a 2D array is declared as follows:
data_type array_name[row_size][column_size];
- data_type represents the data type of the
elements in the array.
- array_name is the name of the 2D array.
- row_size and column_size specify the number of
rows and columns, respectively.
Here's an example of a 2D integer array:
int my2DArray[3][4]; // A 3x4 integer array
You can access elements in a 2D array using two indices: one
for the row and one for the column, like my2DArray[row_index][column_index].
3D Arrays:
- A 3D array is an extension of the concept of 2D arrays,
adding another dimension. It can be thought of as a cube of data.
- In C, a 3D array is declared as follows:
data_type array_name[x_size][y_size][z_size];
- data_type represents the data type of the
elements in the array.
- array_name is the name of the 3D array.
- x_size, y_size, and z_size specify the sizes in
the three dimensions.
Here's an example of a 3D integer array:
int my3DArray[2][3][4]; // A 2x3x4 integer array
Accessing elements in a 3D array requires three indices: one
for each dimension (my3DArray[x_index][y_index][z_index]).
2D and 3D arrays are useful when you need to work with
structured data that can be represented as tables or grids with more than one
dimension. They are commonly used in various applications, such as image
processing, computer graphics, scientific simulations, and representing
multi-dimensional data sets.
Write a program of array to manipulate array elements using
loops
Certainly! Here's a simple C program that demonstrates how
to manipulate array elements using loops. In this example, we'll create an
array of integers, initialize it, and perform some basic operations like
finding the sum and average of the elements.
#include <stdio.h>
int main() {
// Define an
integer array with 5 elements
int myArray[5];
// Initialize the
array with values
myArray[0] = 10;
myArray[1] = 20;
myArray[2] = 30;
myArray[3] = 40;
myArray[4] = 50;
// Calculate the
sum of array elements using a loop
int sum = 0;
for (int i = 0; i
< 5; i++) {
sum +=
myArray[i];
}
// Calculate the
average
double average =
(double)sum / 5;
// Display the
array elements and results
printf("Array
elements: ");
for (int i = 0; i
< 5; i++) {
printf("%d ", myArray[i]);
}
printf("\n");
printf("Sum
of array elements: %d\n", sum);
printf("Average of array elements: %.2f\n", average);
return 0;
}
In this program:
1.
We define an integer array myArray with 5
elements.
2.
We initialize the elements with values from 10
to 50.
3.
We use a for loop to calculate the sum of the
array elements.
4.
We also calculate the average by dividing the
sum by the number of elements.
5.
Finally, we display the array elements, sum, and
average using printf statements.
When you run this program, it will output the array
elements, sum, and average:
Array elements: 10 20 30 40 50
Sum of array elements: 150
Average of array elements: 30.00
You can modify and extend this program to perform various
other operations on arrays using loops, such as finding the maximum or minimum
element, searching for specific values, or sorting the array.
Write a program of 2D array in c language
Here's a simple example of a 2D array program in the C
language. This program creates a 2D array, populates it with values, and then
prints those values.
#include <stdio.h>
int main() {
int rows, cols;
// Define the
dimensions of the 2D array
printf("Enter
the number of rows: ");
scanf("%d", &rows);
printf("Enter
the number of columns: ");
scanf("%d", &cols);
// Declare and
initialize the 2D array
int
arr[rows][cols];
// Input values
into the array
printf("Enter
the elements of the array:\n");
for (int i = 0; i
< rows; i++) {
for (int j = 0;
j < cols; j++) {
scanf("%d", &arr[i][j]);
}
}
// Print the
elements of the array
printf("The
elements of the 2D array are:\n");
for (int i = 0; i
< rows; i++) {
for (int j =
0; j < cols; j++) {
printf("%d\t", arr[i][j]);
}
printf("\n");
}
return 0;
}
This program first asks the user for the number of rows and
columns for the 2D array, then it takes input for each element in the array,
and finally, it prints the elements of the array.
Write a program of 3D array in c language
Here's an example of a 3D array program in the C
language. This program creates a 3D array, populates it with values, and then
prints those values.
#include <stdio.h>
int main() {
int x, y, z;
// Define the
dimensions of the 3D array
printf("Enter
the number of layers: ");
scanf("%d", &x);
printf("Enter
the number of rows per layer: ");
scanf("%d", &y);
printf("Enter
the number of columns per row: ");
scanf("%d", &z);
// Declare and
initialize the 3D array
int arr[x][y][z];
// Input values
into the array
printf("Enter
the elements of the 3D array:\n");
for (int i = 0; i
< x; i++) {
for (int j =
0; j < y; j++) {
for (int k
= 0; k < z; k++) {
scanf("%d", &arr[i][j][k]);
}
}
}
// Print the
elements of the array
printf("The
elements of the 3D array are:\n");
for (int i = 0; i
< x; i++) {
for (int j =
0; j < y; j++) {
for (int k
= 0; k < z; k++) {
printf("%d ", arr[i][j][k]);
}
printf("\n");
}
printf("\n");
}
return 0;
}
This program prompts the user to input the dimensions of the
3D array (number of layers, rows per layer, and columns per row). It then takes
input for each element in the array and prints the elements of the 3D array.
Comments
Post a Comment