We just read about Functions and Arrays, and I'm still a little confused on those as well as passing a function by reference and calling it. I've been trying to get help from my online teacher, but he's not entirely helpful on the things that I don't completely understand, and most of my classmates have the same problems.

For this lab, I understand the concept a little better than the past few that we've done, but I'm unsure as of how to go about "filling in the blanks".
I obviously do not want somebody to just give me all the answers, as I wouldn't be learning anything by this, and I do want to learn what I need to be doing. What I would hopefully like would be some hints or nudges in the right direction to go with this. I'm just unfortunately rather confused, and can't seem to get any help. I'm getting started in programming, and I have bought myself C for Dummies in addition to my class book. I think one of my difficulties is that what we read in our books, and then have a lab to do, aren't as similar as they could be, and the directions as to how to go about them are sometimes unclear. Maybe it's just because I'm a newbie here. Anyway.

We are supposed to write a C program that will decode a secret message, that is 26 characters in length, and is given as a series of numbers, which need to be sorted and compared against a translation set of characters in order to decode it.

These are the instructions:
Perhaps the easiest way to show how to decode a message is to first show how a message is encoded. First, the letters of the alphabet, the numbers, and any
punctuation is scrambled into some sort of sequence.

The string below can be found in codefile.txt:

CFL2AP(T9W,H!JEM86ND0BZ7UK1Y3VG4XR)ISOQ5. ;-

Notice that the above string contains 52 characters (including the nine blanks near the end). Now, let's suppose that we want to encode the message:

HELP ME!

Looking up each of the letters in the above string we find that H is the 11th character (zero-based indexing), E is the 14th, L the 2nd, P the 5th, one of the blanks the 41st, M the 15th, E the 14th, and ! the 12th. Therefore, our initial coding is:

11 14 02 05 41 15 14 12

Next, each number is assigned a three digit sequence number, in ascending order, to precede it. These numbers indicate relative position and need not be consecutive. For example, we might assign the following numbers:

10111
12214
12802
12905
13541
13715
14214
15112

Finally, the order of the numbers is scrambled:

13541
12214
10111
15112
13715
12802
14214
12905

This is the list of numbers you would be given to decode. To decode a message, simply reverse the process: read the numbers into an array and sort them into ascending order.
"Cut" each sorted number into two, using the last two digits as the index to the correct character and print the character.

For this lab, the encoded message in numeric format is given below and can be found in: msgfile.txt:

19546
14501
17309
13027
16149
21512
18035
14014
15700
12515
16514
18207
13407
14837
16842
21037
15333
13244
21224
16321
14146
16014
20727
12804
18811
13711

Given below is the main body of the program that you should use. For extra credit, pass the file names for this lab as command-line arguments.

int main(void) {
    char code[MAX];
    int msg[MAX];
    int msgSize;

    getCode(code);
    msgSize = getMessage(msg);
    sortMessage(msg, msgSize);
    decodeMessage(code, msg, msgSize);
    return 0;
}

Note that the string used to hold the 52 characters read from codefile.txt should be declared to be of length 53 (to reserve a space for the NULL character).
Here is a function that will sort an integer array:

void sortMessage(int msg[], int msgSize) {
    int i, j, temp;

    for (i = 1; i < msgSize; i++) {
        temp = msg[i];
        j = i - 1;
        while (j >= 0 && temp < msg[j]) {
            msg[j+1] = msg[j];
            j = j - 1;
        }
        msg[j+1] = temp;
    }
}

I'm not even going to try confusing myself more by doing the command-line file arguments, but I've been trying to wrap my brain around the function we were given as to how to work that out.
I think the main thing I'm not entirely sure of is how to "cut" the numeric format down and get the last two digits for the "secret message".
I have the two txt files at the start of my program, as:
FILE *msgfile = fopen("msgfile.txt", "r");
FILE *codefile = fopen("codefile.txt", "r");
And I know I need to implement them in for my arrays as a scanf (or at least, I'm assuming), and I feel like I have this thing by the tips of my fingers but I can't quite grab it.
I think I may simply be confused by the way the function and main body frame are laid out, and I need to fill it out with the proper functions and loops, and I just am unclear as to what exactly is needed.
I'm figuring out that a for loop will be needed, and that I definitely need to define the max of the char code and int msg.
Should I have the char code max and int msg max be the same, 26? Or, according to the requirements, should the code be 53 and the msg be 26?

If anyone can give me some pointers here so I can figure this out I would extremely grateful.
If what I'm asking isn't proper, I'm sorry in advance, and I will see if I can find some help elsewhere, I'm just unsure of where to go with this, and I really want to figure this all out.

I think the main thing I'm not entirely sure of is how to "cut" the numeric format down and get the last two digits for the "secret message".

Use division and remainder math:

19546 / 100 = 195
19546 % 100 = 46

It's easier to keep the numbers stored as integers so that you can use them as indexes in the key lookup without doing a string -> integer conversion. Of course, if the message is long enough for the sequence number to overflow and integer you'll need to approach the problem differently, but I don't think that'll happen provided you use long int as the data type (note that int is only guaranteed to be at least 16 bits).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.