#Writing a function that counts the number of times an integer appears in the array.

52 messages · Page 1 of 1 (latest)

fallen swift
#

Hello there, I'm trying to answer this question '2-3. Write a function that given an array of integers and the length of the array, will count the amount of times the numbers 2, 5 and 9 appear using a switch statement. The function must print out your results on one line in the following form:
2:<num_twos>;5:<num_fives>;9:<num_nines>;

If the array contained three 2s, one 5 and eleven 9s, the output would be:

    ```2:3;5:1;9:11;```

Note the colons and semi-colons. Also remember to write out a newline at the end of the output. The function must still produce a line of output even if the size parameter, n, is less than 1.

Signature: void two_five_nine(int array[], int n)'

The format requires 2 files,

main.cpp:

#include <string>

extern void two_five_nine(int array[], int n, int& num_twos, int& num_fives, int& num_nines); 

int main() {
    int arr[] = {2, 5, 9, 2, 9, 9, 5, 2};
    int size = sizeof(arr) / sizeof(arr[0]);
    int num_twos, num_fives, num_nines; 
    two_five_nine(arr, size, num_twos, num_fives, num_nines); 
    std::cout << "2:" << num_twos << ";5:" << num_fives << ";9:" << num_nines << ";" << std::endl;
    return 0;
}```

and function.cpp:

```void two_five_nine(int array[], int n, int& num_twos, int& num_fives, int& num_nines) {
    num_twos = 0;
    num_fives = 0;
    num_nines = 0;
    
    for (int i = 0; i < n; ++i) {
        switch (array[i]) {
            case 2:
                num_twos++;
                break;
            case 5:
                num_fives++;
                break;
            case 9:
                num_nines++;
                break;
        }
    }
}
high relicBOT
#

When your question is answered use !solved 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.

fallen swift
#

They compile and execute fine in VSCode, but the autograder returns this error:

All files found, +0 marks.
Successfully compiled program.out with files main-2-3.cpp function-2-3.cpp. +1 marks
program.out failed to compile using files submission/function-2-3.cpp source/test-2-3.cpp
Compiler stdout:


Compiler stderr:
/tmp/cczHoPEh.o: In function main': test-2-3.cpp:(.text.startup+0x72): undefined reference to two_five_nine(int*, int)'
test-2-3.cpp:(.text.startup+0x96): undefined reference to `two_five_nine(int*, int)'
collect2: error: ld returned 1 exit status

Does anyone have any idea as to what could the problem be here?

empty bough
#

Looks like an include problem

#

function.cpp does not see the declaration of

extern void two_five_nine(int array[], int n, int& num_twos, int& num_fives, int& num_nines);
in main.cpp

#

yes, it's annoying, yes, the C++ compiler is dumb for not detecting that and there are a bunch of reasons

#

the solution is to put the declaration in a .hpp file

#

and include it in both

fallen swift
#

i dont think the auto grader would accept that, it was very specific about the format it wants everything to be uploaded in

#

but i'll try it

empty bough
#

I can only tell you the issue and how it's usually solved;
Usually in C++ "declarations" are defined in .hpp files, and then "definitions" are defined in .cpp files where the .cpp file (and any other file that needs to call the function or use the struct) includes the .hpp file

#

Each .cpp file is basically... its own run of the compiler, blind to all other runs

#

And including other .cpp files isn't an option, because when all runs finish it'll have (identical) duplicates that will conflict (yes it's dumb)

fallen swift
#

used this in the header

#ifndef TWO_FIVE_NINE_HPP
#define TWO_FIVE_NINE_HPP

void two_five_nine(int array[], int n, int& num_twos, int& num_fives, int& num_nines);

#endif
``` and included it in both main.cpp and function.cpp, seems to compile just fine on vscode but autograder returns the same error
empty bough
#

Adding a header guard to a .cpp file is... not a thing

fallen swift
#

Oh

empty bough
#

I mean it is

#

but like

#

it's not a thing that people do

fallen swift
#

so just
void two_five_nine(int array[], int n, int& num_twos, int& num_fives, int& num_nines); then?

empty bough
#

no

#

Explain what the required format is

#

2 files?

#

do they both need to be .cpp files?

fallen swift
#

Mhm

#

Two files, one called main-2-3.cpp and the other called function 2-3.cpp

empty bough
#

That does not answer my question at all

#

ah

#

I see, that's why you use extern

fallen swift
#

I assumed using extern would work, as it did for the previous questions

#

but it's giving me this error when the code runs just fine

empty bough
#

I'm gonna be honest, I never use extern

#

but here is the same issue as you have, solved

fallen swift
#

is it because i havent used #include "function-2-3.cpp" in main...?

#

Nevermind

#

Lemme read it thoroughly

fallen swift
empty bough
#

Yea idk your error message says

two_five_nine(int, int)
meaning it's somehow trying to look for a function call where you call it with 2 numbers

fallen swift
#

worth posting on stackoverflow or should i assume the autograder is having an episode

empty bough
#

idk

fallen swift
#

oh my god

#

i used the wrong signature

#

i forgot to include

#

the signature

#

they wanted

#

Signature: void two_five_nine(int array[], int n)

#

edhferbvhgjf

#

@empty bough thank you for the help i'm just dumb LOL

#

!solved