#Getting CourseName, Approaching searching name, from integer input

146 messages · Page 1 of 1 (latest)

split beacon
#
/* Task 4 */
int getCourseName( int courseNum, char cNameFound[MAX_STR], char courseName[NUMBER_COURSES][MAX_STR], int courseID[NUMBER_COURSES]) {
    /* This function, takes a course number as input, searches for its course name and stores it in
    a string parameter (e.g. cNameFound). It returns 1 if the course is found, 0 otherwise. For
    example, if the course number is 1300, it will return 1 and store “Programming” in
    cNameFound*/

    // initialize a var to check if a course is found
    int courseFound = 0; ```

For reference, below are the coursenames im dealing with, and the course numbers:

Programming
Introductory Programming
Object Oriented Programming
Data Structures
Software Systems Development and Integration
Intro to Databases
Analysis and design of Algorithms
Advanced OO
Data Mining
Cyber Security
1300
1500
2430
2520
2750
3530
3490
4450
4600
4750 ```

raw stormBOT
#

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.

split beacon
#

In general, im sort of new to programming, specficially problem solving and am trying to get a grasp on how to tackle problems like this

#

Ive started out by understanding that we'd need an accumulater, specficially courseFound = 0; so we could increment it when a course is found

#

but im stumped on how to move forward from this

unborn violet
#

Are you sure you need an accumulator ? You just need to look at each course id, and if it matches, write the course name and then return

#

And if there's not match, return a different value

split beacon
#

im having trouble understanding how i would know what to loop through

#

and sort of approach this problem

unborn violet
#

You need to loop through coursename and courseid at the same time

split beacon
#

oh so a nested loop

unborn violet
#

no

#

well not sure what you mean

split beacon
#

when you suggested looping at the same time, i thought of a nested loop lol

unborn violet
#

sry 😅

split beacon
#

but ig that wouldnt work

#

nah all good

#

ig im trying to figure out what questions i should be asking myself when tackling these types of problems, thinking the right way and stuff

unborn violet
#

I think you should just see it as finding a match in one array

#

you could see it as an array of structures that was split

split beacon
#

so id need to first see if courseID and the courseNum actually match in the first place

unborn violet
#

Exactly

split beacon
#

cool

#

seemed straightforward

#
/* Task 4 */
int getCourseName( int courseNum, char cNameFound[MAX_STR], char courseName[NUMBER_COURSES][MAX_STR], int courseID[NUMBER_COURSES]) {
    /* This function, takes a course number as input, searches for its course name and stores it in
    a string parameter (e.g. cNameFound). It returns 1 if the course is found, 0 otherwise. For
    example, if the course number is 1300, it will return 1 and store “Programming” in
    cNameFound*/

    int found = 0;
    for (int i = 0; i < NUMBER_COURSES; i++){
        if (courseID[i] == courseNum){
            found = 1;
        }
    } ```
unborn violet
#

Now you just need to copy the name

split beacon
#

oh so copy the name from courseName ( loop through number of courses again? ) and set that = to cNameFound

unborn violet
#

Well I don't think you need to loop again

split beacon
#

wait that doesnt make sense what am i saying 😭

#

so hypothetically if a course is found

#

then if (found == 1)

unborn violet
split beacon
#

then that means cNameFound is = to coursename i think

#

or would that not make sense

#

ig this is what i got down if it makes sense


    int found = 0;
    for (int i = 0; i < NUMBER_COURSES; i++){
        if (courseID[i] == courseNum){
            found = 1;
            break;
        }
        if (found == 1){
        cNameFound = courseName[i];
    }
    else{
        return 0;
    }
} ```
unborn violet
#

close

#

I don't know if the bracing is correct

split beacon
#

oh so syntax issue

unborn violet
#

I think there is another issue but try fixing the syntax first

split beacon
#

wait yeah youre right

#

if the if statement is in the loop then wouldnt it go multiple times

unborn violet
#

Yup

split beacon
#

but it only need to assgn the name that was found, once

#

so it should be outside the loop

unborn violet
#

Well you need to think about this for a minute

#

If it is outside of the loop, how will you know which one matched since the variable i won't exist anymore?

split beacon
#

oh true

#

thats a good point

unborn violet
#

There's a trick

split beacon
#

so we need i to still exist so we know which ones matched

split beacon
unborn violet
#

just put your copy code after "found = 1"

#

That way you know the entry matched and i exists and has the correct value

#

You don't even need "found" if you return immediately

split beacon
#

ohhh

unborn violet
#

It's also more efficient since you stop searching after you've found a match

split beacon
#

the found var was defnitely uneccesary

  for (int i = 0; i < NUMBER_COURSES; i++)
    {
        if (courseID[i] == courseNum) {
            cNameFound = courseName[i];
            return 1;
            }
            else {
                return 0;
            }
            break;
        }
     
    }```
unborn violet
#

closer

#

but here you return 0 after the first failed match

#

you need to return 0 if after testing every entry, so at the end of the loop

#

Also you can't copy with a single assignement operation

split beacon
#

oh so checking if its false first is better then checking if its true

#

cause it goes through everything

unborn violet
#

not sure what you mean

split beacon
#

returning 0 after the first failed match, as in

if (courseID[i] != courseNum){
  return 0;
} ```
#

is more efficient?

unborn violet
#

no you should not return after a failed match

#

If a match succeeds, you can return since you know the index and thus the name, and after copying, the task is done

#

If a match failed, you need to continue looking to try to find another match

split beacon
#

omg youre right

unborn violet
#

So you can return only when you've checked everything, that is at the end of the loop

split beacon
#
   for (int i = 0; i < NUMBER_COURSES; i++)
    {
        if (courseID[i] == courseNum) {
            cNameFound = courseName[i];
            return 1;
            }
            break;
        }
        return 0;
     
    }
#

here im returning at the very end

unborn violet
#

yup

split beacon
#

so i think this would allow us to go through all the courseIds, to check and see if theres a match

unborn violet
#

but

#

you can't copy the name in a single assignement operation

split beacon
#

ohh

unborn violet
#

you'll need to iterate through the characters

split beacon
#

that makes sense, so thats where the courseNames parameters comes into play

unborn violet
#

well i'm not 100% sure because they are fixed size here

#

gotta test quickly

#

ok so i've tested and another loop is indeed necessary

split beacon
#

i added an inner loop here to go through all the course names

int getCourseName(int courseNum, char cNameFound[MAX_STR], char courseName[NUMBER_COURSES][MAX_STR], int courseID[NUMBER_COURSES])
{
    /* This function, takes a course number as input, searches for its course name and stores it in
    a string parameter (e.g. cNameFound). It returns 1 if the course is found, 0 otherwise. For
    example, if the course number is 1300, it will return 1 and store “Programming” in
    cNameFound*/

    for (int i = 0; i < NUMBER_COURSES; i++)
    {
        if (courseID[i] == courseNum) {
            for (int j = 0; j < NUMBER_COURSES; j++){
                cNameFound = courseName[j];
            }
            return 1;
            }
            break;
        }
        return 0; 
    } ```
#

so if the courseID is = to the courseNum, then we'd check if any of the coursenames are correlated to it

unborn violet
#

nonono

#

you need to copy each character from courseName[i] to cNameFound

split beacon
#

is there a stirng function to copy each character

#

i think i remember someone telling me

#

strcpy?

unborn violet
#

Yes !

split beacon
#

thats clutch

unborn violet
#

Watch out for the layout of two dimensionnal arrays though

split beacon
#

oh would 2D arrays change the way i use strcpy?

unborn violet
#

Well i could make it unusable

#

Also strcpy is meant for null-terminated strings

#

In this case you could use memcpy since the strings are fixed size

#

I've checked and memcpy works

#

strcpy too if the strings are null terminated

split beacon
#

if i were to null terminate it, id have to do it manually right?
with something like
courseName[i][strcspn(courseName[i], "\n")] = '\0';

#

oh but then if we were to use memcpy, then we wouldnt need to manually do that afterwards

#

is that why you suggested it

unborn violet
#

I suggested memcpy because the size of the copy is known at compile time

#

If the strings are not null terminated you would indeed have to do something like that

split beacon
#

oh ok makes sense

unborn violet
#

strcpy would be as good as memcpy (or maybe a bit better), but in this case I would only use it if the strings are already null terminated

split beacon
#

oh seems like memcpy needs the a third argument, the size of the string

#

strlen should do the trick

#
int getCourseName(int courseNum, char cNameFound[MAX_STR], char courseName[NUMBER_COURSES][MAX_STR], int courseID[NUMBER_COURSES])
{
    for (int i = 0; i < NUMBER_COURSES; i++)
    {
        if (courseID[i] == courseNum) {
            // length of courseName below
            int length = strlen(courseName[i]) + 1;
            // copying each char in courseName[i] to cNameFound, so we can store the courseName
            memcpy(cNameFound, courseName[i], length);
            return 1;
            }
            break;
        }
        return 0; 
    } ```
unborn violet
#

Well you don't really need strlen

split beacon
#

oh

unborn violet
#

and by using it you suppose you have null-terminated strings

#

so it comes back to using strcpy

#

but the macro MAX_STR gives you the length of the name

#

use this instead

split beacon
#

but then the MAX_STR is set to i think 50, what if the courseName is less then that

unborn violet
#

Then you'll copy meaningless bits

split beacon
#

Yea

unborn violet
#

not a big issue

#

although if MAX_SIZE was way bigger than the average name, it could become an issue

split beacon
#

these are the names

#

so maybe in the range of 15-30

#

chars

unborn violet
#

Well if you are sure the names are null-terminated, you can choose either one

split beacon
#

fair enough, memcpy should work then to store the courseName in cNameFound i think

 for (int i = 0; i < NUMBER_COURSES; i++)
    {
        if (courseID[i] == courseNum) {
            // copying each char in courseName[i] to cNameFound, so we can store the courseName
            memcpy(cNameFound, courseName[i], MAX_STR);
            return 1;
            }
        }
        return 0; 
    }```
unborn violet
#

What's the break for?

split beacon
#

oh mb left that in from my last try

unborn violet
#

Seems good to me

split beacon
#

Time to test now

#

lets goooo!

#

thanks so much Redclem

#

really appreciate the help

unborn violet
#

No problem, happy to help 😄 🦊

split beacon
#

have any tips for tackling questions like this? did psedocode help kinda understand what it was asking

#

or like commenting line by line and plan out what to do step by step

unborn violet
#

Well i'd recommend pseudocode and commenting

#

And just thinking about and visualising what is required to do

#

And in general just try to solve problems to gain experience

split beacon
#

true

#

will do, thanks again

raw stormBOT
#

@split beacon Has your question been resolved? If so, type !solved :)

split beacon
#

yup!

#

!solved

raw stormBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity