Example 2:
int a[3] = {2, 3, 5}; char ch[10] = "Data" ; float x[3] = {2.5, 3.50,489.98} ;
|
#include<stdio.h> | ||
for(i=0;i<n;i++)
| ||
#include <stdio.h> #include<conio.h>
void main() { int n, i, j, a[100], b[100];// a is original array and b is reversed array
printf("Enter the number of elements in array\n"); scanf("%d", &n); printf("Enter the array elements\n");
for (i= 0; i < n ; i++) scanf("%d", &a[i]);
/* * Copying elements into array b starting from end of array a */ for (i = n - 1, j = 0; i >= 0; i--, j++) b[j] = a[i]; /* Printing reversed array b . */ printf("Reverse array is\n");
for (i = 0; i < n; i++) printf("%d\n", a[i]);
getch(); }
|
a[0][0]
| a[0][1]
| a[0][2]
|
a[1][0]
| a[1][1]
| a[1][2]
|
a[0][0]
| a[0][1]
|
--- | a[0][n]
|
a[1][0]
| a[1][1]
|
--- | a[1][n]
|
- | -
| - | - |
a[m][0] |
a[m][1] |
|
a[m][n] |
1
| 3 | 0 |
-1
| 5 | 9 |
Eg.1 A=
|
find(values, target)
{
for( i = 0; i < values.length; i++)
{
if (values[i] == target)
{ return i; }
}
return –1;
}
If element is found this function returns index of that element otherwise it returns –1.Example : we have an array of integers, like the following:Elements | 9 | 6 | 7 | 12 | 1 | 5 |
Index | 0 | 1 | 2 | 3 | 4 | 5 |
9 | 6 | 7 | 12 | 1 | 5 |
9 | 6 | 7 | 12 | 1 | 5 |
9 | 6 | 7 | 12 | 1 | 5 |
0 | 1 | 2 | 3 | 4 | 5 |
#include<stdio.h> #include<conio.h> int search(int [],int, int); void main() { int a[20],k,i,n,m; clrscr(); printf(“\n\n ENTER THE SIZE OF ARRAY”); scanf(“%d”,&n); printf(“\n\nENTER THE ARRAY ELEMENTS\n\n”); for(i=0;i<n;i++) { | |
scanf(“%d”,&a[i]); } | |
printf(“\n\nENTER THE ELEMENT FOR SEARCH\n”); scanf(“%d”,&k); m=search(a,n,k); if(m==–1) { printf(“\n\nELEMENT IS NOT FOUND IN LIST”); } else { printf(“\n\nELEMENT IS FOUND AT LOCATION %d”,m); } } getch(); } int search(int a[],int n,int k) { int i; for(i=0;i<n;i++) { if(a[i]==k) { return i; } } if(i==n) { return –1; } | |
} | |
#include<stdio.h> #include<conio.h> void main() { int n,i,search,f=0,low,high,mid,a[20]; clrscr(); printf("Enter the number of elements:"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("Enter the number in ascending order a[%d]=",i); scanf("%d",&a[i]); } printf("Enter the element to be searched:"); scanf("%d",&search); low=0; high=n–1; while(low<=high) { mid=(low+high)/2; if(search<a[mid]) { high=mid–1; } else if(search>a[mid]) { low=mid+1; } else { | ||
f=1; printf("Element is found at the position %d:",mid); exit(); } } if(f==0) { printf("Element not found"); } getch(); } | ||
#include <stdio.h>
#include<conio.h>
void bubble_sort(int A[], int n);
int main()
{
int A[100], n, i, j, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter the elements\n", );
for (i = 0; i < n; i++)
scanf("%d", &A[i]);
bubble_sort(A, n);
printf("Sorted list in ascending order:\n");
for ( i = 0 ; i < n ; i++ )
printf("%ld\n", A[i]);
return 0;
}
void bubble_sort(int A[], int n)
{
int i, j, t;
for (i = 0 ; i < ( n – 1 ); i++)
{
for (j = 0 ; j < (n – i – 1); j++)
{
if (A[j] > list[j+1])
{
/* Swapping */
t = A[j];
A[j] = A[j+1];
A[j+1] = t;
}
}
}
}