#Using array for function to output a variable help

1 messages · Page 1 of 1 (latest)

uncut olive
#

I'm trying to solve the 'nilai' was not declared in this scope error which uses array and it happens at line 35, so please fix some thing to make work again and generate maximum number between numbers in an array (and minimum number, but it not included yet). Please be quick ASAP due my homework is due 4 hours left as i created this help forum. The code will be included below. Thank you

sick oliveBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

lost scaffold
uncut olive
lost scaffold
#

ah wait, you where already on it

uncut olive
#

i using geany btw because my univ use it for practice usage

lost scaffold
#

well, if you want access to nilai, you need to pass nilai to the function

uncut olive
#
lost scaffold
lost scaffold
sick oliveBOT
#
How to Learn C++ Programming

We generally recommend a good book to learn the necessary fundamentals:

To actually write and run C++ code, you will need a compiler, editor, and debugger. We strongly recommend to start out using an IDE, which will provide all these tools for you:

<:microsoft:1165512917047853127> Windows
  • [Visual Studio](#1165492293810257920 message)
  • CLion
<:tux:1165505626894520361> Linux
<:apple:1165508607798943754> Mac
lost scaffold
# uncut olive

also is there a specific reason you forward declare max instead of just putting max above main?

frail pond
#

i would complain more about global namespace variables than about forward declared func

lost scaffold
uncut olive
#

what does mean and how to fix it?

wise drift
#

also im not seeing the 'nilai' was not declared in this scope error you talked about in the error list at the bottom of the screenshot

uncut olive
#

@coarse slate

wise drift
uncut olive
wise drift
uncut olive
coarse slateBOT
#
Compiler Output
<source>: In function 'int main()':
<source>:27:21: error: invalid conversion from 'int*' to 'int' [-fpermissive]
   27 |         maxnum = max(&nilai[100], &v);
      |                  ~~~^~~~~~~~~~~~~~~~~
      |                     |
      |                     int*
<source>:27:21: note: possible fix: dereference with '*'
   27 |         maxnum = max(&nilai[100], &v);
      |                  ~~~^~~~~~~~~~~~~~~~~
      |                  *
Build failed
uncut olive
coarse slateBOT
#
Compiler Output
<source>: In function 'int main()':
<source>:27:21: error: no matching function for call to 'max(int (*)[jumlah], void*)'
   27 |         maxnum = max(&nilai, &&v);
      |                  ~~~^~~~~~~~~~~~~
<source>:27:21: note: there are 3 candidates
In file included from /cefs/22/22e6cdc013c8541ce3d1548e_consolidated/compilers_c++_x86_gcc_15.2.0/include/c++/15.2.0/string:53,
                 from /cefs/22/22e6cdc013c8541ce3d1548e_consolidated/compilers_c++_x86_gcc_15.2.0/include/c++/15.2.0/bits/locale_classes.h:42,
                 from /cefs/22/22e6cdc013c8541ce3d1548e_consolidated/compilers_c++_x86_gcc_15.2.0/include/c++/15.2.0/bits/ios_base.h:43,
                 from /cefs/22/22e6cdc013c8541ce3d1548e_consolidated/compilers_c++_x86_gcc_15.2.0/include/c++/15.2.0/ios:46,
                 from /cefs/22/22e6cdc013c8541ce3d1548e_consolidated/compilers_c++_x86_gcc_15.2.0/include/c++/15.2.0/bits/ostream.h:43,
                 from /cefs/22/22e6cdc013c8541ce3d1548e_consolidated/compile
lost scaffold
wise drift
#

;compile

#include <iostream>
using namespace std;
int jumlah, sum, i, v, a, maxnum, minnum;
int max(int*, int);
float rata2;

int main() {
    cout << "[Pengelolahan Nilai Mahasiswa]\n\n";
    do{
        cout << "Masukkan Jumlah Mahasiswa: "; cin >> jumlah;
        if(jumlah > 100){
            cout << "Jumlah mahasiswa melebihi batas sistem (maksimal 100 orang)\n";
        }
    }while(jumlah > 100);
    string nama[jumlah];
    int nilai[jumlah];
    for(int i=0; i<jumlah; i++){
        cout << "\n[Data Mahasiswa ke-" << i+1 << "]" << endl;
        cout << "Masukkan Nama: "; cin >> nama[i];
        cout << "Masukkan Nilai Mahasiswa: "; cin >> nilai[i];
        cout << endl;
    };
    for(int i=0; i<jumlah; i++){
        sum += nilai[i];
    };
    rata2 = sum/jumlah;
    maxnum = max(nilai, jumlah);
    cout << rata2;
    cout << maxnum;
}

int max(int nilai[], int v){
    int i = 0;
    for(i=1; i<jumlah; i++){
        if(nilai[i] < v){
            v = nilai[i];
        }
    };
    return v;
}
coarse slateBOT
#
Program Output
[Pengelolahan Nilai Mahasiswa]

Masukkan Jumlah Mahasiswa:
Compiler Output
Program terminated with signal: SIGFPE
lost scaffold
wise drift
#

this is so hard to debug when i dont know what these jumlah and all words mean, like when i dont know what the problem statement is

uncut olive
#

these words are comes from indonesian

wise drift
uncut olive
#

should i tell you my homework?

wise drift
#

yes , a problem statement will be very helpful

uncut olive
#

Buatlah sistem penilaian untuk mahasiswa dimana diawal kita input nama dan nilai mahasiswa, lalu di outputkan semua nilai dan stringnya dan diakhir dikasih statistik penilaian nya contoh:

===== Program Pengolahan Nilai Mahasiswa =====

Masukkan jumlah mahasiswa: 3

--- Data Mahasiswa ke-1 ---

Masukkan Nama: Budi

Masukkan Nilai: 85

--- Data Mahasiswa ke-2 ---

Masukkan Nama: Ani

Masukkan Nilai: 92

--- Data Mahasiswa ke-3 ---

Masukkan Nama: Chandra

Masukkan Nilai: 78

=======================================

--- Hasil Pengolahan Data ---

Daftar Nilai Mahasiswa:

  1. Budi : 85

  2. Ani : 92

  3. Chandra : 78

Statistik Nilai:

Nilai Rata-rata : 85.00

Nilai Tertinggi : 92 (Diraih oleh: Ani)

Nilai Terendah : 78 (Diraih oleh: Chandra)

#

Here the english translation:


===== Student Score Processing Program =====


Enter the number of students: 3


--- Student Data 1 ---

Enter Name: Budi

Enter Grade: 85


--- Student Data 2 ---

Enter Name: Ani

Enter Grade: 92


--- Student Data 3 ---

Enter Name: Chandra

Enter Score: 78


=======================================

--- Data Processing Results ---


Student Score List:

1. Budi     : 85

2. Ani      : 92

3. Chandra  : 78


Grade Statistics:

Average Grade : 85.00

Highest Grade : 92 (Achieved by: Ani)

Lowest Grade  : 78 (Achieved by: Chandra)```
#

also using function on my programs would get a bonus points for the task

wise drift
#

;compile

#include <iostream>

int max(int nilai[], int jumlah){
    int ans = 0;
    for(int i=0; i<jumlah; i++){
        if(nilai[i] > ans){
            ans = nilai[i];
        }
    }
    return ans;
}

int main() {
    std::cout << "[Pengelolahan Nilai Mahasiswa]\n\n";

    int jumlah;

    do{
        std::cout << "Enter the number of students: ";
        std::cin >> jumlah;
        if(jumlah > 100){
            std::cout << "Jumlah mahasiswa melebihi batas sistem (maksimal 100 orang)\n";
        }
    }while(jumlah > 100);

    std::string nama[100];
    int nilai[100];

    for(int i=0; i<jumlah; i++){
        std::cout << "\n[Data Mahasiswa ke-" << i+1 << "]" << std::endl;
        std::cout << "Masukkan Nama: "; 
        std::cin >> nama[i];
        std::cout << "Masukkan Nilai Mahasiswa: "; 
        std::cin >> nilai[i];
        std::cout << std::endl;
    }
    
    int sum{}, rata2{}, maxnum{};
    
    for(int i=0; i<jumlah; i++){
        sum += nilai[i];
    }
    rata2 = sum/jumlah;

    maxnum = max(nilai, jumlah);

    std::cout << rata2;
    std::cout << maxnum;
}


coarse slateBOT
#
Program Output
[Pengelolahan Nilai Mahasiswa]

Enter the number of students:
Compiler Output
Program terminated with signal: SIGFPE
wise drift
#

@uncut olive i believe the above program is the debugged version of the code you sent

wise drift
uncut olive
#

anyways can you show me which website to compare 2 diff codes?

wise drift
# uncut olive okei, i'd try this

there are few things you should do and dont do in general when writing code

  1. Dont use the line using namespace std; at the top of your code , write std::cout and std::cin explicitly , its not that hard
  2. You cant make an array which is dynamically sized on the stack , instead look into making one on the heap using the new keyword. You tried to do something like std::string names[variable] , you cant do that. (or if they have taught you what std::vector is , use that instead)
  3. As you previously saw , having seperate declarations can mess with you , just define your functions at the top
  4. Revisit what pointers are and how they are pased as arguments and how they are used , your original code looked like you are confused about how to use the properly
uncut olive
coarse slateBOT
#
Program Output
[Program Pengelolahan Nilai Mahasiswa]

Masukkan Jumlah Mahasiswa: Jumlah mahasiswa melebihi batas sistem (maksimal 100 orang)
Masukkan Jumlah Mahasiswa: Jumlah mahasiswa melebihi batas sistem (maksimal 100 orang)
Masukkan Jumlah Mahasiswa: Jumlah ma
uncut olive
#

thank you so much

#

have a good day!

#

!solved