
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Explain the concept of one and two dimensional array processing using C language
Let us first understand the one dimensional array processing in C programming language.
1D Array Processing
Storing values in 1 D Array(reading) is done as follows −
int num[5] int i; for(i=0;i<5;i++){ Scanf("%d",&num[i]); }
Retrieving stored values from 1D array(writing) is done as follows −
int num[5] int i; for(i=0;i<5;i++){ printff("%d",num[i]); }
Example program
Given below is the C program to print the elements in a reverse order from an array −
#include<stdio.h> void main(){ //Declaring the array - run time// int array[5],i; //Reading elements into the array// printf("Enter elements into the array:
"); //For loop// for(i=0;i<5;i++){ //Reading User I/p// printf("array[%d] :",i); scanf("%d",&array[i]); } //Displaying reverse order of elements in the array// printf("The elements from the array displayed in the reverse order are :
"); for(i=4;i>=0;i--){ //Displaying O/p// printf("array[%d] :",i); printf("%d
",array[i]); } }
Output
When the above program is executed, it produces the following result −
Enter elements into the array: array[0] :1 array[1] :2 array[2] :3 array[3] :4 array[4] :5 The elements from the array displayed in the reverse order are: array[4] :5 array[3] :4 array[2] :3 array[1] :2 array[0] :1
2D Array Processing
Storing values in 2D Array(reading) is done as follows −
int a[4][3]; int i,j; for(i=0;i<4;i++){ for(j=0;j<3;j++){ scanf("%d",&a[i][j]); } }
Retrieving stored values from 2D array(writing) is done as follows −
int a[4][3]; int i,j; for(i=0;i<4;i++){ for(j=0;j<3;j++){ printf("%d",a[i][j]); } }
Example Program
Following is the C program to calculate sum and product of all elements in an array by using run time compilation −
#include<stdio.h> void main(){ //Declaring the array - run time// int A[2][3],B[2][3],i,j,sum[i][j],product[i][j]; //Reading elements into the array's A and B using for loop// printf("Enter elements into the array A:
"); for(i=0;i<2;i++){ for(j=0;j<3;j++){ printf("A[%d][%d] :",i,j); scanf("%d",&A[i][j]); } printf("
"); } for(i=0;i<2;i++){ for(j=0;j<3;j++){ printf("B[%d][%d] :",i,j); scanf("%d",&B[i][j]); } printf("
"); } //Calculating sum and printing output// printf("Sum array is :
"); for(i=0;i<2;i++){ for(j=0;j<3;j++){ sum[i][j]=A[i][j]+B[i][j]; printf("%d\t",sum[i][j]); } printf("
"); } //Calculating product and printing output// printf("Product array is :
"); for(i=0;i<2;i++){ for(j=0;j<3;j++){ product[i][j]=A[i][j]*B[i][j]; printf("%d\t",product[i][j]); } printf("
"); } }
Output
When the above program is executed, it produces the following result −
Enter elements into the array A: A[0][0] :2 A[0][1] :3 A[0][2] :4 A[1][0] :1 A[1][1] :2 A[1][2] :3 B[0][0] :4 B[0][1] :5 B[0][2] :3 B[1][0] :2 B[1][1] :1 B[1][2] :2 Sum array is: 6 8 7 3 3 5 Product array is: 8 15 12 2 2 6
- Related Articles
- Explain pointers and one-dimensional array in C language
- Explain pointers and two-dimensional array in C language
- Explain the concept of Uninitialized array accessing in C language
- What is a one-dimensional array in C language?
- Explain the concept of pointers in C language
- Explain the concept of Sorting in C language
- Explain the concept of stack in C language
- What is a two-dimensional array in C language?
- Explain the concept of logical and assignment operator in C language
- Explain the concept of pointer accessing in C language
- Explain the concept of Arithmetic operators in C language
- Explain the concept of Linked list in C language
- Split one-dimensional array into two-dimensional array JavaScript
- Explain bit field in C language by using structure concept
- Difference Between One-Dimensional (1D) and Two-Dimensional (2D) Array

Advertisements