Thread: Student: Help understanding a program

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    3

    Student: Help understanding a program

    I am currently a student studying c programming and specifically for the final. My teacher posted a final review which I mostly understand except for one program. I am supposed to show what the program displays when it is run.

    Code:
    #include <stdio.h>#include <stdlib.h>
    #define INPUT_LEN 100
    #define ARRAY_SIZE 8
    typedef struct {
    	char code;
    	short val;
    } VAL_T;
    static VAL_T G_vals[ARRAY_SIZE] = { { 'M', 1000 },
    { 'D', 500 },
    { 'C', 100 },
    { 'L', 50 },
    { 'X', 10 },
    { 'V', 5 },
    { 'I', 1 },
    { '*', 0 } };
    short get_val(char code) {
    	short i = 0;
    	G_vals[ARRAY_SIZE - 1].code = code;
    	while (G_vals[i].code != code) {
    		i++;
    	}
    	if (i == ARRAY_SIZE - 1) {
    		return 0;
    	}
    	else {
    		return G_vals[i].val;
    	}
    }
    int compute_value(char *str) {
    	char *p;
    	int val = 0;
    	int next_val;
    	int remainder;
    	if (str && *str) {
    		p = str;
    		p++;
    		if (*p == '\0') {
    			val = get_val(*str);
    		}
    		else {
    			remainder = compute_value(p);
    			val = get_val(*str);
    			next_val = get_val(*p);
    			if (val < next_val) {
    				val = remainder - val;
    			}
    			else {
    				val = remainder + val;
    			}
    		}
    		printf("Evaluating %s = %d\n", str, val);
    	}
    	return val;
    }
    int main() {
    	printf("%d\n", compute_value("MCMXCIV"));
    	getch();
    	return 0;
    }
    I put it into a program and the output is:
    Code:
    Evaluating V=5
    Evaluating IV=4
    Evaluating CIV=104
    Evaluating XCIV=94
    Evaluating MXCIV=1094
    Evaluating CMXCIV=994
    Evaluating MCMXCIV=1994
    1994
    I am trying to understand the program but I can't seem to get it all. If you could explain each function separately that would be appriciated, that is how I normally understand programs.
    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,596
    If you want to understand it, start with something like
    Code:
    short get_val(char code) {
        short i = 0;
        printf("DEBUG: Called get_val(%c)\n", code );
        // rest of code
    Add more printf("DEBUG" to your taste.

    Or, use a debugger to single-step through the code. This gives you the ability to inspect far more things about the program state.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    582
    +1 for stepping through it in a debugger. Always helpful.

    I'm guessing that it's the recursion in compute_val that is causing confusion. Note that get_val isn't called at all until compute_val has reached the end of the string.

    I'd suggest grabbing a pen and paper and working through the processing of a simple string (e.g. XVI). Write down the chain of function calls by hand, making sure to note the parameters passed and value returned. It should hopefully start to make sense then.

    The variable name "remainder" is an odd choice and is probably not helping. Also, no idea why get_val stores the code for the numeral at the end of its array.

    Shout if you have any specific questions. If what I wrote makes no sense to you, go read about recursive functions first

  4. #4
    Registered User
    Join Date
    Mar 2015
    Posts
    3
    Thanks for the replies. I think I understand it now (thanks to you guys and a classmate) the get_val finds the input, which is given in the printf, and compares that to the static array which is the equivalents for roman numerals. Then the comput_value function uses recursion taking the whole input and working towards the end, using pointers one step ahead of the actual array slot, to find the '/0'. It then takes the first value, which is V, and since *p=='/0' just prints the value for V, which is 5. It then goes one space back for IV where is uses the second else to find the remainder, current value, and compare them like you would for roman numerals to find the outcome (since I=1 and V=5 and I is before V and less than V the val=5-1=4. And it continues until the input string reaches the beginning, then just posting the final val.

    Thanks for the help, I know you didn't have to help a student but thank you. I now feel less worried about the final.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    It should show something refering to the fact that you've used getch() without including conio.h :P
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 11-16-2013, 06:18 PM
  2. Student records program
    By mugiwara528 in forum C Programming
    Replies: 3
    Last Post: 03-14-2011, 02:53 AM
  3. c program on grades of a student
    By galmca in forum C Programming
    Replies: 14
    Last Post: 09-29-2004, 03:07 PM
  4. Student Grade Program
    By Vinod Menon in forum C Programming
    Replies: 1
    Last Post: 05-31-2004, 12:32 AM
  5. Student Grade Program
    By Vinod Menon in forum C Programming
    Replies: 5
    Last Post: 05-28-2004, 02:49 PM