# Optimize?

• 12-20-2011
nuko
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.
• 12-20-2011
jimblumberg
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:
Quote:

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
• 12-20-2011
nuko
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?
• 12-21-2011
alegro
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.