C Programming Examples
-R.Sandhiya,
AP/CIT
Arrays
2
1. Example to print
reverse of n numbers
3
“
#include <stdio.h>
void main()
{
int i,n,a[100];
printf("nnRead n number of values in an array and display it in
reverse order:n");
printf("Input the number of elements to store in the array :");
scanf("%d",&n);
printf("Input %d number of elements in the array :n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&a[i]);
}
4
“ printf("nThe values stored into the array are : n");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
printf("nnThe values store into the array in reverse are :n");
for(i=n-1;i>=0;i--)
{
printf("%d",a[i]);
}
printf("nn");
}
5
2. Example to separate
odd and even integers
and display in separate
arrays
6
7
8
#include <stdio.h>
void main()
{
int arr1[10], arr2[10], arr3[10];
int i,j=0,k=0,n;
printf("Input the number of elements to be stored in the array :");
scanf("%d",&n);
printf("Input %d elements in the array :n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
9
for(i=0;i<n;i++)
{
if (arr1[i]%2 == 0)
{
arr2[j] = arr1[i];
j++;
}
else
{
arr3[k] = arr1[i];
k++;
}
}
10
printf("nThe Even elements are : n");
for(i=0;i<j;i++)
{
printf("%d ",arr2[i]);
}
printf("nThe Odd elements are :n");
for(i=0;i<k;i++)
{
printf("%d ", arr3[i]);
}
printf("nn");
}
11
Input the number of elements to be stored in the array
:5
Input 5 elements in the array :
element - 0 : 25
element - 1 : 47
element - 2 : 42
element - 3 : 56
element - 4 : 32
The Even elements are :
42 56 32
The Odd elements are :
25 47
3. Example to merge and
sort numbers in
descending order
12
13
14
#include <stdio.h>
void main()
{
int arr1[100], arr2[100], arr3[200];
int s1, s2, s3;
int i, j, k;
printf("nnMerge two arrays of same size sorted in decending order.n");
printf("Input the number of elements to be stored in the first array :");
scanf("%d",&s1);
printf("Input %d elements in the array :n",s1);
for(i=0;i<s1;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
15
printf("Input the number of elements to be stored in the second array :");
scanf("%d",&s2);
printf("Input %d elements in the array :n",s2);
for(i=0;i<s2;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr2[i]);
}
/* size of merged array is size of first array and size of second array */
s3 = s1 + s2;
16
/*----------------- insert in the third array------------------------------------*/
for(i=0;i<s1; i++)
{
arr3[i] = arr1[i];
}
for(j=0;j<s2; j++)
{
arr3[i] = arr2[j];
i++;
}
17
/*----------------- sort the array in decending order ---------------------------*/
for(i=0;i<s3; i++)
{
for(k=0;k<s3-1;k++)
{
if(arr3[k]<=arr3[k+1])
{
j=arr3[k+1];
arr3[k+1]=arr3[k];
arr3[k]=j;
}
}
}
18
/*--------------- Prints the merged array ------------------------------------*/
printf("nThe merged array in decending order is :n");
for(i=0; i<s3; i++)
{
printf("%d ", arr3[i]);
}
printf("nn");
}
19
Merge two arrays of same size sorted in decending order.
Input the number of elements to be stored in the first array :3
Input 3 elements in the array :
element - 0 : 1
element - 1 : 2
element - 2 : 3
Input the number of elements to be stored in the second array :3
Input 3 elements in the array :
element - 0 : 1
element - 1 : 2
element - 2 : 3
The merged array in decending order is : 3 3 2 2 1 1
20
THANKS!
You can find me at:
▪ rsandhiya@cit.edu.in

Examples sandhiya class'

  • 1.
  • 2.
  • 3.
    1. Example toprint reverse of n numbers 3
  • 4.
    “ #include <stdio.h> void main() { inti,n,a[100]; printf("nnRead n number of values in an array and display it in reverse order:n"); printf("Input the number of elements to store in the array :"); scanf("%d",&n); printf("Input %d number of elements in the array :n",n); for(i=0;i<n;i++) { printf("element - %d : ",i); scanf("%d",&a[i]); } 4
  • 5.
    “ printf("nThe valuesstored into the array are : n"); for(i=0;i<n;i++) { printf("%d",a[i]); } printf("nnThe values store into the array in reverse are :n"); for(i=n-1;i>=0;i--) { printf("%d",a[i]); } printf("nn"); } 5
  • 6.
    2. Example toseparate odd and even integers and display in separate arrays 6
  • 7.
  • 8.
    8 #include <stdio.h> void main() { intarr1[10], arr2[10], arr3[10]; int i,j=0,k=0,n; printf("Input the number of elements to be stored in the array :"); scanf("%d",&n); printf("Input %d elements in the array :n",n); for(i=0;i<n;i++) { printf("element - %d : ",i); scanf("%d",&arr1[i]); }
  • 9.
    9 for(i=0;i<n;i++) { if (arr1[i]%2 ==0) { arr2[j] = arr1[i]; j++; } else { arr3[k] = arr1[i]; k++; } }
  • 10.
    10 printf("nThe Even elementsare : n"); for(i=0;i<j;i++) { printf("%d ",arr2[i]); } printf("nThe Odd elements are :n"); for(i=0;i<k;i++) { printf("%d ", arr3[i]); } printf("nn"); }
  • 11.
    11 Input the numberof elements to be stored in the array :5 Input 5 elements in the array : element - 0 : 25 element - 1 : 47 element - 2 : 42 element - 3 : 56 element - 4 : 32 The Even elements are : 42 56 32 The Odd elements are : 25 47
  • 12.
    3. Example tomerge and sort numbers in descending order 12
  • 13.
  • 14.
    14 #include <stdio.h> void main() { intarr1[100], arr2[100], arr3[200]; int s1, s2, s3; int i, j, k; printf("nnMerge two arrays of same size sorted in decending order.n"); printf("Input the number of elements to be stored in the first array :"); scanf("%d",&s1); printf("Input %d elements in the array :n",s1); for(i=0;i<s1;i++) { printf("element - %d : ",i); scanf("%d",&arr1[i]); }
  • 15.
    15 printf("Input the numberof elements to be stored in the second array :"); scanf("%d",&s2); printf("Input %d elements in the array :n",s2); for(i=0;i<s2;i++) { printf("element - %d : ",i); scanf("%d",&arr2[i]); } /* size of merged array is size of first array and size of second array */ s3 = s1 + s2;
  • 16.
    16 /*----------------- insert inthe third array------------------------------------*/ for(i=0;i<s1; i++) { arr3[i] = arr1[i]; } for(j=0;j<s2; j++) { arr3[i] = arr2[j]; i++; }
  • 17.
    17 /*----------------- sort thearray in decending order ---------------------------*/ for(i=0;i<s3; i++) { for(k=0;k<s3-1;k++) { if(arr3[k]<=arr3[k+1]) { j=arr3[k+1]; arr3[k+1]=arr3[k]; arr3[k]=j; } } }
  • 18.
    18 /*--------------- Prints themerged array ------------------------------------*/ printf("nThe merged array in decending order is :n"); for(i=0; i<s3; i++) { printf("%d ", arr3[i]); } printf("nn"); }
  • 19.
    19 Merge two arraysof same size sorted in decending order. Input the number of elements to be stored in the first array :3 Input 3 elements in the array : element - 0 : 1 element - 1 : 2 element - 2 : 3 Input the number of elements to be stored in the second array :3 Input 3 elements in the array : element - 0 : 1 element - 1 : 2 element - 2 : 3 The merged array in decending order is : 3 3 2 2 1 1
  • 20.
    20 THANKS! You can findme at: ▪ rsandhiya@cit.edu.in