C++ pointer help on this problem - Write a program that dynamically allocates an
ID: 3832041 • Letter: C
Question
C++ pointer help on this problem -
Write a program that dynamically allocates an array large enough to hold a user-dened number of test scores (max size of 10 is sufficient for testing). Once all the scores (scores should be randomly generated numbers from 1 to 20) are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. You should have a function that displays the contents of the array. The program should display the array before it is sorted, sorted list of scores and averages with appropriate headings. Use pointer notation rather than array notation. Use the bubble sort to sort your array in ascending order.
Explanation / Answer
Hi,
Please find the code to the following scenario which uses two fucntions bubble_sort and calc_average to to dthe sorting and then after taht calculate the average of those numbers.
rand is used to generate numbers randomly.
Please find the code below:-
CODE:-
#include <iostream>
#include <stdlib.h> /*rand */
using namespace std;
void bubble_sort (int *a, int n);
int calc_average (int *a, int n);
int main()
{
int *a; // arary as pointer
int n; // Size needed for array
int avg; //will hold the average of the array
cout<<"Please tell the size of array"<<endl;
cin >> n; // Read in the size
a = new int[n]; // Allocate n ints and save ptr in a.
for (int i=0; i<n; i++) {
a[i] = 0; // Initialize all elements to zero.
}
//This will randomly genrate numbers from 1 to 20 and will be assigned to array
for (int i=0; i<n; i++)
{
a[i] = rand() % 20 + 1;
}
//Display array in before sorting
cout<<"Array before sorting is:"<<endl;
for (int i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
//Pass array to the function which sorts array using bubble sort
bubble_sort(a,n);
cout<<"Array after sorting is:"<<endl;
for (int i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
//calculate the average of the sorted array
avg=calc_average(a,n);
cout<<"The avergae of the array is:"<<avg<<endl;
//Below two statements are necessary
delete [] a; // When done, free memory pointed to by a.
a = NULL; // Clear a to prevent using invalid memory ref
return 0;
}
//This function will sort the array using bubble sort algorithm.
void bubble_sort (int *a, int n)
{
for (int i = 0; i < n; ++i)
for (int j = 0; j < n - i - 1; ++j)
if (a[j] > a[j + 1])
{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
//This function will calculate the average of teh array scores and will return the average to the called fucntion
int calc_average (int *a, int n)
{
int avgc=0;
for (int i = 0; i < n; ++i)
{
avgc=avgc+a[i];
}
avgc=avgc/n;
return avgc;
}
=======================================================================================
OUTPUT:-
Please tell the size of array
10
Array before sorting is:
4
7
18
16
14
16
7
13
10
2
Array after sorting is:
2
4
7
7
10
13
14
16
16
18
The avergae of the array is:10
==================================================================================
Please let me know in case of any clarification required.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.