Week3_misc.pptx

12
CS1010: Programming Methodology http://www.comp.nus.edu.sg/~cs1010/ Aaron Tan

description

asdf

Transcript of Week3_misc.pptx

Page 1: Week3_misc.pptx

CS1010: Programming Methodology http://www.comp.nus.edu.sg/~cs1010/

Aaron Tan

Page 2: Week3_misc.pptx

Week 3: Miscellany (1/2)

CS1010 (AY2013/4 Semester 1)

You are urged to explore on your own, as it is impossible for us to cover every detail in class. Often during exploration, you may encounter problems or have

questions you need clarification on. We encourage you to use the IVLE forums to post your queries,

instead of emailing us, so that anybody can help answer your queries and the answer can be read by all.

Seniors are also welcome to post in the forums, and in the past they did post useful materials for you.

So, please use the forums to help yourself and your future juniors!

Week3 Miscellany - 2

Page 3: Week3_misc.pptx

Week 3: Miscellany (2/2)

CS1010 (AY2013/4 Semester 1)

Some of the topics here are related to this week’s lecture topics, but are taken out of the lecture slides to keep the lecture focused.

Some of the topics here are collected from discussion in past years’ IVLE forum.

Note that sometimes the discussion here may include materials that have not been covered yet, or are outside the scope of CS1010. Do not be too concerned about whether a topic is in or outside

the syllabus. Sometimes, there is no clear distinction. It is always good to know that little extra, even if it is outside the syllabus, as it will strengthen your knowledge on the whole.

Week3 Miscellany - 3

Page 4: Week3_misc.pptx

1. return statement in main( )

CS1010 (AY2013/4 Semester 1)

Q: Why do we write return 0; in our main() function? Answer:

Our main() function has this headerint main(void)

Hence it must return an integer (to the operating system) The value 0 is chosen to be returned to the operating system

(which is UNIX in our case). This is called the status code of the program.

In UNIX, when a program terminates with a status code of 0, it means that it is a successful run.

You may optionally check the status code to determine the appropriate follow-up action. In UNIX, this is done by typing echo $? immediately after you have run your program. – You do not need to worry about this.

Week3 Miscellany - 4

Page 5: Week3_misc.pptx

2. Default return type (1/3)

CS1010 (AY2013/4 Semester 1)

Program can be compiled, but with warnings:

Week3 Miscellany - 5

1 #include <stdio.h> 2 3 int main(void) { 4 printf("%d\n", f(100, 7)); 5 return 0; 6 } 7 8 f(int a, int b) { 9 return a/b;10 }

A ‘type-less’ function has default return type of int

warning: implicit declaration of function 'f' line 4 (due to absence of function prototype)warning: return type defaults to 'int' line 8

Page 6: Week3_misc.pptx

2. Default return type (2/3)

CS1010 (AY2013/4 Semester 1)

Program can be compiled, but with warnings:

Week3 Miscellany - 6

1 #include <stdio.h> 2 3 int main(void) { 4 f(100, 7)); 5 return 0; 6 } 7 8 void f(int a, int b) { 9 printf("%d\n", a/b);10 }

Another example

warning: implicit declaration of function 'f' line 4 (due to absence of function prototype)warning: conflicting types for 'f'

Without function prototype, compiler assumes function f to be an int function when it encounters this.

However, f is defined as a void function here, so it conflicts with above.

Page 7: Week3_misc.pptx

2. Default return type (3/3)

CS1010 (AY2013/4 Semester 1) Week3 Miscellany - 7

Tips: Provide function prototypes for all functions Explicitly specify the function return types for all functions

1 #include <stdio.h> 2 3 int f(int, int); 4 5 int main(void) { 6 printf("%d\n", f(100, 7)); 7 return 0; 8 } 9 10 int f(int a, int b) {11 return a/b;12 }

Page 8: Week3_misc.pptx

3. CodeCrunch: How to make use of the input and output files?

CS1010 (AY2013/4 Semester 1)

Your program works on interactive inputs, but CodeCrunch executes your program by redirecting the input data from a text file. This way, it can test you program by reading input data from

different text files, one at a time.

You can do this in UNIX using input redirection <. Assuming that you have copied the input text file set1.in

into your directory, you can type: a.out < set1.in

Likewise, you may also use output redirection > to redirect output to a text file instead of to the screen:

a.out < set1.in > myset1.out

Week3 Miscellany - 8

Page 9: Week3_misc.pptx

4. CodeCrunch: using ‘diff’ to compare

CS1010 (AY2013/4 Semester 1)

You may then use the diff command in UNIX to compare your output file myset1.out with the correct output file set1.out provided by CodeCrunch:

diff myset1.out set1.out

If the two files are identical, no output will be produced by the diff command.

This is handy in cases where the differences between your output and the model output are not visible to the eyes, for example, trailing spaces in an output line.

Week3 Miscellany - 9

Page 10: Week3_misc.pptx

5. CodeCrunch: Program failing all test cases

CS1010 (AY2013/4 Semester 1)

Q: I tested my program and it works well, but when I submit it to CodeCrunch, it fails all the test cases! Why?

This is a very commonly encountered problem once students start to submit their programs to CodeCrunch.

A very likely reason is that you have forgotten to initialise some variable properly. Remember that an uninitialised numeric variable may not contain zero.

Correct your program and resubmit to CodeCrunch! Some students just ignored CodeCrunch feedback and did

nothing to correct their program when it fails all test cases. Don’t do this!

Week3 Miscellany - 10

Page 11: Week3_misc.pptx

6. Filenames to avoid in naming your executable files

CS1010 (AY2013/4 Semester 1)

Q: I compiled my C program into an executable file called test, but I can’t run it. Why?

The reason is that test is a UNIX built-in command. When you type test you are running this UNIX command instead of your executable program!

Tips To run the program, prefix it with ./, i.e., type this instead:

./test make is another commonly used name that should be avoided. Avoid naming your executable files with names that are UNIX

commands. (If your program can’t run, check that it doesn’t have the same name as a UNIX command.)

Week3 Miscellany - 11

Page 12: Week3_misc.pptx

End of File