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)
- please 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_forwardc plus plus language , please solve itarrow_forwardAnalyze the statements below: double [] templ = {50.0, 69.0, 75.0, 80.0, 55.0}; printArray (temp1); The call to printArray sends the of/to the array temp1.arrow_forward
- Write the definition of a function named total that receives two parameters: an array ary of element type double and an int called size that contains the number of elements of the array. The function returns the sum of the elements of the array as a double. Use a for loop in your function definition. (8%) Write the prototype for the function total. (3%) Call function total with the array called myArray that contains 6 elements and assign the result to double myTotal. (3%)arrow_forwardJava:arrow_forwardProblem Description - JAVA PROGRAMMING Use a Two-dimensional (3x3) array to solve the following problem: Write an application that inputs nine numbers, each of which is between 1 and 10, inclusive. Display the array after the user inputs each value. Rotate/flip the array by changing places. Make the rows columns and vice versa. You have to move the elements to their new locations. Remember to validate the input and display an error message if the user inputs invalid data. Documentation and the screenshot(s) of the results. Example: 1 2 3 4 5 6 7 8 9 the result will be : 1 4 7 2 5 8 3 6 9arrow_forward
- Java - Functions with 1D Arrays Create a program that asks the user for the size of an integer array and the elements of the array. Then, create a function called maxArray() with the following details: Parameters: An integer array The size of the integer array Return type - int Return value - the maximum value of the integer array Call the function you created in the main and pass the integer array and its size to it. Make sure to store the return value in a variable so you could print it afterwards. Input 1. First line contains the size of the array 2. Succeeding lines are the elements of an array 3. First line contains the size of the array 4. Succeeding lines are the elements of an array 5. First line contains the size of the array 6. Succeeding lines are the elements of an array 7. First line contains the size of the array 8. Succeeding lines are the elements of an array Output Enter size of array: 5 Enter element 1: 1 Enter element 2: 2 Enter…arrow_forwardint[] array = { 1, 4, 3, 6 }; What would be a proper syntax to access 3 from this array?arrow_forwarda. double [] al = new double(2); b. double [] a2 = new {2.5}: c. double [] a4 = {5, 2}: d. double [] a3 2. Which of the following is correct array declaration? = new {5.1. 2.8}:arrow_forward
- JAVA ARRAY MANIPULATION QUESTION: make a method called manipulator that takes a double array as parameter and modifies it. The method will modify the array then print the array. The method must modify the existing array, it CAN NOT create a different array as the solution. The manipulator method will be a void method. Remember: To get an average of 2 digits you add them together then divide by 2. Average of x and y = (x+y)/2 Modifications the method will do: For every 2 consecutive elements in the array, both the elements are replaced by their average. If the number of elements in an array is odd don't modify the last number of the array. Basically, you find the average of 2 consecutive elements then replace both of the elements with that value. Examples: Array = {2.0,3.0} will be changed to {2.5,2.5} when passed to the manipulator method Array = {2.0,3.0,45} will be changed to {2.5,2.5,45} when passed to the manipulator method Array = {2.0,3.0,45,55} will be changed to…arrow_forward2. How do you initialize an array in C? a) int arr[3] = (1,2,3); b) int arr(3) = {1,2,3}; c) int arr[3] = {1,2,3}; d) int arr(3) = (1,2,3);arrow_forwardProgram 3 SRC voting application You must write a small application in JAVA that stores SRC candidate names and the votes they received. Your program must: 1. Create an array of SRC candidate names. Populate the array with ten names. 2. Create an array of votes. Populate the array with the number of votes for every candidate. The two arrays work in parallel. 3. Print the name of every candidate with his/her number of votes. 4. Print the total number of votes casted. 5. Add votes for a specific candidate. a. Read the name of the candidate b. Add one vote for the specific candidate C. Print the name of the candidate and his/her number of votesarrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT