Concept explainers
Copying one array to another array:
An array is the variable which stores the same types of data. To copy the data from one array to another, first initialize the two arrays with their respective element sizes.
- Consider one of the arrays is filled with a set of values and the filled array is required to be copied to another array.
- To copy the content of one array to another, assign each element of the array to another by using the equal to (“=”) operator.
- Each element of the array can be referred to by using index numbers of the array.
Example:
Consider the following code which performs the copying of array elements.
// Header file section
#include<iostream>
using namespace std;
// Main function definition
int main()
{
// Declaring the arrays
int list1[5];
int list2[5];
//Declare the variables
int n, i;
/* Prompt the user to enter the number of elements
need to be copied */
cout << "Enter the number of elements need to be copied" << endl;
// Read “n” value from the user
cin >> n;
/* Execute the for loop to include the elements in the list1[]*/
for (i = 0; i < n; i++)
{
cin >> list1[i];
}
// Copying of one array to another
/* Execute the for loop to copy the elements from
list1[] to list2[]*/
for (i = 0; i < n; i++)
{
//Copying each element of the array to another
list2[i] = list1[i];
}
// Print the header
cout << "copied array" << endl;
/* Execute the for loop to print the elements in the
list2[]*/
for (i = 0; i < n; i++)
{
cout << list2[i] << endl;
}
}
Explanation:
Thus, in the above example, the two arrays are first initialized with the elements and then one array is assigned to another array using the equal to “=” operator. But, this equal to operator does not copy the content alone; it is used for assignment.
Want to see the full answer?
Check out a sample textbook solutionChapter 7 Solutions
Starting Out with C++ from Control Structures to Objects (9th Edition)
- Create an application that simulates a game of Tic Tac Toe in C# using a two-dimensional array. At the end of the game make sure to declare who the winner is by reading the array.arrow_forwardplease help me with this JAVA Programming challenge from chapter 7. Below is the challenge question Thank you 17. 2D Array Operations Write a program that creates a two-dimensional array initialized with test data. Use any primitive data type that you wish. The program should have the following methods: getTotal. This method should accept a two-dimensional array as its argument and return the total of all the values in the array. getAverage. This method should accept a two-dimensional array as its argument and return the average of all the values in the array. getRowTotal. This method should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The method should return the total of the values in the specified row. getColumnTotal. This method should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the…arrow_forwardWrite an application that stores nine integers in an array. Display the integers from first tolast, and then display the integers from last to first.arrow_forward
- a) create a method that accepts a 2D array and shuffles it values (like playing cards) b) test your method from main: print your 2D array before and after c) create a similar method that will return the array. Call and test it from main .arrow_forward***Needs to be written in Java*** Write a hangman game that randomly generates a word and prompts the user to guess one letter at a time, as presented in the sample run. Each letter in the word is displayed as an asterisk. When the user makes a correct guess, the actual letter is then displayed. When the user finishes a word, display the number of misses and ask the user whether to continue to play with another word. Declare an array to store words, as follows: (see image attatched)arrow_forwardQ2. Write a Java program named RandomNumbers that creates a single-dimensional array of int of size 20 and initialize its elements with 20 random integer numbers (their range must be between 1 and 100 inclusive. Write statements in your program to display the array elements, and how many odd and even numbers are there in the array. For example, if the random numbers were stored in the array are: 83 48 89 66 40 94 31 31 12 45 45 87 10 24 78 2 28 31 52 46 Then the program should display: The random numbers are: 83 48 89 66 40 94 31 31 12 45 45 87 10 24 78 2 28 31 52 46 There are 8 odd numbers There are 12 even numbers An element in the array is a peak (max or min) if the elements before and after that element are greater than the element itself (min) or the elements before and after that element are less than the element (max). Write a method in your program called displayPeaks…arrow_forward
- Q1 Write a Java program named matrixArray that generate 25 random integer numbers (their range must be between 0 and 50 exclusive) and stores them an array The program should do the following 1- Display (print out) all numbers 2- Display all between 20 and 35 inclusive. 3- Sum array elements between 20 and 35 inclusive.arrow_forwardIN C++ Write a method that lets you insert a number anywhere in an array that is already full. For instance, if you have an array of size 5: 3,6,1,2,0. You should be able to insert the number 9 in some desired/specified position. E.g 3,6,1,9,2,0.arrow_forwardFor an array declartion of int number [25];, what will be the ending index? 0 1 24 25arrow_forward
- char[] W = { 'U', 'N', 'I', 'O', 'B', 'K'};What is the index value of B in the above array declaration? a. 4 b. 5 c. 14 d. 2arrow_forwardUsing Java Develop a program that allows the user to enter numbers into an array. Input will be as follows:The user will enter the total number of integers to be entered into the array.The user will then enter that number of unique integers (negative or positive). Do not allow the number of valuesentered to exceed the array size.Develop the following methods(SIX) to:- The ‘main’ method- Print the array- Sort the array(YOU MUST DEVELOP YOUR OWN SORT METHOD –don’t use the API classes).- Determine the highest value.- Determine the lowest value.- Calculate the average value (double)Program Logic (algorithm):- Prompt and get the size of the array- Create the array of that size- Loop through the initialization of the array- Prompt the user for each value- Save the value in the corresponding element-Print the array- Sort the array- Print the array again- Output the highest value- Output the lowest value- Output the averagearrow_forwardWrite an application that stores the following nine integers in an array: 10, 15, 19, 23, 26, 29, 31, 34, 38. Display the integers from first to last, and then display the integers from last to first.arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage