#arrays/functions help
81 messages · Page 1 of 1 (latest)
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 run !howto ask.
the logic is sound
but the question asks you to count the number of same elements not print the same elements
how would i find the number of same elements?
how would you print when you encounter the same element
im not sure tbh
by looking at them?
looking at what
the elements in the array
which elements
the ones that are the same idk 😭
would you compare vec1[0] and vec2[1]?
i.e would you compare the first and second element of first and second arrays respectively?
well that's the logic
so you'd compare
vec1[0] and vec2[0]
vec1[1] and vec2[1]
vec1[2] and vec2[2] and so on
ya
if they're equal
then you know that they're matching elements
try to figure out the rest on your own
👍 ty
if you get stuck feel free to ping
@celest briar Has your question been resolved? If so, run !solved :)
for my function, would i initialise a 2d array and then put that array in my function?
why do you feel you need a 2d array?
yep
srry but im rlly confused as to what to put in my function, do i just put vector1 and vector2 in it?
since this is C, arrays passed to functions decay into pointers
so your function will have no way of knowing what is the size of the arrays
so your arrayComparison function will need to have int vec1[], int vec2[] and int size parameters
fortunately, the size of the two vectors is specified by a constant, so you can just pass that into the function as well
instead of vector1[10] and vector2[10] you should use vector1[SIZE] and vector2[SIZE]
ic
where size is specified using ```c
#define SIZE 10
since the question asks for it
it's a preprocessor directive
mhm
so it doesn't matter where you define it
but convention is to define it at the top
since it'll be used throughout the program
so it would be like this
it's your function definition so uh
you'd have a normal variable called int size
you would use SIZE when you're actually calling the function
if you were to use SIZE in your function definition, your function wouldn't be able to use it as a variable
when the preprocessor goes through your code
it would replace all occurences of SIZE with 10
so your function would look like
int arrayComparison(int vec1, int vec2, 10)
which doesnt make sense right?
yeah
also
int vec1 means the function will be expecting a single integer
if you want to pass in an array, you would have to write int vec1[]
or int *vec1
in the function definition?
try to write the complete program once
and then we'll talk over it if there are any issues
you said to have a normal variable called int size and use it in the function definition
like so?
int size, yes
the int size at the top is not what i meant
the size variable will be used in the arrayComparison function
so it should be local to it
yeah
!solved