-2

I try to find the maximum (higest) value from the input, but the value converted to other value. e.g when i put 3 for Mahasiswa.ipk it converted to 0, and Mahasiswa.nim converted to 6946913. How i can the real value shown in the ouput ?

using namespace std;

struct Mhs
{
    char nama[30];
    int nim[20];
    float ipk[2];
};

int BacaDataMhs(int N);
int TampilIpkMaxMin(int N);
int TampilIpkDua(int N); /// edited : I can use this function 

int main(int argc, char ** argv)
{
    int N;
    cout << "Input Value : "; cin >> N;
    TampilIpkMaxMin(N);
    int BacaDataMhs(int N);
}


int BacaDataMhs(int N)
{
    struct Mhs Mahasiswa[N];
    int i;

    for(i=0; i<N; i++)
    {
        cout << "NAMA : "; cin >> Mahasiswa[i].nama;
        cout << "NIM : "; cin >> Mahasiswa[i].nim[i];
        cout << "IPK : "; cin >> Mahasiswa[i].ipk[i];
        cout << endl;
    }
}

int TampilIpkMaxMin(int N)
{
    struct Mhs Mahasiswa[N];
    int i;
    int max=Mahasiswa[0].ipk[0];
    int min=Mahasiswa[0].ipk[0];
    int index_max=0;
    int index_min=0;

    for(i=0; i<N; i++)
    {
        if (max<Mahasiswa[i].ipk[i]) //calculate the higgest value
        {
            max=Mahasiswa[i].ipk[i];
            index_max=i;
        }
        if (min>Mahasiswa[i].ipk[i])
        {
            min=Mahasiswa[i].ipk[i];
            index_min=i;
        }
    }

    cout << "Nilai Tertinggi :" <<endl; //show the higgest value
    cout << "nama "<< Mahasiswa[index_max].nama << endl;
    cout << "nim "<< Mahasiswa[index_max].nim [index_max] << endl;
    cout << "ipk "<< Mahasiswa[index_max].ipk [index_max] << endl;

}
 int TampilIpkDua(int N) // i can use this function but not with int   TampilMhsMaxMin (int N )
{
 struct Mhs Mahasiswa[N];
int i;

    for(i=0; i<N; i++)
    {
        if (Mahasiswa[i].ipk[i]>2)
        {
            cout << "nama "<< Mahasiswa[i].nama << endl;
            cout << "nim "<< Mahasiswa[i].nim[i] << endl;
            cout << "ipk "<< Mahasiswa[i].ipk[i] << endl;
        }
    }

}

//edited

hannah arif
  • 61
  • 10
  • 1
    You never write anything to `Mahasiswa` ! Are some parts of the code missing? Currently you are using an uninitialized variable. – Support Ukraine Mar 14 '17 at 10:54
  • `Mahasiswa[i].ipk[i])` is bad. Using `i` for indexing into `ipk` is wrong as you you have defined `ipk[2];` You probably need an additional for-loop using index 0 and 1 – Support Ukraine Mar 14 '17 at 10:57
  • sorry i just edit the new code, it is the problem – hannah arif Mar 14 '17 at 10:59
  • you are using two different `Mahasiswa` in reading and searching. Read your `praktikum books` again. – AchmadJP Mar 14 '17 at 11:34
  • @AchmadJP Sorry, i dont understand what did you mean, can you show me what is the mistakes.. – hannah arif Mar 14 '17 at 12:14
  • the name `Mahasiswa` in `TampilIpkMaxMin` refers to a different array of Mhs than the name `Mahasiswa` in `BacaDataMhs`. The behaviour of your program is undefined, which in this case seems to be presenting as garbage values being printed – Caleth Mar 14 '17 at 13:54
  • @Caleth, sorry what should i do? is that no way to keep them in different function ? – hannah arif Mar 14 '17 at 21:47
  • I can't tell what you are *trying* to do. Are the members of the struct *exactly* what you need? – Caleth Mar 14 '17 at 22:09
  • @Caleth I just edited the new code, i can use int TampilIpkDua (int N), but what should do, so i can use TampilMhsMaxMin( int N ) too? I just want to make it in a different fucntion, so i can call it anytime.. – hannah arif Mar 14 '17 at 23:25
  • You are using local variables for Mahasiswa and they don't exist outside the function so none of the data is saved past the end of the function. – Jerry Jeremiah Mar 14 '17 at 23:58
  • @ Jerry Jeremiah; how if i put struct Mhs Mahasiswa[N] outside of the main function; to be global variable, can i use these variables? is there no way to make it in the different function ? – hannah arif Mar 15 '17 at 00:14

1 Answers1

0

I still can't figure out the purpose of this exercise, but you don't seem to understand how scope or parameters work.

(I think) You want to have a collection of Mhs's, that get modified and read from in your functions. The easiest place to have define them is main, which is the common ancestor in the call graph.

Arrays with runtime bounds are not C++, although some compilers will accept them. std::vector is a much better container than an array for general use.

Don't put using namespace std in global scope, it will bring far too many names in.

#include <iostream>
#include <vector>
#include <algorithm>

struct Mhs
{
    std::string nama;
    int nim[20]; // Do you always need exactly 20 ints per nama?
    float ipk[2]; // Do you always need exactly 2 floats per nama?
};

void BacaDataMhs(std::vector<Mhs> &);
void TampilIpkMaxMin(std::vector<Mhs> &);

int main(int argc, char ** argv)
{
    int N;
    std::cout << "Input Value : "; std::cin >> N;

    // initialise N empty Mhs' into a vector called Mahasiswa
    std::vector<Mhs> Mahasiswa(N, Mhs{});

    // pass Mahasiswa to input function
    BacaDataMhs(Mahasiswa);

    // pass Mahasiswa to min_max output function
    TampilIpkMaxMin(Mahasiswa);
}

bool compareIpk(const Mhs & lhs, const Mhs & rhs)
{
    // find the largest of each sides ipk

    int left_max = *std::max_element(std::begin(lhs.ipk), std::end(lhs.ipk));
    int right_max = *std::max_element(std::begin(rhs.ipk), std::end(rhs.ipk));

    // compare them
    return left_max < right_max;
}

// simple text representation of an Mhs
std::ostream & operator <<(std::ostream & os, const Mhs & mhs)
{
    os << "nama "<< mhs.nama << std::endl;
    os << "nim {";
    for (int i = 0; i < 20; ++i)
    {
        os << mhs.nim[i];
    }
    os << '}' << std::endl;
    os << "ipk {";
    for (int i = 0; i < 2; ++i)
    {
        os << mhs.ipk[i];
    }
    os << '}' << std::endl;
    return os;
}

void BacaDataMhs(std::vector<Mhs> & Mahasiswa)
{
    for(Mhs & mhs : Mahasiswa)
    {
        std::cout << "NAMA : "; std::cin >> mhs.nama;
        std::cout << "NIM : "; 
        for(int i = 0; i < 20; ++i) {
            std::cin >> mhs.nim[i]; // Read in 20 ints
        }
        std::cout << "IPK : ";
        for(int i = 0; i < 2; ++i) {
            std::cin >> mhs.ipk[i]; // Read in 2 floats
        }

        std::cout << std::endl;
    }
}

void TampilIpkMaxMin(std::vector<Mhs> & Mahasiswa)
{
    using It = std::vector<Mhs>::iterator;
    std::pair<It, It> Mahasiswa_max = std::minmax_element(Mahasiswa.begin(), Mahasiswa.end(), compareIpk);

    std::cout << "Nilai Tertinggi :" << std::endl; //show the highest value
    std::cout << *Mahasiswa_max.first;

    std::cout << "Lowest Value :" << std::endl; //show the lowest value
    std::cout << *Mahasiswa_max.second;

}
Community
  • 1
  • 1
Caleth
  • 52,200
  • 2
  • 44
  • 75
  • @Thanks, I think it is what i mean. I have used the header, but the program still can not run properly, can you show me about it, i have not learned about vector yet... – hannah arif Mar 16 '17 at 04:33
  • @maftuarif I've fixed the errors, and listed the neccecary #includes, the array operator << is now somewhat hairy – Caleth Mar 16 '17 at 10:59