
- Learn C By Examples Time
- Learn C by Examples - Home
- C Examples - Simple Programs
- C Examples - Loops/Iterations
- C Examples - Patterns
- C Examples - Arrays
- C Examples - Strings
- C Examples - Mathematics
- C Examples - Linked List
- C Programming Useful Resources
- Learn C By Examples - Quick Guide
- Learn C By Examples - Resources
- Learn C By Examples - Discussion
Array Operations In C
Arrays are collection of similar data types. They are arranged linearly and can be accessed sequentially by referring to next memory space. A
Simple 3x3 Matrix Printing
#include <stdio.h> int main() { int i,j; for(i = 1; i <= 3; i++) { for(j = 1; j <= 3; j++) printf("1 "); printf("\n"); } return 0; }
The output should look like this −
1 1 1 1 1 1 1 1 1
Zero Matrix Printing
#include <stdio.h> int main() { int i,j; for(i = 1; i <= 3; i++) { for(j = 1; j <= 3; j++) printf("0 "); printf("\n"); } return 0; }
The output should look like this −
0 0 0 0 0 0 0 0 0
Unit Matrix Printing
#include <stdio.h> int main() { int i,j; for(i = 1; i <= 3; i++) { for(j = 1; j <= 3; j++) { if(i == j) printf("1 "); else printf("0 "); } printf("\n"); } return 0; }
The output should look like this −
1 0 0 0 1 0 0 0 1
Reverse Diagonal Matrix
#include <stdio.h> int main() { int i,j; for(i=1;i<=3;i++) { for(j=1;j<=3;j++) { if(i+j==3+1) printf("1 "); else printf("0 "); } printf("\n"); } return 0; }
The output should look like this −
0 0 1 0 1 0 1 0 0
Advertisements