2014-11-19 2 views
-5

나는이 기능을 시작하는 방법을 모른다.배열 빼기 평균을 다른 배열에 넣으시겠습니까?

새로운 배열을 생성하기 위해 배열의 값에서 평균을 뺍니다.

예 :

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

는 프로그램 이전했고 오류로 인해 실행 couldnt한다.

누구나 내게 힌트를 줄 수 있습니까? 그런

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


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

return newarray; 

뭔가 :

#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++) 

} 
+3

무엇을 오류? 사람들이 당신을 위해 그것을 디버깅 할 것으로 기대합니까? –

답변

1

코드에 내용을 다시 작성했습니다. 이 도움을 바랍니다 :

#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; 
} 
관련 문제