search
HomeBackend DevelopmentC++In C programming, working with 2D arrays at runtime

In C programming, working with 2D arrays at runtime

Question

Write a C program that uses runtime compilation to calculate the sum and product of all elements in a two-dimensional array.

Solution

  • Run-time compilation or initialization is also known as dynamic allocation. Allocating memory at execution time (runtime) is called dynamic memory allocation.

  • The functions calloc() and malloc() support dynamic memory allocation.

  • The functions calloc() and malloc() support dynamic memory allocation. p>

In this program, we will calculate the sum of all elements and the product of all elements of a 2D array at runtime.

Logic used to calculate the sum of all elements in a two-dimensional array-

printf("Sum array is : </p><p>");
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("</p><p>");
}

Logic used to calculate the product of all elements in a two-dimensional array-

printf("Product array is : </p><p>");
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("</p><p>");
}
}

Example

Example demonstration

#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&#39;s A and B using for loop//
   printf("Enter elements into the array A: </p><p>");
   for(i=0;i<2;i++){
      for(j=0;j<3;j++){
         printf("A[%d][%d] :",i,j);
         scanf("%d",&A[i][j]);
      }
      printf("</p><p>");
   }
   for(i=0;i<2;i++){
      for(j=0;j<3;j++){
         printf("B[%d][%d] :",i,j);
         scanf("%d",&B[i][j]);
      }
      printf("</p><p>");
   }
   //Calculating sum and printing output//
   printf("Sum array is : </p><p>");
   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("</p><p>");
   }
   //Calculating product and printing output//
   printf("Product array is : </p><p>");
   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("</p><p>");
   }
}

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

The above is the detailed content of In C programming, working with 2D arrays at runtime. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
使用C++编写代码,找到第N个非平方数使用C++编写代码,找到第N个非平方数Aug 30, 2023 pm 10:41 PM

我们都知道不是任何数字的平方的数字,如2、3、5、7、8等。非平方数有N个,不可能知道每个数字。因此,在本文中,我们将解释有关无平方数或非平方数的所有内容,以及在C++中查找第N个非平方数的方法。第N个非平方数如果一个数是整数的平方,则该数被称为完全平方数。完全平方数的一些例子是-1issquareof14issquareof29issquareof316issquareof425issquareof5如果一个数不是任何整数的平方,则该数被称为非平方数。例如,前15个非平方数是-2,3,5,6,

使用C++编写的数组右旋转的反转算法使用C++编写的数组右旋转的反转算法Sep 08, 2023 pm 08:17 PM

在本文中,我们将了解逆转算法,将给定的数组向右旋转k个元素,例如&minus;Input:arr[]={4,6,2,6,43,7,3,7},k=4Output:{43,7,3,7,4,6,2,6}Explanation:Rotatingeachelementofarrayby4-elementtotherightgives{43,7,3,7,4,6,2,6}.Input:arr[]={8,5,8,2,1,4,9,3},k=3Output:{4,9,3,8,5,8,2,1}寻找解决方案的方

如何使用PHP中的array_sum函数计算二维数组中某一列元素的总和如何使用PHP中的array_sum函数计算二维数组中某一列元素的总和Jun 26, 2023 pm 12:45 PM

在PHP编程中,array_sum函数是一个非常实用的函数,它可以计算数组中所有元素的总和。然而,当我们需要计算二维数组中某一列元素的总和时,可能会遇到些许麻烦。本文将介绍如何使用PHP中的array_sum函数计算二维数组中某一列元素的总和。首先,我们需要了解二维数组的概念。二维数组就是一个包含了多个数组的数组,可以把它看作是一张表格。每个数组都代表了表格

php数组二维怎么转一维数组php数组二维怎么转一维数组Aug 03, 2023 am 11:14 AM

php数组二维转一维数组的方法:1、使用循环遍历,使用循环遍历二维数组,将每个元素添加到一维数组中;2、使用“array_merge”函数,可以将多个数组合并为一个数组,将二维数组当做参数传递给“array_merge”函数,将其转换为一维数组;3、使用“array_reduce”函数,可以将数组中的所有值通过一个回调函数来进行处理,并最后返回一个结果。

在C编程中,找到一个圆的面积在C编程中,找到一个圆的面积Aug 25, 2023 pm 10:57 PM

圆是封闭图形。圆上的所有点到圆内一点的距离都相等。中心点称为圆心。点到圆心的距离称为半径。面积是封闭图形尺寸跨度的定量表示。圆的面积是圆的尺寸内包围的面积。计算圆面积的公式,Area=&pi;*r*r为了计算面积,我们给出了圆的半径作为输入,我们将使用公式来计算面积,算法STEP1:Takeradiusasinputfromtheuserusingstdinput.STEP2:Calculatetheareaofcircleusing,&nbsp;&nbsp;area=(

使用C++找到数组中唯一配对的数量使用C++找到数组中唯一配对的数量Sep 07, 2023 am 11:53 AM

我们需要适当的知识才能在C++的数组语法中创建几个唯一的对。在查找唯一对的数量时,我们计算给定数组中的所有唯一对,即可以形成所有可能的对,其中每个对应该是唯一的。例如-Input:array[]={5,5,9}Output:4Explanation:Thenumberofalluniquepairsare(5,5),(5,9),(9,5)and(9,9).Input:array[]={5,4,3,2,2}Output:16寻找解决方案的方法有两种方法可以解决这个问题,它们是&minus;

使用C++编写代码,找到具有相同最小值和最大值的子数组的数量使用C++编写代码,找到具有相同最小值和最大值的子数组的数量Aug 25, 2023 pm 11:33 PM

在本文中,我们将使用C++解决寻找最大值和最小值相同的子数组数量的问题。以下是该问题的示例&minus;Input:array={2,3,6,6,2,4,4,4}Output:12Explanation:{2},{3},{6},{6},{2},{4},{4},{4},{6,6},{4,4},{4,4}and{4,4,4}arethesubarrayswhichcanbeformedwithmaximumandminimumelementsame.Input:array={3,3,1,5,

如何使用PHP中的array_column函数获取二维数组中指定列的值如何使用PHP中的array_column函数获取二维数组中指定列的值Jun 26, 2023 pm 01:32 PM

在PHP编程中,我们常常需要对数组进行操作,包括获取指定列的值。而PHP提供了一个非常方便的函数——array_column,可以帮助我们快速获取一个二维数组中指定列的值。本文将会介绍如何使用array_column函数。array_column函数的基本用法:array_column(array$array,mixed$column_key[

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.