SlideShare uma empresa Scribd logo
1 de 14
GET 100% MARKS IN
COMPUTER SCIENCE
2-D ARRAY FUNCTION
WRITING QUESTION
EXAM WEIGHTAGE-3/4MARKS
VIDEO 1
FUNCTION HEADER
Void nameof function(int arr[][5], int r,int c)
{
//write your function code here
}
Argument 1 : int arr[][5] for passing 2-d array to
function (first index(row index) no. will be left
blank but it is mandatory to pass the second
index(col index) no. )
Argument 2 : int r to pass the row size of 2d array
Argument 3 : int c to pass the column size of 2d
array
TYPE OF QUESTION
1. To calculate sum of particular elements of a 2d array
2. To print particular elements of a 2 d array
3. To update a 2d array in a particular manner
4. To swap/shift the elements of a 2d array in a particular manner
LOGIC FOR PRINTING A 2D ARRAY
Void PrintArray(int arr[][4],int r, int c)
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<arr[i][j]<<“t”;
}
cout<<“n”;
}
}
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
OUTPUT
LOGIC FOR CALCULATING SUM OF A 2D ARRAY
Void PrintArray(int arr[][4],int r, int c)
{
int sum=0;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
sum=sum+ arr[i][j];
}
}
cout<<“Sum of matrix elements : ”<<sum;
}
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
OUTPUT
Sum of matrix elements : 136
SQUARE MATRIX
00 01 02 03
10 11 12 13
20 21 22 23
30 31 32 33
Square matrix : in which no of row(r) = no of col(c)
1.)Main Diagonal : arr[0][0], arr[1][1], arr[2][2], arr[3][3]
Condition for main diagonal : (i==j)
Off diagonal: arr[0][3], arr[1][2], arr[2][1], arr[3][0]
Condition for off diagonal : (i+j)==r-1 / (i+j)==c-1
Upper triangle : (above main diagonal)
condition (i<=j)
Lower Triangle: (below main diagonal)
condition(i>=j)
Main diagonalOFF Diagonal
1.) Write a function printdiagonal(int arr[][4],int r,int c) to print
diagonals of a 2d array
Void printdiagonal(int arr[][4],int r,int c)
{
cout<<“Diagonal ONE : ”;
for(int i=0;i<r;i++)
{
cout<<arr[i][i]<<“t”;
}
cout<<“nDiagonal TWO :”;
for(int i=0;i<r;i++)
{
Cout<<arr[i][r-1-i];
}
}
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
OUTPUT:
DIAGONAL ONE : 1 6 11 12
DIAGONAL TWO : 4 7 10 13
2.) Write a function printdsum(int arr[][4],int r,int c) to print
sum of diagonals of a 2d array
Void printdsum(int arr[][4],int r,int c)
{
int d1sum=0,d2sum=0;
for(int i=0;i<r;i++)
{
d1sum=d1sum+arr[i][j];
}
for(int i=0;i<r;i++)
{
d2sum=d2sum+arr[i][r-1-i];
}
cout<<“Main Diagonal Sum : ”<<d1sum;
cout<<“nOff Diagonal Sum : ”<<d2sum;
}
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
OUTPUT:
Main Diagonal Sum : 34
Off Diagonal Sum : 34
3.) Write a function printupper(int arr[][5],int r,int c) to print Upper
triangle elements of a 2d array
Void printdiagonal(int arr[][5],int r,int c)
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(i<=j)
cout<<arr[i][j]<<“t”;
else
cout<<“t”;
}
cout<<“n”;
}
}
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
OUTPUT:
1 2 3 4
6 7 8
11 12
16
4.)Write a function printlower(int arr[][5],int r,int c) to print
Upper triangle elements of a 2d array
Void printdiagonal(int arr[][4],int r,int c)
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(i>=j)
cout<<arr[i][j]<<“t”;
else
cout<<“t”;
}
cout<<“n”;
}
}
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
OUTPUT:
1
5 6
9 10 11
13 14 15 16
5.) Write a function printalternate(int arr[][5],int r,int c)
to print Upper triangle elements of a 2d array
Void printdiagonal(int arr[][4],int r,int c)
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(i+j%2==0)
cout<<arr[i][j]<<“t”;
else
cout<<“t”;
}
cout<<“n”;
}
}
Condition :
i+j = even
00 01 02
10 11 12
20 21 22
30 31 32
00 02
11
20 22
31
PRACTICE QUESTION
Q.1) Write a function printuppersum(int arr[][5],int r,int c) to calculate and print sum
Upper triangle elements of a 2d array
Q.2) Write a function printlowersum(int arr[][5],int r,int c) to calculate and print sum of lower triangle
elements of a 2d array
Q.3) Write a function printalternatesum(int arr[][5],int r,int c) to calculate and print sum of alternate
elements of a 2d array
THANKS FOR WATCHING MY VIDEO
EMAIL : theaakashkumar@gmail.com

Mais conteúdo relacionado

Mais procurados

Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
Ahmed Khateeb
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
PTCL
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
ritu1806
 

Mais procurados (19)

Circular queues
Circular queuesCircular queues
Circular queues
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
 
Permute
PermutePermute
Permute
 
Tail Recursion in data structure
Tail Recursion in data structureTail Recursion in data structure
Tail Recursion in data structure
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
 
Queue implementation
Queue implementationQueue implementation
Queue implementation
 
A quick introduction to R
A quick introduction to RA quick introduction to R
A quick introduction to R
 
Removal Of Recursion
Removal Of RecursionRemoval Of Recursion
Removal Of Recursion
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
 
Permute
PermutePermute
Permute
 
Matlab integration
Matlab integrationMatlab integration
Matlab integration
 
Array notes
Array notesArray notes
Array notes
 
Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions
 
openMP loop parallelization
openMP loop parallelizationopenMP loop parallelization
openMP loop parallelization
 
Functions
FunctionsFunctions
Functions
 
Stack Data Structure V1.0
Stack Data Structure V1.0Stack Data Structure V1.0
Stack Data Structure V1.0
 
Stack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxStack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptx
 

Semelhante a 2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS

2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
AAKASH KUMAR
 
#include iostream using namespace std; void InsertionSort(int .pdf
#include iostream using namespace std; void InsertionSort(int .pdf#include iostream using namespace std; void InsertionSort(int .pdf
#include iostream using namespace std; void InsertionSort(int .pdf
brijmote
 
c++ program I need to sort arrays using an insertion sort and a mer.pdf
c++ program I need to sort arrays using an insertion sort and a mer.pdfc++ program I need to sort arrays using an insertion sort and a mer.pdf
c++ program I need to sort arrays using an insertion sort and a mer.pdf
dhavalbl38
 
I have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdfI have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdf
shreeaadithyaacellso
 
Function recap
Function recapFunction recap
Function recap
alish sha
 
Function recap
Function recapFunction recap
Function recap
alish sha
 

Semelhante a 2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS (20)

2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
 
Arrays
ArraysArrays
Arrays
 
array2d.ppt
array2d.pptarray2d.ppt
array2d.ppt
 
#include iostream using namespace std; void InsertionSort(int .pdf
#include iostream using namespace std; void InsertionSort(int .pdf#include iostream using namespace std; void InsertionSort(int .pdf
#include iostream using namespace std; void InsertionSort(int .pdf
 
c++ program I need to sort arrays using an insertion sort and a mer.pdf
c++ program I need to sort arrays using an insertion sort and a mer.pdfc++ program I need to sort arrays using an insertion sort and a mer.pdf
c++ program I need to sort arrays using an insertion sort and a mer.pdf
 
ماترێکس به‌ کوردی ئارام
ماترێکس به‌ کوردی ئارامماترێکس به‌ کوردی ئارام
ماترێکس به‌ کوردی ئارام
 
I have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdfI have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdf
 
Computer Programming- Lecture 9
Computer Programming- Lecture 9Computer Programming- Lecture 9
Computer Programming- Lecture 9
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for Speed
 
Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output) Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)
 
Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)
 
Function recap
Function recapFunction recap
Function recap
 
Function recap
Function recapFunction recap
Function recap
 
Managing console
Managing consoleManaging console
Managing console
 
Vcs16
Vcs16Vcs16
Vcs16
 
2.1 ### uVision Project, (C) Keil Software .docx
2.1   ### uVision Project, (C) Keil Software    .docx2.1   ### uVision Project, (C) Keil Software    .docx
2.1 ### uVision Project, (C) Keil Software .docx
 
Qno 3 (a)
Qno 3 (a)Qno 3 (a)
Qno 3 (a)
 
Advanced C - Part 3
Advanced C - Part 3Advanced C - Part 3
Advanced C - Part 3
 
C Code and the Art of Obfuscation
C Code and the Art of ObfuscationC Code and the Art of Obfuscation
C Code and the Art of Obfuscation
 
chapter1.ppt
chapter1.pptchapter1.ppt
chapter1.ppt
 

Mais de AAKASH KUMAR

Mais de AAKASH KUMAR (20)

NETWORKING AND COMMUNICATION || SLIDE 1 || TOPOLOGY AND PLACEMENT OF DEVICES|...
NETWORKING AND COMMUNICATION || SLIDE 1 || TOPOLOGY AND PLACEMENT OF DEVICES|...NETWORKING AND COMMUNICATION || SLIDE 1 || TOPOLOGY AND PLACEMENT OF DEVICES|...
NETWORKING AND COMMUNICATION || SLIDE 1 || TOPOLOGY AND PLACEMENT OF DEVICES|...
 
Inheritance question
Inheritance questionInheritance question
Inheritance question
 
Header file BASED QUESTION- CBSE CS CLASS 12TH
Header file BASED QUESTION- CBSE CS CLASS 12THHeader file BASED QUESTION- CBSE CS CLASS 12TH
Header file BASED QUESTION- CBSE CS CLASS 12TH
 
Constructor & destructor based question- cbse cs class 12th
Constructor & destructor based question-  cbse cs class 12thConstructor & destructor based question-  cbse cs class 12th
Constructor & destructor based question- cbse cs class 12th
 
CHOOSE THE CORRECT IDENTIFIER - Q.1 CBSE CS EXAM
CHOOSE THE CORRECT IDENTIFIER - Q.1 CBSE CS EXAMCHOOSE THE CORRECT IDENTIFIER - Q.1 CBSE CS EXAM
CHOOSE THE CORRECT IDENTIFIER - Q.1 CBSE CS EXAM
 
Practical exam special- CBSE CS CLASS 12th
Practical exam special- CBSE CS CLASS 12thPractical exam special- CBSE CS CLASS 12th
Practical exam special- CBSE CS CLASS 12th
 
Inheritance question class 12th
Inheritance question class 12thInheritance question class 12th
Inheritance question class 12th
 
Ms word Part 2
Ms  word Part 2Ms  word Part 2
Ms word Part 2
 
Ms word Part 1
Ms  word Part 1Ms  word Part 1
Ms word Part 1
 
Power point2007instruction
Power point2007instructionPower point2007instruction
Power point2007instruction
 
Html introduction Part-2
Html introduction Part-2Html introduction Part-2
Html introduction Part-2
 
Html Slide Part-1
Html Slide Part-1Html Slide Part-1
Html Slide Part-1
 
Evolution / history of Computer
Evolution / history of ComputerEvolution / history of Computer
Evolution / history of Computer
 
computer system
computer system computer system
computer system
 
Array within a class
Array within a classArray within a class
Array within a class
 
Input Output Devices and Memory Unit
Input Output Devices and Memory UnitInput Output Devices and Memory Unit
Input Output Devices and Memory Unit
 
C++ programming Unit 5 flow of control
C++ programming Unit 5 flow of controlC++ programming Unit 5 flow of control
C++ programming Unit 5 flow of control
 
c++ programming Unit 4 operators
c++ programming Unit 4 operatorsc++ programming Unit 4 operators
c++ programming Unit 4 operators
 
c++ programming Unit 3 variables,data types
c++ programming Unit 3 variables,data typesc++ programming Unit 3 variables,data types
c++ programming Unit 3 variables,data types
 
c++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ programc++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ program
 

Último

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Último (20)

PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 

2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS

  • 1.
  • 2. GET 100% MARKS IN COMPUTER SCIENCE 2-D ARRAY FUNCTION WRITING QUESTION EXAM WEIGHTAGE-3/4MARKS VIDEO 1
  • 3. FUNCTION HEADER Void nameof function(int arr[][5], int r,int c) { //write your function code here } Argument 1 : int arr[][5] for passing 2-d array to function (first index(row index) no. will be left blank but it is mandatory to pass the second index(col index) no. ) Argument 2 : int r to pass the row size of 2d array Argument 3 : int c to pass the column size of 2d array
  • 4. TYPE OF QUESTION 1. To calculate sum of particular elements of a 2d array 2. To print particular elements of a 2 d array 3. To update a 2d array in a particular manner 4. To swap/shift the elements of a 2d array in a particular manner
  • 5. LOGIC FOR PRINTING A 2D ARRAY Void PrintArray(int arr[][4],int r, int c) { for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { cout<<arr[i][j]<<“t”; } cout<<“n”; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 OUTPUT
  • 6. LOGIC FOR CALCULATING SUM OF A 2D ARRAY Void PrintArray(int arr[][4],int r, int c) { int sum=0; for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { sum=sum+ arr[i][j]; } } cout<<“Sum of matrix elements : ”<<sum; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 OUTPUT Sum of matrix elements : 136
  • 7. SQUARE MATRIX 00 01 02 03 10 11 12 13 20 21 22 23 30 31 32 33 Square matrix : in which no of row(r) = no of col(c) 1.)Main Diagonal : arr[0][0], arr[1][1], arr[2][2], arr[3][3] Condition for main diagonal : (i==j) Off diagonal: arr[0][3], arr[1][2], arr[2][1], arr[3][0] Condition for off diagonal : (i+j)==r-1 / (i+j)==c-1 Upper triangle : (above main diagonal) condition (i<=j) Lower Triangle: (below main diagonal) condition(i>=j) Main diagonalOFF Diagonal
  • 8. 1.) Write a function printdiagonal(int arr[][4],int r,int c) to print diagonals of a 2d array Void printdiagonal(int arr[][4],int r,int c) { cout<<“Diagonal ONE : ”; for(int i=0;i<r;i++) { cout<<arr[i][i]<<“t”; } cout<<“nDiagonal TWO :”; for(int i=0;i<r;i++) { Cout<<arr[i][r-1-i]; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 OUTPUT: DIAGONAL ONE : 1 6 11 12 DIAGONAL TWO : 4 7 10 13
  • 9. 2.) Write a function printdsum(int arr[][4],int r,int c) to print sum of diagonals of a 2d array Void printdsum(int arr[][4],int r,int c) { int d1sum=0,d2sum=0; for(int i=0;i<r;i++) { d1sum=d1sum+arr[i][j]; } for(int i=0;i<r;i++) { d2sum=d2sum+arr[i][r-1-i]; } cout<<“Main Diagonal Sum : ”<<d1sum; cout<<“nOff Diagonal Sum : ”<<d2sum; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 OUTPUT: Main Diagonal Sum : 34 Off Diagonal Sum : 34
  • 10. 3.) Write a function printupper(int arr[][5],int r,int c) to print Upper triangle elements of a 2d array Void printdiagonal(int arr[][5],int r,int c) { for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { if(i<=j) cout<<arr[i][j]<<“t”; else cout<<“t”; } cout<<“n”; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 OUTPUT: 1 2 3 4 6 7 8 11 12 16
  • 11. 4.)Write a function printlower(int arr[][5],int r,int c) to print Upper triangle elements of a 2d array Void printdiagonal(int arr[][4],int r,int c) { for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { if(i>=j) cout<<arr[i][j]<<“t”; else cout<<“t”; } cout<<“n”; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 OUTPUT: 1 5 6 9 10 11 13 14 15 16
  • 12. 5.) Write a function printalternate(int arr[][5],int r,int c) to print Upper triangle elements of a 2d array Void printdiagonal(int arr[][4],int r,int c) { for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { if(i+j%2==0) cout<<arr[i][j]<<“t”; else cout<<“t”; } cout<<“n”; } } Condition : i+j = even 00 01 02 10 11 12 20 21 22 30 31 32 00 02 11 20 22 31
  • 13. PRACTICE QUESTION Q.1) Write a function printuppersum(int arr[][5],int r,int c) to calculate and print sum Upper triangle elements of a 2d array Q.2) Write a function printlowersum(int arr[][5],int r,int c) to calculate and print sum of lower triangle elements of a 2d array Q.3) Write a function printalternatesum(int arr[][5],int r,int c) to calculate and print sum of alternate elements of a 2d array
  • 14. THANKS FOR WATCHING MY VIDEO EMAIL : theaakashkumar@gmail.com