Fill an input array of size 10 with random numbers in the range between 1 and 10
ID: 3545893 • Letter: F
Question
Fill an input array of size 10 with random numbers in the range between 1 and 100
using the built-in subroutine random. Then reverse the ordering of the array, in other
words, the last element of the input array is the rst element in the reverse array.
Output both arrays. If your program works properly, the output should be similar to
Fig. 4
Figure 4: The rst row is the input array and the second the reverse output array.
The skeleton code is given below. (only add code where it says, and no editing pre existing code)
//==============================================================
int[] array = new int[10];// input array
int[] output = new int[10];// output array
PFont myFont;
int hOffset, vOffset, vSpacing, hSpacing;
// Declare variables that you may need in here
void setup()
{
size(600, 200);
smooth();
myFont = createFont("FFScala", 32);
textFont(myFont);
textAlign(LEFT);
vSpacing = 50;// vertical spacing between text
hSpacing = 50;// horizontal spacing between numbers
// initialize array with random number in the range 1 to 100
// make an array output that reverses the order of array
// for example, in array, if it is 34, 45,
}
void draw()
{
background(255, 255, 255);
fill(0);
hOffset = 20;// horizontal offset. The position to show the first character
vOffset = 50;// vertical offset.
// add code below to output the two arrays
}
Explanation / Answer
int[] array = new int[10];// input array
int[] output = new int[10];// output array
PFont myFont;
int hOffset, vOffset, vSpacing, hSpacing;
// Declare variables that you may need in here
void setup()
{
size(600, 200);
smooth();
myFont = createFont("FFScala", 32);
textFont(myFont);
textAlign(LEFT);
vSpacing = 50;// vertical spacing between text
hSpacing = 50;// horizontal spacing between numbers
// initialize array with random number in the range 1 to 100
for(int i=0;i<10;i++)
array[i]=(rand()%100)+1;
// make an array output that reverses the order of array
// for example, in array, if it is 34, 45,
int temp,k;
k=9;
for(j=0;j<10;j++,k--)
{
output[j]=array[k];
}
}
void draw()
{
background(255, 255, 255);
fill(0);
hOffset = 20;// horizontal offset. The position to show the first character
vOffset = 50;// vertical offset.
// add code below to output the two arrays
cout<<" Input Array: ";
for(int i=0;i<10;i++)
cout<<array[i]<<" ";
cout<<" Output: ";
for(int j=0;j<10;j++)
cout<<output[j]<<" ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.