Thread: Optimize?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    2

    Optimize?

    Hey everyone,
    I tried to write a program wich scans some strings from stdi, stores them in arrays then from an array ("days") reads the chars and prints the day of the week according to the abreviation (MTWRF).

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int abvToString(char days);
    
    int main(int argc, const char *argv[])
    {
        char *dept[5];
        char days[5];
        char *wDays[] = {"Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays"};
        int d, l, j;
        int course_num;
        int time;
    
        //Getting input from user (MATH 1270 TR 800)
        printf("Enter dept code, course number, days and time like so:\n>COSC 2060 MWF 1410\n>");
        scanf("%s%d%s%d", dept, &course_num, days, &time);
    
        printf("%s %d meets ", dept, course_num);
    
        //Obtaining length of "days" array to loop from 0 to d
        d = strlen(days);
        for(j = 0; j <= d; j++){
    
            //Return value is the index for "wDays" array
            l = abvToString(days[j]);
            printf("%s ", wDays[l]);
    
        }
        printf("at %d\n", time);
    
        return 0;
    }
    
    int abvToString(char days){
    
        const char abv[] = {'M', 'T', 'W', 'R', 'F'};
    
        switch(days){
            case 'M':
            case 'm':
                return 0;
                break;
            case 'T':
            case 't':
                return 1;
                break;
            case 'W':
            case 'w':
                return 2;
                break;
            case 'R':
            case 'r':
                return 3;
                break;
            case 'F':
            case 'f':
                return 4;
                break;
            default:
                break;
    
        }
    
    }
    Sample run:
    [me@host C]$ ./fig9-2
    Enter dept code, course number, days and time like so:
    >COSC 2060 MWF 1410
    >MATH 1270 TR 800
    MATH 1270 meets Tuesdays Thursdays at 800


    Two things I don't understand:
    1. When I change
    Code:
    int main(int argc, const char *argv[])
    to
    Code:
    int main(void)
    I get MATH 1270 meets Tuesdays Thursdays H��� at 800 as output.

    2. MATH 1270 meets Tuesdays Thursdays at 800
    prints an extra blank space before the "at 800"

    Its probably messy.
    Is there a better way of solving the problem?
    Any suggestion is welcome, thanks.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If you are not getting any warnings, you need to check your compiler settings and insure warnings are enabled. Here are the warnings my compiler generated:
    main.c||In function ‘main’:|
    main.c|17|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’|
    main.c|19|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’|
    main.c|6|warning: unused parameter ‘argc’|
    main.c|6|warning: unused parameter ‘argv’|
    main.c||In function ‘abvToString’:|
    main.c|37|warning: unused variable ‘abv’|
    main.c|65|warning: control reaches end of non-void function|
    ||=== Build finished: 0 errors, 6 warnings ===|
    The first two warnings are probably the cause of your problems.

    Jim

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    2
    Thanks, I forgot to add -Wall -Werror flags to gcc

    Changed:
    Code:
    char *dept[5];
    To:
    Code:
    char dept[5];
    And removed the abv array

    Prints just fine after that.

    In the abvToString function I'm not sure what exactly to add at the end , since:
    Code:
    fig9-2.c:62:1: error: control reaches end of non-void function [-Werror=return-type]
    Any suggestion on optimizing?

  4. #4
    Registered User
    Join Date
    Dec 2011
    Location
    saskatoon
    Posts
    3
    All of your return statements are within test cases. Therefore incase the function never executes any of the test cases the function still needs to return something.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Optimize server
    By kargo in forum C++ Programming
    Replies: 11
    Last Post: 03-28-2011, 12:19 PM
  2. Please help me optimize my code
    By lazyme in forum C++ Programming
    Replies: 3
    Last Post: 01-25-2010, 04:05 AM
  3. Help me optimize this function
    By h3ro in forum C++ Programming
    Replies: 15
    Last Post: 02-24-2008, 06:40 PM
  4. Help me optimize
    By sand_man in forum Game Programming
    Replies: 14
    Last Post: 05-10-2005, 05:19 PM
  5. How to Optimize
    By cfrost in forum C++ Programming
    Replies: 5
    Last Post: 11-09-2004, 09:07 AM