

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Working with two-dimensional array at runtime in C programming
Problem
Write a C program to calculate sum and product of all elements in two-dimensional array using run time compilation.
Solution
Runtime compilation or initialization is also called as dynamic allocation. Allocation of memory at the time of execution (run time) is known as dynamic memory allocation.
The functions calloc() and malloc() support allocating of dynamic memory.
In this program, we will calculate the sum of all elements and product of all elements of two-dimensional array at run time.
Logic for computing sum of all elements in 2D array −
printf("Sum array is : \n"); 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("\n"); }
Logic for computing product of all elements in 2D array −
printf("Product array is : \n"); 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("\n"); } }
Example
#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: \n"); for(i=0;i<2;i++){ for(j=0;j<3;j++){ printf("A[%d][%d] :",i,j); scanf("%d",&A[i][j]); } printf("\n"); } for(i=0;i<2;i++){ for(j=0;j<3;j++){ printf("B[%d][%d] :",i,j); scanf("%d",&B[i][j]); } printf("\n"); } //Calculating sum and printing output// printf("Sum array is : \n"); 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("\n"); } //Calculating product and printing output// printf("Product array is : \n"); 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("\n"); } }
Output
Enter elements into the array A: A[0][0] :A[0][1] :A[0][2] : A[1][0] :A[1][1] :A[1][2] : B[0][0] :B[0][1] :B[0][2] : B[1][0] :B[1][1] :B[1][2] : Sum array is : 000 000 Product array is : 000 000
- Related Questions & Answers
- C Program on two-dimensional array initialized at run time
- Split one-dimensional array into two-dimensional array JavaScript
- Passing two dimensional array to a C++ function
- How to declare a two-dimensional array in C#
- What is a two-dimensional array in C language?
- Explain pointers and two-dimensional array in C language
- Working with csv files in Python Programming
- Dimensional Array in C#?
- C# program to Loop over a two dimensional array
- How do I sort a two-dimensional array in C#
- Difference Between One-Dimensional (1D) and Two-Dimensional (2D) Array
- Display a two-dimensional array with two different nested loops in matrix form PHP?
- Transpose of a two-dimensional array - JavaScript
- How to get memory usage at runtime using C++?
- How to perform arithmetic operations on two-dimensional array in C?
Advertisements