Question:
How to modify an array inside of a function in C++?
anonymous
2012-05-16 17:20:37 UTC
I'm taking and intro to C++ class and I'm having a little trouble with a portion of an assignment. We have just learned functions and arrays. I have some trouble with passing arrays into functions. Here's the assignment:


Write a function "subtract_average" to subtract the average of the numbers in an array from each element of the array. The function should call the function to compute the array average as the first step in subtracting the average. For instance, if the array contains (1:5; 3:5; 1), then the average is 2. Subtracting the average changes the array elements to (0:5; 1:5; 1) whose average is 0. Function "subtract_average" should take two parameters, the array, and the number of elements in the array. The array parameter can either be declared as a fixed size array or as a variable size array. The array should NOT have type const. The array size should have type const. If the array parameter is a if xed size array, the array size must be a global constant. The function modi es the array and does not return any value.

In the main program, compute and print the sum and average of the array. Call the function to subtract the average from each element of the array and then print the array containing the modi ed list. Finally, again compute and print the average of the array which now contains the modi ed list. Note that the average of the modi ed list should always be close to zero. Because of numerical error, it may not be exactly zero."

Here's my program thus far, as you can see I just need a little help with my third function which subtracts the average of the array, and creates a new array. I understand the idea completely, but I'm not sure how to put it into the function properly (I've commented out the portions which I'm having trouble with). Thanks


#include
#include
using namespace std;

// FUNCTION PROTOTYPES

double compute_sum(int array[ ], int numElements);
double compute_average(int array[ ], int numElements);
// subtract_average(int array[ ], int numElements);


int main()
{
const int ARRAY_SIZE(100);
int array[ARRAY_SIZE];
double average(0.0);
double sum(0.0);
int numElements(0);
int x(0);

cout<< "Enter list of 100 or fewer non-zero numbers: ";
cin >> x;
numElements = 0;

while (x != 0 && numElements < ARRAY_SIZE)
{
array[numElements] = x;
numElements = numElements + 1;
cin >> x;
}

average = compute_average(array, numElements);
cout << "Average: " << average << endl;

sum = compute_sum(array, numElements);
cout << "Sum: " << sum << endl;

// modify = subtract_average(array, numElements);
// cout << "Modified array: " << modify << endl;

return 0;
}

// FUNCTION DEFINITIONS

double compute_sum(int array[], int numElements)
{
int sum(0);
for (int i=0; i {
sum = sum + array[i];
};
return(sum);
}

double compute_average(int array[], int numElements)
{
int sum = compute_sum(array, numElements);
double average = double(sum)/numElements;
return(average);
}

// double subtract_average(int array[], int numElements)
Three answers:
?
2012-05-16 18:56:50 UTC
Hi,

my code looks slightly different from yours. It also does not include , which among others contains the function fabs, which you probably wanted to use in your subtract_average function. On the other hand additional to the includes you used I added algorithm and iterator. Both of these headers are needed in order to make use of the copy function (I used it in main). In case you feel uncertain about the copy function you could use print, which I implemented in the following code as an alternative.



The trick with the subtract_average function is that there are no negative values. So in case the average of the array is bigger than the current element the result needs to be multiplied with -1. This behavior would - as I hope - be identical with the result of applying the fabs function to the reduced element!.



Have fun



Check the code here:

http://ideone.com/irqeN
Sadsongs
2012-05-16 17:30:10 UTC
Declare your array eg int num_array[100]



Function declaration is func abc (int an_array[], int length)



Call with abc(num_array, x) whwre x is the number of elements to consider,
anonymous
2016-10-24 05:46:20 UTC
Bah, I hate C++.... How about: #comprise // or perhaps with that's called in C++ void createArray(flow *array) { int length; cin >> length; array = malloc(sizeof(flow) * length); //... } the down edge is you want to do free(array) someplace as a way to no longer leak memory. i do not if "flow array[]" particularly of "flow *array" makes any huge difference (it shouldn't!!) yet possibly new flow[length] allocates it on the stack or something? Edit: the guy below called it a "close by scope", so yeah, the "new" preparation allocates it on the stack.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...