Question

In: Computer Science

Rewrite your program for part 1. Do not declare the array globally, declare it in the...

Rewrite your program for part 1. Do not declare the array globally, declare it in the loop function. This now requires that you add two parameters to your fill array and print array functions. You must now pass the array name and array size as arguments, when the program calls these functions. The program has the same behavior as problem 1, but illustrates the difference between globally and locally declared variables.

The program code for part 1 was:

int Array[15] = {0}; //declaration of global array size 15

void setup(){
Serial.begin(9600); //start with baud rate of 9600
}

void loop(){
fillArray(10,99); //fills array with random numbers between 1 and 99
delay(500);
printArray(5); //displays data with 5 numbers per line
delay(1000); //prints data on serial monitor every second
}

void fillArray(int low, int high){
for(int i = 0; i < (sizeof(Array)/sizeof(Array[0])); i++){ //size of array
Array[i] = random(low,high);
}
}

void printArray(int NumberPerLine){
Serial.println("Array is:"); //prints the text and adds new line
int counter = 0; //variable to store how many numbers printed
for(int i = 0; i < (sizeof(Array)/sizeof(Array[0])); i++){
if(counter == NumberPerLine){
Serial.println(""); //print new line
counter = 1; //number printed on a single line is again set to 1
}
else{
counter++; //adds 1 to counter
}
Serial.print(Array[i]); // print numbers one by one
Serial.print(" "); //adds space between each number
}
Serial.println(""); //adds new line
}

Solutions

Expert Solution

Updated Code

//declaration of global array size 15

void setup()
{
Serial.begin(9600); //start with baud rate of 9600
}

void loop()
{
const int arraySize = 15;
int Array[arraySize] = {0};
fillArray(10,99,Array,arraySize); //fills array with random numbers between 1 and 99
delay(500);
printArray(5,Array,arraySize); //displays data with 5 numbers per line
delay(1000); //prints data on serial monitor every second
}

void fillArray(int low, int high,int Array[],int arraySize)
{
for(int i = 0; i < arraySize; i++){ //size of array
Array[i] = random(low,high);
}
}

void printArray(int NumberPerLine,int Array[],int arraySize)
{
Serial.println("Array is:"); //prints the text and adds new line
int counter = 0; //variable to store how many numbers printed
for(int i = 0; i < arraySize; i++){
if(counter == NumberPerLine)
{
Serial.println(""); //print new line
counter = 1; //number printed on a single line is again set to 1
}
else{
counter++; //adds 1 to counter
}
Serial.print(Array[i]); // print numbers one by one
Serial.print(" "); //adds space between each number
}
Serial.println(""); //adds new line
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template....
Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template. And rewrite your main function to test your template for integer array and double array. Following is my complete code: #include <iostream> using namespace std; class Array { private: // Pointer to memory block to store integers int* data; // Maximum size of memory block int cap; // Stores number of integers in an array int num; public: // Constructor Array(int size); // Default...
Write the following program in MIPS: a) declare an array A of the following numbers: 3,...
Write the following program in MIPS: a) declare an array A of the following numbers: 3, 5, 8, 10, 12, 2, 76, 43, 90, 44 b) declare a variable called size which stores the number of element in array A, that is 10. c) write a subroutine to search for a number stored in an array and return true or false. In C++ the subroutine is as follows: search(array, size, number_To_Search) e.g. search(A, 10, 12) The subroutine should return 0...
C++ Vectors. Create a program do the following in the program: 1. declare an vector without...
C++ Vectors. Create a program do the following in the program: 1. declare an vector without specifying the size 2. use push_back to add random integers between 100 and 999 to the vector 3. write a function that returns the smallest, largest, and average of the numbers in the vector display the smallest, largest, and average of the numbers in the vector
Write a C program to Declare an integer array of size 10 with values initialized as...
Write a C program to Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9])...
1.Declare a two-dimensional array of Strings namedchessboard.
1. Declare a two-dimensional array of Strings named chessboard.2. Declare a two-dimensional array of integers named tictactoe.3. Declare and create a two-dimensional array of chars,tictactoe, with 3 rows, each with 3 elements.4. Create a two-dimensional array of ints, plan, with 2 rows, and and 3 columns and initialize the first row to 8, 20, 50 and the second row to 12, 30, 75. Use member initializer syntax.
Part 1:Write a program in Java that declares an array of 5 elements and displays the...
Part 1:Write a program in Java that declares an array of 5 elements and displays the contents of the array. Your program should attempt to access the 6th element in the array (which does not exist) and using try. catch your program should prevent the run-time error and display your error message to the user. The sample output including the error message is provided below. Part (1) Printing an element out of bounds 5 7 11 3 0 You went...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt the user for 6 integer values and store them in arr. Prompt the user for a target integer target. Search for targetin arr. If targetis found to match an element in the arr, then the program prints out a message which contains the address of the found element, otherwise, if no element found then the message “the element target not found” will be printed...
Declare and initialize an array to store the course name or code.
Declare and initialize an array to store the course name or code.
Part 1: Create a character array and save your first and last name in it
PROGRAMMING IN C:Part 1:Create a character array and save your first and last name in itNote: You can assign the name directly or you can use the scanf function.Display your name on the screen.Display the address (memory location) in hexadecimal notation of the array. (hint: use %p)Use a for loop to display each letter of your name on a separate line.Part 2:Create a one dimensional array and initialize it with 10 integers of your choice.Create a function and pass the...
How to do a blackjack game with the following rules and WITHOUT USING ARRAY Part 1...
How to do a blackjack game with the following rules and WITHOUT USING ARRAY Part 1 – Displaying Cards Write a function to display (print) a card. sample program output void displayCard(int card) Prints the name of the card (ten of spades, queen of diamonds etc.). The parameter should be a value between 1 and 13, from which the type of card can be determined (1 = ace, 2 = two, …, 10, == ten, 11 = jack, 12 =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT