-5

i have no clue how to to start this function.

i wanted the value in the array to subtract from the average to produce the new array.

example:

1 2 3 old array
avg 2
new array -1 0 1

had a program earlier and couldnt run it because of errors.

can anyone give me a hint?

#include <iostream>
#include <cmath>

using namespace std;

const int SIZE = 100;
void readdata (double [], int &);
double findaverage (double [], int);
void howfaraway (double [], int);

int main()
{
    int n;
    double avg;
    double mark[SIZE];

    readdata(mark, n);
    avg = findaverage(mark, n);
    cout << avg;
    return 0;
}

void readdata(double numbers[], int&n)
{
    cout << "Enter the size> ";
    cin >> n;
    for (int count = 0; count < n; count++)
    {
        cout << "Enter the integer> ";
        cin >> numbers[count];
    }
    return;
}

double findaverage (double p[], int n)
{
    double sum = 0;
    for (int count = 0; count < n; count++)
        sum = sum + p[count];

    return (double) sum / n;
}

void howfaraway (double r[], double s[], int n)
{
    for (int count = 0; count < n; count++)

}
1
  • 3
    What are the errors? Do you expect people to debug it for you?
    – Eugene Sh.
    Commented Nov 19, 2014 at 21:34

2 Answers 2

1

I'll write some psuedocode:

length = lengthOf(oldarray);
newarray = new array[length]
avg=sum(oldarray)/length;


for(i=0; i<length; i++){
    newarray[i]=oldarray[i]-avg;
}

return newarray;

something like that.

1

I rewrote something in your code. I hope this help:

#include <iostream>
#include <cmath>

using namespace std;
const int SIZE = 100;

//Variables here
int n;
double avg, numbers[SIZE];

//Functions here
void readdata ();
double findaverage ();
void howfaraway ();

int main()
{
    readdata(); // Read the input and calculate the avg.
    cout << "This is the Average: " << avg << endl; // Printing the avg
    cout << "This is the new array: "; 
    howfaraway(); // Printing the new array
    return 0;
}

void readdata()
{
    cout << "Enter the size: ";
    cin >> n;
    avg = 0;
    for (int i = 0; i < n; i++)
    {
        cout << "Enter the integer: ";
        cin >> numbers[i];
        avg += numbers[i]; // Getting the total sum
    }
    if(n > 0)
        avg /= n; // Getting the avg
}

void howfaraway()
{
    for (int i = 0; i < n; i++)
        cout << numbers[i] - avg << " "; // Printing each new element
    cout << endl;
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.