Question
Write C++ Program
The aim of this is to develop various functions on an array. First, create an array (A) with 10 integers, which will be rando
0 0
Add a comment Improve this question Transcribed image text
Answer #1

All the explanation is in the code comments. Hope this helps!

Code:

#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <ctime>

using namespace std;

// note - array is passed by reference
// and hence any changes made in the function are reflected

// function to copy array
void Copy(int A[], int B[], int size)
{
// loop for all elements
for(int i=0; i<size; i++)
{
B[i] = A[i];
}
}

// function to copy array in reverse
void Reverse(int A[], int B[], int size)
{
// loop for all elements
for(int i=0; i<size; i++)
{
// copy last ith element (size-i-1) as ith element of B
B[i] = A[size-i-1];
}
}

int main()
{
// size of array
int n = 10;
// 2 arrays
int A[n], B[n];
  
// randomly initialise A with 10 integers
// seed the random number generator
srand(time(0));
// loop for all elements
for(int i=0; i<n; i++)
// choose number from 0 to 100
A[i] = rand() % 101;
  
// print initialisation
cout << "Initialization of A...\n";
cout << "\tA :";
for(int i=0; i<n; i++)
cout << " " << A[i] << ",";
cout << endl;
  
// copy and print results
Copy(A, B, n);
cout << "After Copy(A, B)\n";
cout << "\tA :";
for(int i=0; i<n; i++)
cout << " " << A[i] << ",";
cout << endl;
cout << "\tB :";
for(int i=0; i<n; i++)
cout << " " << B[i] << ",";
cout << endl;
  
// reverse copy and print results
Reverse(A, B, n);
cout << "After Reverse(A, B)\n";
cout << "\tA :";
for(int i=0; i<n; i++)
cout << " " << A[i] << ",";
cout << endl;
cout << "\tB :";
for(int i=0; i<n; i++)
cout << " " << B[i] << ",";
cout << endl;
  
// exit message
cout << "Bye...\n";

return 0;
}

Sample result:

Initialization of A... A: 31, 87, 20, 63, 31, 93, 78, 58, 96, 50, After Copy (A, B) A: 31, 87, 20, 63, 31, 93, 78, 58, 96, 50

Code screenshots:

1 #include <iostream> 2. #include <stdlib.h> /* srand, rand */ 3 #include <ctime> 4 5 using namespace std; 6 7 // note - arra

// Loop for all elements for(int i=0; i<n; i++) // choose number from 0 to 100 A[i] = rand() % 101; 41 42 43 44 45 46 47 48 4

Add a comment
Know the answer?
Add Answer to:
Write C++ Program The aim of this is to develop various functions on an array. First,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++ please Instructions The aim of this is to develop various functions on an array. First,...

    c++ please Instructions The aim of this is to develop various functions on an array. First, create an array (A) with 10 integers, which will be randomly selected from the range of 0 to 100. Print the items of the array on screen as one line. - Write a function to copy A to B. The function signature should be "void Copy(int A[], int B[], int size)". This function copies content of A into B in the same order. -...

  • Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array...

    Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array and // its size and returns the index of the first occurrence of the largest element II in the array. Also, write a function to display the array #include ·peh.h" #include <iostream> using namespace std const int ARRAY_SIZE = 15; int main int list[ARRAY SIZE56, 34, 67, 54, 23, 87, 66, 92. 15, 32, 5, 54, 88, 92, 30 cout < List elements: "...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • c/c++ Passing arrays as arguments to functions. The main() is given, write the function 'average' (Study...

    c/c++ Passing arrays as arguments to functions. The main() is given, write the function 'average' (Study the syntax of passing an array as a function parameter) The program prompts the user to first enter the number of tests given in a course, and then prompts him/her to enter the mark for each test. The marks are stored in an array. This takes place in the main() function. It then passes the array to a function named 'average' that calculates and...

  • The ExceptionLab class provided: – Creates an array of 100 elements and fills it with random...

    The ExceptionLab class provided: – Creates an array of 100 elements and fills it with random numbers from 1 to 100. – It asks the user for an index value between 0 and 99. – Prints the element at that position. – If a number > 99 is entered by the user, the class will abort with an ArrayIndexOutOfBoundsException • Modify the ExceptionLab: – Add a try-catch clause which intercepts the ArrayIndexOutOfBounds and prints the message: Index value cannot be...

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • - Write a program that performs several operations on an array of positive ints. The program...

    - Write a program that performs several operations on an array of positive ints. The program should prompt the user for the size of the array (validate the size) and dynamically allocate it. Allow the user to enter the values, do not accept negative values in the array. - Your program should then define the functions described below, call them in order, and display the results of each operation: a) reverse: This function accepts a pointer to an int array...

  • Complete the mult() and printArr() functions #include <iostream> using namespace std; void init(int arr]05], int x)t //initialization loop for row 1 and 2 for(int i = 0; i < 2; i++){ arr i]...

    Complete the mult() and printArr() functions #include <iostream> using namespace std; void init(int arr]05], int x)t //initialization loop for row 1 and 2 for(int i = 0; i < 2; i++){ arr i][j] x; void mult(int arr JSD //multiply the elements of row 2 by row 1, and store result in row3 void printArr int arr ][5]) // a loop to print out all the elements of the array int main) int arr[3105] 31 int x 32 34 // initialize...

  • I need it in JAVA Write a program that randomly populates an array of size 100,...

    I need it in JAVA Write a program that randomly populates an array of size 100, sorts it, and then finds the median. The random numbers must be from 0-99 and all integer values. Also since there is an even set of numbers the median is found by taking the mean of the two middle numbers (In other words the 50th and 51st). You have to code your own sorting method. You may not use a built in java sorter....

  • **IN C*** * In this lab, you will write a program with three recursive functions you...

    **IN C*** * In this lab, you will write a program with three recursive functions you will call in your main. For the purposes of this lab, keep all functions in a single source file: main.c Here are the three functions you will write. For each function, the output example is for this array: int array[ ] = { 35, 25, 20, 15, 10 }; • Function 1: This function is named printReverse(). It takes in an array of integers...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT