Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Write a Function in C++ to find & display sum of rows & sum of columns of a 2 dimension array.

void SumRowCol(int A[][20], int N, int M) { for(int R=0;R<N;R++) { int SumR=0; for(int C=0;C<M;C++) SumR+=A[R][C]; cout<<"Row("<<R<<")="<<SumR<<endl; } for(int C=0;C<M;C++) { int SumC=0; for(int R=0;R<N;R++) SumC+=A[R][C]; cout<<"Column("<<C<<")="<<SumC<<endl; } } /* A = [ ] */

OUTPUT
Row(0)=20 Row(1)=23 Column(0)=15 Column(1)=12 Column(2)=16

Write a program to find sum of both left and right diagonal elements from two dimensional array.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int A[3][3], sum_left=0,sum_right=0; cout<<"Enter The Elements Of Array "; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { /* A = [ ] */

cin>>A[i][j]; } } for(int r=0;r<3;r++) { for (int c=0;c<3;c++) { if(r==c) sum_left = sum_left + A[r][c]; else if((r+c)==3) sum_right = sum_right + A[r][c]; } } cout<<"Sum Of Left Diagonal Elements="<<sum_left<<endl; cout<<"Sum Of Right Diagonal Elements= "<<sum_right; getch(); }

OUTPUT
Sum Of Left Diagonal Elements = 11 Sum Of Right Diagonal Elements = 18

Suppose an array A containing integers in unsorted order. Write a user defined function to sort them in ascending order using Bubble sort technique. The Function should have the parameters as (i) (ii) An Array Number Of Elements N

void bubbleSort(int array[],int N) { int i,j; for(i=0;i<N;i++) /*array[]={7,6,23,4,47}*/ { for(j=0;j<i;j++) { if(array[i]>array[j]) { int temp=array[i]; //swap array[i]=array[j]; array[j]=temp; } } } cout<<The Sorted Array is : <<endl; for(i=0;i<N;i++) cout<<array[i]<<|; }

OUTPUT

The Sorted Array is : 4|6|7|23|47|

You might also like