Question

In: Computer Science

For C program, convert the 1-D stencil program from lab03 to use array reference (B[I]) to...

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;

Solutions

Expert Solution

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:

...............................................................

..............................................................

.....................................................................

.....................................................................


Related Solutions

I want the program to use 1- D array and display a table as output. Write...
I want the program to use 1- D array and display a table as output. Write a script to simulate the rolling of two dice. The script should use Math.random to roll the first die and again to roll the second die. The sum of the two values should then be calculated. [Note: Since each die can show an integer value from 1 to 6, the sum of the values will vary from 2 to 12, with 7 being the...
Write Matrix Addition 2 D (dimensional) Array program in c++.
Write Matrix Addition 2 D (dimensional) Array program in c++.
Use Verilog to design and implement a function as  c = c+∑b*ai, i is from 1 to...
Use Verilog to design and implement a function as  c = c+∑b*ai, i is from 1 to 8. Here ai is stored in a SRAM with width as 16 and depth as 8 (8 rows of 16‐bit data), and b is stored in a 16‐bit register. c is initialized as 0.
Create a C++ program that makes use of both concepts i.e. Array of structure and array...
Create a C++ program that makes use of both concepts i.e. Array of structure and array within the structure by using the following guidelines: 1. Create an Array of 5 Structures of Student Records 2. Each structure must contain: a. An array of Full Name of Student b. Registration Number in proper Format i.e 18-SE-24 c. An array of Marks of 3 subjects d. Display that information of all students in Ascending order using “Name” e. Search a particular student...
Write a C++ program to determine the sum of two 1-D matrices: A = [a b...
Write a C++ program to determine the sum of two 1-D matrices: A = [a b c d] B = [e f g h] The user will provide the values of a to h.
Take the Java program Pretty.java and convert it to the equivalent C program. You can use...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use the file in.txt as sample input for your program. v import java.io.*; import java.util.*; public class Pretty { public static final int LINE_SIZE = 50; public static void main(String[] parms) { String inputLine; int position = 1; Scanner fileIn = new Scanner(System.in); while (fileIn.hasNextLine()) { inputLine = fileIn.nextLine(); if (inputLine.equals("")) { if (position > 1) { System.out.println(); } System.out.println(); position = 1; } else...
Complete the "dumb" 8 queens program that Use the 1 dimensional array representation. C++ This is...
Complete the "dumb" 8 queens program that Use the 1 dimensional array representation. C++ This is the solution to the  question. i want this program to written in different WAY. nothing fancy. #include<cmath> #include<fstream> #include<iostream> using namespace std; bool ok(int b[][8]){ int rQueens=0, dQueens=0; for(int row=0; row<8; row++){ for(int column=0; column<8; column++){ //Rows test if(b[row][column]==1) rQueens++; if(rQueens>1) return false; //Diagonals test    for(int j=1; ((column-j)>=0)&&((row-j)>=0); j++){ if(b[row-j][column-j]==1&&b[row][column]==1) return false; } for(int k=1; ((column-k)>=0)&&((row+k)<8); k++){ if(b[row+k][column-k]==1&&b[row][column]==1) return false; } } rQueens=0; }...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }. For this part of the assignment, you should first write a recursive Fib(n) function. .For testing, print out the 100 integers. b) For the second part of this assignment, you must...
To convert the circuit for F(A,B,C,D) = AB'C' + BC'D + AB'D to a 3 level...
To convert the circuit for F(A,B,C,D) = AB'C' + BC'D + AB'D to a 3 level NAND circuit would require: factor AB' from AB'C' + AB'D factor B' from AB'C' + AB'D factor D from BC'D + AB'D factor C' from AB'C' + BC'D none of these
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n...
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n = 50) and displays the string representations of those numbers with following conditions If the current number is divisible by 2, then print CSU If the current number is divisible by 5, then print SB If the current number is divisible by both 2 and 5, then print CSUSB If the number is neither divisible by 2 nor 5, then print the number Example:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT