Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
You’ve been asked to write a C program to satisfy the following three requirements:
• Requirement 1: Ask the user to enter the course code.
• Requirement 2: If the course code matches one from the list shown in Table 2, then
the message shown in bolt below should be displayed to the user. For example, let’s
assume that the user enters the 3093 code.
EEE3093: Course level = 3, Credits = 16
• Requirement 3: If the course code does not match one from the list shown in Table 2,
then the message shown in red below should be displayed to the user. For example,
let’s assume that the user enters an incorrect code of 3193.
An invalid course code of EEE3193 was entered

Number Code Course level Number of credits
1      1007          1        12
2      2047          2        16
3      2044          2        16
4      2046          2        16
5      3096          3        16
6      3095          3        18
7      3093          3        16
8      3100          3        16
9      3097          3         8
10     3094          3        16
11     3099          3         8
12     3098          3         8
13     4006          4         8
14     4051          4         8
15     4123          4         8 
16     4122          4         8
17     4022          4        40


What I have tried:

C++
#include <stdio.h>
#include <stdint.h>

int main()

{   int code, found = 0;

    int courses [3][17] = {
        {1007,2047,2044,2046,3096,3095, 3093, 3100, 3097, 3094, 3099, 3098,4006, 4051,4123,4122, 4022},
        {1,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4},
        {12,16,16,16,16,18,16,16,8,16,8,8,8,8,8,40}};




    printf("Enter course code ");
    scanf("%d", &code);


        for (int j =0; j<17;  j++)
        {
            if (courses[0][j] = code )

            {

                printf("EEE%d: Course level = %d, Credits = %d", courses[0][j],courses[1][j],courses[2][j]);
                found = 1;
                break;
            }
        }
        if (found == 0)
        {
            printf("An invalid course code of EEE%d was entered",code);
        }




    return 0;

    }
Posted
Updated 21-Oct-20 7:16am
v5

Quote:
I canot finish my code because now am getting confused in terms of calling the index in my array

Rather simple: This
C++
Number Code Course level Number of credits
1      1007          1        12
2      2047          2        16
3      2044          2        16
4      2046          2        16
5      3096          3        16
6      3095          3        18
7      3093          3        16
8      3100          3        16
9      3097          3         8
10     3094          3        16
11     3099          3         8
12     3098          3         8
13     4006          4         8
14     4051          4         8
15     4123          4         8 
16     4122          4         8
17     4022          4        40

is a 2D array, it is made of 17 rows by 3 columns.
To address a cell, you must tell which row and which col
C++
courses[row, col]

code find the course should be:
C++
for (i= 0; i< 17; i++ )
{
    if (strcmp(code, courses[i, 0])  == 0 )
    {
        found = 1;
        break;
    }
}

because course code is in column 0 (first column)
Your code have other flaws.

Advice: take time to learn C properly, there is tonnes of tutorials around.
Teaching you C is out of scope of this little textbox.
 
Share this answer
 
v2
Presumably if you get to the end of the array, and you haven't found the code, you can print the error message. So your task now is to think a little bit about how you might decide if the course number was found, and then what to do when you scanned all the course numbers.

That being said, there's several coding errors with what you've show us. If this is a cut & paste of your code, then clearly it hasn't been run through a compiler yet. Do that, and then fix the errors (and warnings). At that point you should have a program that at least will report if you entered a valid code or not. Then you can go ahead and think about printing the invalid error code.

Some things to think about: Once you've found a valid code, why not stop comparisons right away, rather than continuing on with, now pointless, comparisons.
Comparing strings is slow, comparing ints is fast, maybe course numbers, at least, should be ints?
Perhaps a struct might be a better data structure to hold the course information, rather than a multi-dimension array?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900