answersLogoWhite

0


Best Answer

#include #include voidmain() { int a[10]; int i,sum=0; int *ptr; printf("Enter 10 elements:n"); for(i=0;i<10;i++) scanf("%d",&a[i]); ptr = a; /* a=&a[0] */ for(i=0;i<10;i++) { sum = sum + *ptr; //*p=content pointed by 'ptr' ptr++; } printf("The sum of array elements is %d",sum); }

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

#include<stdio.h>

#include<conio.h>

void main()

{

int arr[50][50];

int i,j,n1,n2;

printf("enter size of array no of rows-");

scanf("%d",&n1);

printf("enter size of array no of columns-");

scanf("%d",&n2);

for (i=0;i<n1;i++)

{

for (j=0;j<n2;j++)

{

printf("enter elemnt");

scanf("%d",&arr[i][j]);

}

}

getch();

}

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

#include<stdio.h>

#include<assert.h>

/* sum the given 2D array */

int sum_array (const int* arr, const unsigned int rows, const unsigned int cols) {

unsigned int x, y;

int sum;

sum = 0;

if (!arr) return sum;

for (x=0; x<rows; ++x)

for (y=0; y<cols; ++y)

sum += arr[x*cols+y];

return sum;

}

int main (void) {

const unsigned int rows=5;

const unsigned int cols=6;

int sum;

int a[rows][cols];

unsigned int x, y;

for (x=0; x<rows; ++x)

for (y=0; y<cols; ++y)

a[x][y] = (x+1)*(y+1);

sum = sum_array ((int*)a, rows, cols);

printf ("The sum of the array is: %d\n", sum);

assert(sum == 315);

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

If the arrays are fixed-length, summing them is reasonably trivial:

int a[5] = {1, 2, 3, 4, 5};

int b[5] = {10, 20, 30, 40, 50};

int c[5];

for (unsigned i=0; i<5; ++i) {

c[i] = a[i] + b[i];

}

Note that a, b, and c must all be of the same size. With fixed-length arrays it's a good idea to store the size in a constant variable:

const unsigned size = 5;

int a[size] = {1, 2, 3, 4, 5};

int b[size] = {10, 20, 30, 40, 50};

int c[size];

for (unsigned i=0; i<size; ++i) {

c[i] = a[i] + b[i];

}

Summing variable-length arrays is a bit more complex, but we can simplify with a function:

int* sum (int* a, int* b, const unsigned size) {

int* p = malloc (size * sizeof (int));

if (p == NULL) return NULL; // sanity check!

for (unsigned i=0; i<size; ++i) {

p[i] = a[i] + b[i];

}

return p;

}

The sanity check is necessary to ensure the memory was actually allocated. If successful, the caller is responsible for releasing the allocated memory:

void f (int* a, int* b, const unsigned size) {

int* p = sum (a, b, size);

if (p == NULL) return; // sanity-check!

// ...

free (p);

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a c program sum of all elements from a two dimensional (2D) array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you write a program in C to find and display the sum of each row and column of a 2 dimensional array of type float?

array type


How are arrays processed?

Usually one element at a time. If you want to process all elements of an array, you write a loop.Usually one element at a time. If you want to process all elements of an array, you write a loop.Usually one element at a time. If you want to process all elements of an array, you write a loop.Usually one element at a time. If you want to process all elements of an array, you write a loop.


Write a program to sort elements of an array in ascending order?

import java.util.Arrays; public class arraysort { public static void main(String[] a) { int array[] = { 2, 5, -2, 6, -3 }; Arrays.sort(array); for (int i : array) { System.out.println(i); } } }


How do you write a program in c plus plus to check if an array is symmetric?

To determine if an array is symmetric, the array must be square. If so, check each element against its transpose. If all elements are equal, the array is symmetric.For a two-dimensional array (a matrix) of order n, the following code will determine if it is symmetric or not:templatebool symmetric(const std::array& matrix){for (size_t r=0 ; r


How do you write a C Program to fill up an Integer Array?

Reference:cprogramming-bd.com/c_page1.aspx# array programming


Could you Write a program for 8086 microprocessor that displays on the monitor the average of 2 numbers from an array?

How to write a program for mouse in microprocessor?


How do you write a program to read set of numbers using by an array and display the ascending order of the given input numbers?

To write a C++ program to display the student details using class and array of object.


How do you write a program that accepts 50 integer values and sort them in basic programming?

Create an array with 50 elements and input the integers one a time, filling the array. Use an insertion sort on the array for each input except the first. Alternatively, input the values first and then use insertion sort.


Write a program to add array of N elements?

int sumArray(int N, int* A) { int sum = 0; do sum += A[N-1] while (N-- &gt; 1); return sum; }


How do you write an assembly language program to find the sum of n numbers using array?

write an assembly language program to find sum of N numbers


Write a c program to find the maximum value of 25 element in an array?

int findMax(int *array) { int max = array[0]; for(int i = 1; i &lt; array.length(); i++) { if(array[i] &gt; max) max = array[i] } return max; }


Write a c program to find the maximum of two numbers and print out with its position?

maxValue = function (array) {mxm = array[0];for (i=0; i&lt;array.length; i++) {if (array[i]&gt;mxm) {mxm = array[i];}}return mxm;}; i don't know