#arrays/functions help

81 messages · Page 1 of 1 (latest)

celest briar
#

Hey, so I have this exercise in my lab and I'm very confused as to where I should start. Could I get some help?

I was thinking to create a function that loops through the 2 arrays and an if statement in that loop which prints any equal integers. Is that a good idea?

visual idolBOT
#

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.

bronze torrent
#

but the question asks you to count the number of same elements not print the same elements

celest briar
bronze torrent
celest briar
bronze torrent
#

okay

#

as a human

#

how would you know if two elements in the arrays are matching

celest briar
#

by looking at them?

bronze torrent
#

looking at what

celest briar
#

the elements in the array

bronze torrent
#

which elements

celest briar
#

the ones that are the same idk 😭

bronze torrent
#

would you compare vec1[0] and vec2[1]?

#

i.e would you compare the first and second element of first and second arrays respectively?

celest briar
#

no

#

you'd compare the same elements

#

of both arrays

bronze torrent
#

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

celest briar
#

ya

bronze torrent
#

if they're equal

#

then you know that they're matching elements

#

try to figure out the rest on your own

celest briar
#

👍 ty

bronze torrent
#

if you get stuck feel free to ping

visual idolBOT
#

@celest briar Has your question been resolved? If so, run !solved :)

celest briar
bronze torrent
#

why do you feel you need a 2d array?

celest briar
#

oh

#

is it just 2 1d arrays?

bronze torrent
#

yep

celest briar
# bronze torrent yep

srry but im rlly confused as to what to put in my function, do i just put vector1 and vector2 in it?

bronze torrent
#

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]

celest briar
#

ic

bronze torrent
#

where size is specified using ```c
#define SIZE 10

#

since the question asks for it

celest briar
#

and would i define size in the function?

#

or outside

bronze torrent
#

it's a preprocessor directive

celest briar
#

mhm

bronze torrent
#

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

celest briar
#

so it would be like this

bronze torrent
#

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?

celest briar
#

yeah

bronze torrent
#

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

celest briar
#

mhm

#

but what do i do with the size variable, then?

#

do i just initialise it as 10

bronze torrent
#

try to write the complete program once

#

and then we'll talk over it if there are any issues

celest briar
#

like so?

bronze torrent
#

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

celest briar
#

mhm

#

ill write the program and send it, i'd rather go through every mistake at once

bronze torrent
#

yeah

celest briar
#

!solved