Question

IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the new array. Creates a new array that is a copy of the argument array, and returns a pointer to the new array. The function will work as follows: Accept an array and its size as arguments. Dynamically allocate a new array that is the same size as the argument array. Copy the elements of the argument array to the new array. Return a pointer to the new array. Exercise 2: Returning Pointers from Functions Suppose you want to write a function that passes an array argument, getRandomNumbers, to get a pointer to an array of random numbers in the array. The function dynamically allocates an array, uses the system clock to seed the random number generator, populates the array with random values, and then returns a pointer to the array. Function getRandomNumbers to generate a random array and return a pointer. int* getRandomNumbers(int num); // The parameter indicates the number of numbers requested. The algorithm can be described as follows: Accept an array size as argument Dynamically allocate a new array that is the same size as the argument. White a loop to generate random numbers to the new array Return result as a pointer Exercise 3: Increase Array Size Once an array is created, its size is fixed. Sometimes, you need to add more values to an array than it is able to hold; in this case, you may create a new larger array to replace the existing array. Write a function that returns a pointer to a new int array that is double the size of the array passed in the parameters to this function. Use the following header: int *double_capacity(const int *list, int size) Write a test program that prompts the user to enter the number of elements of an array, and then enter the elements (type int). After that the program will use your double_capacity() function to double the capacity of the array and prompt the user for more values. Finally, the program will display all the elements of the array. Note: make sure to delete the old array, since it isn't likely to be used after the function creates and returns the new, bigger array. Exercise 4: Student Scores In these assignments you are asked to develop functions that have dynamic arrays as parameters. Remember that dynamic arrays are accessed by a pointer variable and thus the parameters that serve as dynamic arrays are, in fact, pointer variables. void sort(float* score, int num_scores); // a prototype whose function has a // dynamic array as its first //parameter. It is a pointer variable int main() { float *score; // a pointer variable score = new float(num_scores); // allocation of the array sort(score,scoreSize); // call to the function Write a program that will read scores into an array. The size of the array should be input by the user (dynamic array). The program will find and print out the average of the scores. It will also call a function that will sort (using a bubble sort) the scores in ascending order. The values are then printed in this sorted order. Sample Run: Please input the number of scores 5 Please enter a score 100 Please enter a score 90 Please enter a score 95 Please enter a score 100 Please enter a score 90 The average of the scores is 95 Here are the scores in ascending order 90 90 95 100 100

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>

using namespace std;

void bubble (int arr[ ], int n)

{
int i,j,temp;
for(i=0; i<(n-1); i++)

{

for(j=0; j<(n-i-1); j++)

{

if(arr[j]>arr[j+1])

{

temp=arr[j];

arr[j]=arr[j+1];

arr[j+1]=temp;

cin>>n;

cout<<"Enter "<<n<<" scores :";

}

}

}

cout<<"here are the scores in ascending order :\n";

for(i=0; i<n; i++)

{

cout<<arr[i]<<endl;

}

}

int main()

{

int n,i,arr[50], j,temp,sum;

float avg=0.0;

cout<<"Enter total number of scores :";

for(i=0; i<n; i++)

{

cin>>arr[i];

}

for(i=0; i<n; i++)

{

sum+=arr[i];

}

avg= sum/n;

cout<< " the average of the scores is :" <<avg<<endl;

bubble(arr,n);

return 0;

}

In the above program

We first declare the header files which iostream

Using namespace std enables the user to use different names for objects and classes in c++(not used in this program)

We then write a function for bubble sort by passing the size and array values to it from the main function

Here the technique used is bubble sort where each pair of adjacent elements in the array are checked and swapped if not I'm order

In the main function we declare all variables

We dynamically take the size of the array and array values from the user and calculate both the sum and average of these valu(i.e the scores) we then print the average value of the scores.

By calling the bubble function we sort the array of scores by passing it as a parameter to the bubble sort function and print the ascending order of the scores

Add a comment
Know the answer?
Add Answer to:
IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...
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
  • Hi this is C++, I'm really struggle with it please help me.... ************************ Here is the...

    Hi this is C++, I'm really struggle with it please help me.... ************************ Here is the original high score program,: Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look exactly like this: Enter the name for score #1: Suzy Enter the score for...

  • 3460:209 Assignment 9-B Assignment9-B: The Element Shifter The purpose of this assignment is to help gauge...

    3460:209 Assignment 9-B Assignment9-B: The Element Shifter The purpose of this assignment is to help gauge your skills in writing small programs that involve pointers. The program also contains functions and may perform input, output, files and file processing, use arrays and vectors and/or c-string/string arrays, flow of control, and/or calculations. PROGRAM SPECIFICATION For this program, we are going to expand a standard array by dynamically allocating a new one with a larger footprint. The program will use a function...

  • - 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...

  • C++ Single Dimensional Arrays

    Exercise #1: Design and implement a program (name it AssignGrades) that stores and processes numeric scores for a class. The program prompts the users to enter the class size (number of students) to create a single-dimensional array of that size to store the scores. The program prompts the user to enter a valid integer score (between 0 and 100) for each student. The program validates entered scores, rejects invalid scores, and stores only valid scores in the array.  The program...

  • solve it in c+* Part II: Dynamic Arrays and Pointer Arithmetic Q5: Implement a subset function...

    solve it in c+* Part II: Dynamic Arrays and Pointer Arithmetic Q5: Implement a subset function for a dynamic array which returns a new dynamic array that is a subset of the original. (15pt) input parameters: array - (the array and any related parameters) start - index of the first element end - index of the last element This function returns a new dynamic array containing the elements from the start thru the end indices of the original array. All...

  • 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 Programming Language 2(a) Define a struct with 1 int array named i, 1 float array...

    C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array named f, and one double array named d, each of size M. (b)Declare array x with N of those structs. (c)Write a void function to traverse array x (using a pointer) assigning to each element in each array d (in each struct in array x) the sum of the corresponding elements in arrays i and f (in the same struct). Use 3 pointers (of...

  • using c++ 13 do pretty much whatever they want Exercise: Array Resizing You will create an...

    using c++ 13 do pretty much whatever they want Exercise: Array Resizing You will create an array manipulation program that allows the user is to pre much whatever meant to ananas Wananching the program the user will passat are the contains a set of value and that w 2 Check to see the could be opened at the program was not opened the continue 3. Create an array and with the values from Presente en de h and powermany reded...

  • write the program in c++ Pointen & A???ys: Write u function that is passed a pointer...

    write the program in c++ Pointen & A???ys: Write u function that is passed a pointer to a float array, and the size the array as an int. The function should return a pointer to the maximum element in the array The function should be: int *maxp(float* ap, int size) Write a main() program to lest the function with different input arrays. Grading Criteria (1 point each): Uses cin/cout correct function definition prompts user for input, reads it in correct...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

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