0% found this document useful (0 votes)
2 views26 pages

Array Examples

The document provides various C programming examples related to arrays and matrices, including insertion methods, matrix addition, and multiplication. It also covers sorting algorithms (bubble, selection, and insertion sort), searching techniques (linear and binary search), and conversions between decimal and binary numbers. Additionally, it includes tasks such as finding the highest and lowest numbers in an array, generating Fibonacci series, and printing patterns.

Uploaded by

jayaburman0
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views26 pages

Array Examples

The document provides various C programming examples related to arrays and matrices, including insertion methods, matrix addition, and multiplication. It also covers sorting algorithms (bubble, selection, and insertion sort), searching techniques (linear and binary search), and conversions between decimal and binary numbers. Additionally, it includes tasks such as finding the highest and lowest numbers in an array, generating Fibonacci series, and printing patterns.

Uploaded by

jayaburman0
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ARRAY Examples

Insertion at Beginning, End, and Middle


#include <stdio.h> // Display original array
printf("\nOriginal array: ");
int main() { for (i = 0; i < n; i++)
int arr[100]; printf("%d ", arr[i]);
int n, i, pos, value;

// Input number of elements


printf("Enter number of elements: ");
scanf("%d", &n);

// Input array elements


printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
Insertion at Beginning, End, and Middle
printf("Enter value to insert: "); printf("Enter position (1 to %d): ", n + 1);
scanf("%d", &value); scanf("%d", &pos);
// Shift all elements right printf("Enter value to insert: ");
for (i = n - 1; i >= 0; i--) scanf("%d", &value);
arr[i + 1] = arr[i];
arr[0] = value; // insert at beginning // Shift elements to right from position
n++; for (i = n - 1; i >= pos - 1; i--)
arr[i + 1] = arr[i];
arr[pos - 1] = value; // insert at given position
printf("Enter value to insert: "); n++;
scanf("%d", &value);
arr[n] = value; // insert at end
n++;
Write a program to Print a Matrix
int main() {
int A[10][10];
int m, n, i, j;

// Input number of rows and columns


printf("Enter number of rows and columns: ");
scanf("%d %d", &m, &n);

// Input elements of the matrix


printf("Enter elements of matrix A (%d x %d):\n", m, n);
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &A[i][j]);
}
}
Write a program to Add Two Matrices
// Add the two matrices
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
• Print series n, n-1, n-2, . . . , 3, 2, 1
• Print series 2, 4, 6, 8, . . . n
• Fibonacci Series
• Sum of digits
• Armstrong number
• Prime number
• Sum of series 1 to n * *
• Pattern Printing *
* * * *
* * * * * * * * *
* * * * * * * * * * * **
Print the triangle: *
* *
int rows = 4;
* * *
// This loop for traverse pyramid from top to bottom
for (int i = 1; i <= rows; i++) * * * *
{
// This loop for printing leading whitespaces
// This loop for printing * character in each row
for (int j = 1; j < =(rows - i) ; j++) for (int k = 1; k <= i; k++)
{ {
printf("* ");
printf(" "); }
} printf("\n");
}
return 0;
}
Print the triangle: *
* * *
#include <stdio.h> * * * **
void main(){
{
int rows = 3; // Inner loop 2 to print star (*) character for each row
for (int k = 1; k <= (2*i)- 1; k++)
// This loop to print all rows
{
for (int i = 1; i < =rows; i++) printf("* ");
{ }
printf("\n");
// Inner loop 1 to print white spaces for each row
}
for (int j = 1; j < =(rows - i) ; j++) }
{
printf(" ");
}
• Find out the highest and lowest numbers in an array of n numbers.
• Write a program to search any number using Linear Search method.
• Write a program to search any number using Binary Search method.
• Sorting of an array in ascending order (using bubble sort method).
• Sorting of an array in ascending order (using selection sort method).
• Sorting of an array in ascending order (using insertion sort method).
• Convert decimal number to its equivalent binary number.
• Convert binary number to its equivalent decimal number.
• Write a program for matrix multiplication.
• Find the transpose of a given matrix.
Find out the highest and lowest numbers in
an array of n numbers.
#include <stdio.h> max = min = a[0];
void main() { for(i = 1; i < n; i++) {
int n, i, max, min; if(a[i] > max)
printf("Enter number of elements: "); max = a[i];
scanf("%d", &n); if(a[i] < min)
int a[n]; min = a[i];
printf("Enter %d numbers:\n", n); }
for(i = 0; i < n; i++) printf("Highest = %d\nLowest = %d\n", max, min);
scanf("%d", &a[i]); }
Write a program to search any number using
Linear Search method.
printf("Enter number to search: ");
#include <stdio.h>
scanf("%d", &key);
int main() {
int n, i, key, found = 0;
for(i = 0; i < n; i++) {
printf("Enter number of elements: ");
if(a[i] == key) {
scanf("%d", &n);
printf("Element found at position %d\n", i + 1);
int a[n];
found = 1;
printf("Enter %d elements:\n", n);
break;
for(i = 0; i < n; i++)
}
scanf("%d", &a[i]);
}
if(!found)
printf("Element not found\n");
return 0;
}
Write a program to search any number using
Binary Search method.
while(low <= high) {
#include <stdio.h>
mid = (low + high) / 2;
int main() {
if(a[mid] == key) {
int n, i, key, low, high, mid, found = 0;
printf("Element found at position %d\n", mid + 1);
printf("Enter number of elements: ");
found = 1;
scanf("%d", &n);
break;
int a[n];
} else if(a[mid] < key)
printf("Enter %d sorted elements:\n", n);
low = mid + 1;
for(i = 0; i < n; i++)
else
scanf("%d", &a[i]);
high = mid - 1;
}
printf("Enter element to search: ");
if(!found)
scanf("%d", &key);
printf("Element not found\n");
return 0;
low = 0; high = n - 1;
}
Sorting of an array in ascending order (using
bubble sort method).
for(i = 0; i < n - 1; i++) {
#include <stdio.h>
for(j = 0; j < n - i - 1; j++) {
int main() {
if(a[j] > a[j + 1]) {
int n, i, j, temp;
temp = a[j];
printf("Enter number of elements: ");
a[j] = a[j + 1];
scanf("%d", &n);
a[j + 1] = temp;
int a[n];
}
printf("Enter %d numbers:\n", n);
}
for(i = 0; i < n; i++)
}
scanf("%d", &a[i]);
printf("Sorted array: ");
for(i = 0; i < n; i++)
printf("%d ", a[i]);
return 0;
}
Sorting of an array in ascending order (using
selection sort method).
for(i = 0; i < n - 1; i++) {
#include <stdio.h>
min = i;
int main() {
for(j = i + 1; j < n; j++) {
int n, i, j, min, temp;
if(a[j] < a[min])
printf("Enter number of elements: ");
min = j;
scanf("%d", &n);
}
int a[n];
temp = a[i];
printf("Enter %d elements:\n", n);
a[i] = a[min];
for(i = 0; i < n; i++)
a[min] = temp;
scanf("%d", &a[i]);
}
printf("Sorted array: ");
for(i = 0; i < n; i++)
printf("%d ", a[i]);
return 0;
}
Sorting of an array in ascending order (using
insertion sort method)
#include <stdio.h> for(i = 1; i < n; i++) {
int main() { key = a[i];
int n, i, j, key; j = i - 1;
printf("Enter number of elements: "); while(j >= 0 && a[j] > key) {
scanf("%d", &n); a[j + 1] = a[j];
int a[n]; j--;
printf("Enter %d numbers:\n", n); }
for(i = 0; i < n; i++) a[j + 1] = key;
scanf("%d", &a[i]); }

printf("Sorted array: ");


for(i = 0; i < n; i++)
printf("%d ", a[i]);
return 0;
}
Convert decimal number to its equivalent
binary number.
Divide the decimal number by 2.
Store the remainder (0 or 1).
Repeat the division using the quotient until it becomes 0.
Read remainders backward → gives the binary equivalent.
Convert decimal number to its equivalent
binary number.
#include <stdio.h> printf("Binary equivalent: ");
int main() { for(i = i - 1; i >= 0; i--)
int n, bin[32], i = 0; printf("%d", bin[i]);
printf("Enter a decimal number: "); return 0;
scanf("%d", &n); }

while(n > 0) {
bin[i] = n % 2;
n = n / 2;
i++;
}
Convert binary number to its equivalent
decimal number.
 The binary number system has base 2, meaning each digit (bit)
represents a power of 2.
To convert binary to decimal, we multiply each binary digit by 2
raised to the power of its position, counting positions from right to
left (starting at 0).
Then, we add all these values together.
Convert binary number to its equivalent
decimal number.
while(bin > 0) {
#include <stdio.h>
rem = bin % 10;
#include <math.h>
dec = dec + rem * base;
int main() {
base = base * 2;
long long bin;
bin = bin / 10;
int dec = 0, base = 1, rem;
}
printf("Enter a binary number: ");
scanf("%lld", &bin);
printf("Decimal equivalent: %d", dec);
return 0;
}
Write a program for matrix multiplication.
Write a program for matrix multiplication.

for(i = 0; i < m; i++) { // Row of A


for(j = 0; j < p; j++) { // Column of B
C[i][j] = 0; // Initialize result element

for(k = 0; k < n; k++) { // Common dimension


C[i][j] += A[i][k] * B[k][j];
}
}
}
Write a program for matrix multiplication.
#include <stdio.h>
int main() {
int A[10][10], B[10][10], C[10][10];
int m, n, p, q;
int i, j, k;
// Input matrix sizes
printf("Enter rows and columns for first matrix: ");
scanf("%d %d", &m, &n);
printf("Enter rows and columns for second matrix: ");
scanf("%d %d", &p, &q);
// Check if multiplication is possible
if (n != p) {
printf("Matrix multiplication not possible! (columns of A ≠ rows of B)\n");
return 0;
}
Write a program for matrix multiplication.
// Input matrix A // Matrix multiplication logic
printf("Enter elements of first matrix (A):\n"); for (i = 0; i < m; i++) {
for (i = 0; i < m; i++) for (j = 0; j < q; j++) {
for (j = 0; j < n; j++) for (k = 0; k < n; k++) {
scanf("%d", &A[i][j]); C[i][j] += A[i][k] * B[k][j];
}
// Input matrix B }
printf("Enter elements of second matrix (B):\n"); }
for (i = 0; i < p; i++) // Display result
for (j = 0; j < q; j++) printf("Resultant Matrix (A × B):\n");
scanf("%d", &B[i][j]); for (i = 0; i < m; i++) {
for (j = 0; j < q; j++) {
// Initialize result matrix to 0 printf("%d\t", C[i][j]);
for (i = 0; i < m; i++) }
for (j = 0; j < q; j++) printf("\n");
C[i][j] = 0; }
return 0;
} } 0;
Find the transpose of a given matrix.
Find the transpose of a given matrix.

for(i = 0; i < m; i++) {


for(j = 0; j < n; j++) {
T[j][i] = A[i][j]; // swap row & column indices
}
}
Find the transpose of a given matrix.
for(i = 0; i < r; i++)
#include <stdio.h>
for(j = 0; j < c; j++)
int main() {
t[j][i] = a[i][j];
int a[10][10], t[10][10], r, c, i, j;
printf("Enter rows and columns: ");
printf("Transpose of matrix:\n");
scanf("%d%d", &r, &c);
for(i = 0; i < c; i++) {
for(j = 0; j < r; j++)
printf("Enter matrix elements:\n");
printf("%d ", t[i][j]);
for(i = 0; i < r; i++)
printf("\n");
for(j = 0; j < c; j++)
}
scanf("%d", &a[i][j]);
return 0;
}

You might also like