cs 202 computer science ii lab

12
CS 202 Computer Science II Lab Fall 2009 September 10

Upload: medge-hendrix

Post on 31-Dec-2015

19 views

Category:

Documents


1 download

DESCRIPTION

CS 202 Computer Science II Lab. Fall 2009 September 10. Today Topics. Reusing programs (making header files) Script mounting your USB Copy-Paste in vi. Reusing Programs. Why reuse? Copy-paste the code Advantages: NONE Disadvantage: a lot!! Using header files - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 202 Computer Science II Lab

CS 202Computer Science II Lab

Fall 2009September 10

Page 2: CS 202 Computer Science II Lab

Today Topics

• Reusing programs (making header files)• Script• mounting your USB• Copy-Paste in vi

Page 3: CS 202 Computer Science II Lab

Reusing Programs

• Why reuse?• Copy-paste the code– Advantages: NONE– Disadvantage: a lot!!

• Using header files• Object-oriented programming

Page 4: CS 202 Computer Science II Lab

Header file example…

• Create headerExample directory– *do not delete files

• Create basicMath.h file :

1. /* basicMath.h */2. 3. int add (int a, int b);

4. int mul (int a, int b);

Page 5: CS 202 Computer Science II Lab

.Header file example..• Create basicMath.cpp file :

1. /* basicMath.cpp */

2. int add (int a, int b) {

3. return a+b;

4. }

5. int mul (int a, int b) {

6. return a*b;

7. }

Page 6: CS 202 Computer Science II Lab

..Header file example.• Create main.cpp file :1. #include <iostream>

2. #include "basicMath.h"

3. using namespace std;

4. int main(int argc, char ** argv) {

5. int a, b;

6. cout << " Name: Your-Name " << endl;

7. cout << " Section: Your-Section-No " << endl;

8. if (argc > 2) {

9. a = atoi(argv[1]);

10. b = atoi(argv[2]);

11. } else {

12. a = 1;

13. b = 2;

14. }

15. cout << a << " + "<< b << " = " << add(a, b) << endl;

16. cout << a << " * "<< b << " = " << mul(a, b) << endl;

17. return 0;

18. }

Page 7: CS 202 Computer Science II Lab

…Header file example

– g++ -c main.cpp

– g++ -c basicMath.cpp

– Or:

– g++ -c *.cpp

– g++ -o main main.o basicMath.o

– ./main 12 5 > output.out

Page 8: CS 202 Computer Science II Lab

Exercise• After successfully running the program, add the

following function to “basicMath.cpp”, and try to make it work by editing main.cpp & basicMath.h

int min (int a, int b) {

return (a < b) ? a : b;

}

• Testing Program– ./main

– ./main > output.out

• Print output.out– lpr output.out

Page 9: CS 202 Computer Science II Lab

Script

• Script [file name] … exit• Default file name: typescript

Page 10: CS 202 Computer Science II Lab

mounting your USB

• df -T to see list of mounted devices

• mount /dev/fd0 /mnt/floppy• mount /dev/cdrom /mnt/cdrom• mount /dev/sda1 /mnt/usbdisk

Page 11: CS 202 Computer Science II Lab

Copy-Paste in vi

• (n)yy (n) lines to buffer• y(n)w yanks (n) words to buffer

• p puts yanked or deleted text after cursor• P puts yanked or deleted text before

cursor

Page 12: CS 202 Computer Science II Lab

Questions?