mercredi 2 janvier 2019

the nan error for all possible case i enter

the output is to print out the farthest number from x from array my code, for all case, only print out nan, i have been staring at my code for hours but still got nothing, eventhough i know something went wrong

it doesn't show any error so i don't really know what to do, i'm a newbie, and i know this question may sound kinda dumb, but can somebody help me please :((

#include <iostream>
#define MAX 100
#include <cmath>
using namespace std;
void nhap(float a[] ,int &n)
{
    int x;
    do
       {
        cout<< "\nnhap so n: ";
        cin >> n;
        cout<< "\nnhap so x: ";
        cin >> x;
    if(n<0 || n> MAX)
        cout << "\nsai roi, moi nhap lai ";
        }
        while(n<0 || n >MAX);
        for(int i=0; i<n; i++)
            {
            cout << "a[" << i << "]= ";
            cin >> a[i];
            }
}
void xuat(float a[], int &n)
{
    for(int i=0; i <n; i++)
    {
    cout << a[i] << " ";
    }
}
void taomang (float a[], float b[], int &n, int x)
{
    for (int i=0; i<n; i++)
        b[i] = abs(a[i] - x);
}
float xaxnhat(float a[], float b[], int &n)
{
    int s = b[0];
    for(int i=0; i<n; i++)
    {
        if(b[i]>s)
        {
            s=b[i];
        }
    }
        return s;
}
float xuatketqua (float a[], float b[], int n)
{
    int k = xaxnhat(a,b,n);
    for (int i=0; i<n; i++)
    {
        if (b[i] == k)
            return a[i];
    }
}
int main()
{
int n;
float a[MAX];
float b[MAX];
nhap(a,n);
xuat(a,n);
cout << " \ngia tri xa gia tri x nhat la: ";
cout << xuatketqua(a,b,n);
return 0;
}

for example array: 19 434 23 55 7 x: 2 then the farthest value from x is: 434

Aucun commentaire:

Enregistrer un commentaire