Selasa, 11 September 2012

Initialize Array From Procedure in C

Sometimes, a very complicated algorithm use a lot of matrix, and I sometimes find a process to initialize a matrix or array from a procedure or function. So, this is a little example how to initialize array from procedure.


#include <stdio.h>

int main() {
// declare array a
int a[5];
int masukan;
printf("Masukkan nilai: ");
scanf("%d",&masukan);
// initialize array with method
test(a, masukan);
int j;
for(j = 0; j < 5; j++) {
printf("a[%d]: %d \n",j,a[j]);
}
return 0;
}

void test(int *b, int c) {
int i;
for(i = 0; i < 5; i++) {
b[i] = c;
}
}

FYI, I build this code in linux, and compile it with gcc. Just a little share today, I hope it can usefull. happy coding, go Open Source.

Tidak ada komentar: