Você está na página 1de 11

Arrays applications

/*Sorting array elements */


# include <stdio.h>
# dene SIZE 10
int main()
{
int num[SIZE]={2,6,4,8,10,12,89,68,45,37};
int i,pass,hold;
/*************************************************/
//printing data before sorting.
printf("\nData items in original order\n");
for(i=0 ; i <= SIZE-1 ; i++) prin8("%4d",num[i]);
/*************************************************/
//Sorting.
for(pass=1 ; pass<=SIZE-1 ; pass++)
for(i=0 ; i<=SIZE-2 ; i++)
if (num[i] > num[i+1])

//ascending order.

{ hold=num[i]; num[i]=num[i+1];num[i+1]=hold;}
/***************************************************/
//printing data after sorting.
printf("\nData items after sorting\n");
for (i=0 ; i<=SIZE-1 ; i++)
printf("%4d",num[i]);
/***************************************************/
getchar();
getchar();
return 0;
}

Page 1 of 11

Arrays applications
Lets see how?
Pass1:
2
[0]

2
Pass2:

6
[1]
6
[1]
4

10

12

89

68

45

37

4
[2]

10

12

89

68

45

37

6
[2]

8
[3]

10

12

89

68

45

37

8
[3]

10
[4]

12

89

68

45

37

12
[5]

89

68

45

37

12
[5]

89
[6]

68

45

37

89
[6]

68
[7]

45

37

45
[8]

37

10
[4]
10

10

10

10

12

12

12

68

68

89
[7]
45

89
[8]

37
[9]

10

12

68

45

37

89

10

12

37

45

68

89

..........

Pass9:
2

Page 2 of 11

Arrays applications
/*Calculating the sum and average of an array elements */
# include <stdio.h>
# include <stdlib.h>
int main()
{
oat average=0.0;
int i , sum=0 , num[10];
/********** Entering element's value **************** */
for (i=0 ; i<10 ; i++)
{
num[i] = rand() % 100;
sum += num[i];
}
average = (oat) sum /10.0;
/******** printing ... ****************************** */
puts("\nthe elements are >\n ");
for (i=0 ; i<10 ; i++) prin8("%3d",num[i]);
printf("\n\nSum = %d\taverage = %f",sum,average);

getchar();
getchar();
return 0;
}

Page 3 of 11

Arrays applications
/* Searching for avalue of an element in an array */
# include <stdio.h>
# dene NE 9 // NE means number of elements.
int main()
{
int
i , vs ,

// vs means value to search.

arr[NE]={5,2,1,0,7,9,11,6,3};
/* ******* Entering the value to search ********** */
printf("\nEnter a value to search> ");
scanf("%d",&vs);
/* ******** searching operation **************** */
for (i=0 ; i<NE ; i++)
if (vs == arr[i])
{
printf("\nFound the value %d at the position %d",arr[i],i);
break;
}
if(i > NE-1) prin8("\nValue not found");

getchar();
getchar();
return 0;
}

Page 4 of 11

Arrays applications
/* Initializing multidimensional arrays */
# include <stdio.h>
void printArray(const int [][3]);

int main()
{
/* ********** initialize elements ******* */
int a1[2][3]={{1,2,3},{4,5,6}},
a2[2][3]={1,2,3,4,},
a3[2][3]={{1,2},{4}};
/* ********** calling & printing ******** */
prin8("Array a1: \n");
printArray(a1);
prin8("Array a2: \n");
printArray(a2);
prin8("Array a3: \n");
printArray(a3);
getchar();
getchar();
return 0;
}
void printArray(const int a[][3])
{
int i,j;
for (i=0 ; i<=1 ; i++)
{
for (j=0 ; j<=2 ; j++) prin8("%2d",a[i][j]);
printf("\n");
}
}
The const type qualifier can be applied to an array parameter in a function definition to prevent the original array from being modified in the
function body .Functions should not be given the capability to modify an array unless it absolutely necessary.

Page 5 of 11

Arrays applications
/* Adding corresponding elements of two arrays
storing the result in a third array
processes the first n elements only
*/

# include <stdio.h>
void
add_arrays(const int a1[],
const int a2[],
int asum[],
int n);

int main()
{
int a1[]={3,7,2};
int a2[]={5,2,8};
int asum[]={0,0,0};
add_arrays(a1,a2,asum,2);

getchar();
getchar();
return 0;
}
void add_arrays(const int a1[],const int a2[],int asum[],int n)
{
for (int i=0 ; i<n ; i++)
{
asum[i]=a1[i] + a2[i];
prin8("%4d",asum[i]);
printf("\n");
}
}

Page 6 of 11

Arrays applications
/* Sum of elements in a two dimensional array */
# include <stdio.h>
# dene ROW 2
# dene COL 3
int SUM(int a[][COL]);

int main()
{
int b[ROW][COL]={{1,2,3},{4,5,6}};
int sum ;
sum = SUM(b);
printf("\n\tSum of elements is>%d",sum);

getchar();
getchar();
return 0;
}
int SUM(int a[][COL])
{
int i,j,Sum=0;
for (i=0 ; i<ROW ; ++i)
for(j=0 ; j<COL ; ++j)
Sum += a[i][j];
return Sum;
}

Page 7 of 11

Arrays applications
/* Find the max value for the elements
of the main diagonal of a square array */
# include <stdio.h>
# dene SR 3
int max(int a[SR][SR]);
int main()
{
int a[SR][SR]={5,7,9,3,2,1,4,2,8};
printf("\nThe array>\n");
for(int n=0 ; n<SR ; ++n)
{
for (int k=0 ; k<SR ; ++k)
prin8("%3d",a[n][k]);
printf("\n");
}
printf("\tthe max value in main diagonal is> %d",max(a));

getchar();
getchar();
return 0;
}
int max(int a[SR][SR])
{
int max=a[0][0],i;
for (i=0 ; i<SR ; ++i)
if(max<a[i][i]) max=a[i][i];
return max;
}

Page 8 of 11

Arrays applications
/* Multiplying multi-dimansional array by a vector array */
# include <stdio.h>
# dene M 3
m_a_times_v(int a[M][M] , int v[M]);
int main()
{
int a[M][M]={5,1,2,7,3,4,8,1,6};
int v[M]={7,4,9};
m_a_times_v(a,v);

getchar();
getchar();
return 0;
}
m_a_times_v(int a[M][M] , int v[M])
{
int i,j,x[M];
for (i=0 ; i<M ; i++)
{
printf("\n");x[i]=0;
for(j=0 ; j<M ; j++)
x[i] += a[i][j]*v[j];
prin8("%4d",x[i]);
printf("\n");
}

Page 9 of 11

Arrays applications
/* matrix multiplication. */
# include <stdio.h>
# dene M 3
# dene N 2
# dene P 1
mat_pro(int a[M][N], int b[N][P] , int c[M][P]);

int main()
{
int a[M][N]={5,4,2,8,1,2};
int b[N][P]={3,5};
int c[M][P];
mat_pro(a,b,c);

getchar();
getchar();
return 0;
}
mat_pro(int a[M][N], int b[N][P] , int c[M][P])
{
int i,j,k;
for (i=0 ; i<M ; i++)
for (j=0 ; j<P ; j++)
{
c[i][j]=0;
for (k=0 ; k<N ; k++)
c[i][j] += a[i][k]*b[k][j];
prin8("%4d",c[i][j]);
printf("\n");
}
}

Page 10 of 11

Arrays applications
/* ******use string array in drawing ******* */
# include <stdio.h>

main()
{
char str[9][9]={{' ',' ',' ',' ','*'},
{' ',' ',' ','*','*','*'},
{' ',' ','*','*','*','*','*'},
{' ','*','*','*','*','*','*','*'},
{'*','*','*','*','*','*','*','*','*'},
{' ','*','*','*','*','*','*','*'},
{' ',' ',' ','*','*','*'},
{' ',' ',' ',' ','*'}};
for(int i=0 ;i<9 ; i++)
{for (int j=0 ; j<9 ;j++)
printf("%c",str[i][j]);
printf("\n");
}
getchar();
getchar();
}

Page 11 of 11

Você também pode gostar