In: Computer Science
For C program, convert the 1-D stencil program from lab03 to use array reference (B[I]) to access array element instead of using pointers. The C program follows these steps: 1) declare two arrays, each has 100 elements; 2) use a for loop to randomly generate 100 integers and store them in one array; 3) use another for loop to do the 1-D stencil and store the result in the other array;
A stencil pattern is a map where each output depends on a “neighborhood” of inputs.
A stencil output is a function of a “neighborhood” of elements in an input collection.
C program:
1) declare two arrays, each has 100 elements;
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int randArray[100],stencilArray[100],i;
2) use a for loop to randomly generate 100 integers and store them in one array;
for(i=0;i<100;i++)
randArray[i]=rand();
printing the random elements from the array
printf("\nElements of the array::");
for(i=0;i<100;i++)
{
printf("\nElement number %d::%d",i+1,randArray[i]);
}
3) use another for loop to do the 1-D stencil and store the result in the other array;
for (i =0; i < 100 ; ++i )
{
stencilArray[i] = 0.25f * stencilArray[i-1] + 0.50f * stencilArray[i] + 0.25f * stencilArray[i+1];
}
print the 1D stencil array
printf("\n1D Stencil of the random array::");
for(i=0;i<100;i++)
{
printf("\n%d::%d",i+1,stencilArray[i]);
}
return 0;
}
Output:
...............................................................
..............................................................
.....................................................................
.....................................................................