1 / 21

91.102 - Computing II - Problem Session - 1.

91.102 - Computing II - Problem Session - 1. Some Notes on Input/Output . In C++ you used two primary functions to manage console-directed Input and Output. A sample program - similar to one in a C++ text: #include <iostream.h> int main() { int integer1, integer2, sum; // declare vars

Download Presentation

91.102 - Computing II - Problem Session - 1.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 91.102 - Computing II - Problem Session - 1. Some Notes on Input/Output. In C++ you used two primary functions to manage console-directed Input and Output. A sample program - similar to one in a C++ text: #include <iostream.h> int main() { int integer1, integer2, sum; // declare vars cout << “Enter first integer\n”;// prompt user cin >> integer1; // read integer cout << “Enter a second integer\n”; cin >> integer2; // read integer sum = integer1 + integer2; // compute sum cout << “Sum is “ << sum << endl;// output result return 0; }

  2. 91.102 - Computing II - Problem Session - 1. How would we say the same in C? #include <stdio.h> int main() { int integer1, integer2, sum; // declare vars printf(“Enter first integer\n”);// prompt user scanf(“%d”, &integer1); // read integer printf(“Enter a second integer\n”); scanf(“%d”, &integer2); // read integer sum = integer1 + integer2; // compute sum printf(“Sum is %d \n”, sum); // output result return 0; }

  3. 91.102 - Computing II - Problem Session - 1. The two pairs of functions are fairly similar. cin and cout don’t seem to require formatting information; while printf and scanf do. The C++ version is easier to use, while the C version allows more flexibility. Let’s look at a couple more examples of what scanf and printf will let you do. You could have #include <stdio.h> int main() { int integer1, integer2, sum; // declare vars printf("Enter two integers separated by a space\n"); scanf("%d %d", &integer1, &integer2);//read integer sum = integer1 + integer2; // compute sum printf("Sum is %d \n", sum); // output result return 0; }

  4. 91.102 - Computing II - Problem Session - 1. #include <stdio.h> int main() { int integer1; float float1, sum; // declare vars char *st="This is it!"; printf("Enter an integer followed by a floating point number;\n"); // prompt user printf("separate them with a space.\n"); // prompt user scanf("%d %f", &integer1, &float1);// read int and float sum = integer1 + float1; // compute sum printf("First Printout:\n"); printf("Sum of %d and %f is %f.\n", integer1, float1, sum); // output result printf("Second Printout:\n"); printf("Sum of %8d and %6.2f is %8.4f.\n", integer1, float1, sum); // output result printf("Third Printout: %s.\n", st); printf("Sum of %8d and %10.2f is %10.4f.\n", integer1, float1, sum); // output result return 0; }

  5. 91.102 - Computing II - Problem Session - 1. The console interaction looks like Enter an integer followed by a floating point number; separate them with a space. 123 456.789 First Printout: Sum of 123 and 456.789001 is 579.789001. Second Printout: Sum of 123 and 456.79 is 579.7890. Third Printout: This is it!. Sum of 123 and 456.79 is 579.7890. Notice the formatting and lack thereof - the unformatted stuff even shows some of the problems associated with representation precision.

  6. 91.102 - Computing II - Problem Session - 1. • Format Specifiers, after the % character: • Zero or more flag characters (-, +, #, 0, <space>) • Optional minimum field width (decimal integer) • Optional precision specification (decimal integer) • Optional size specification (l, L, or h) • Conversion operator - a single character from a rather large set: c - character; d - decimal digit; o - octal digit; X - hexadecimal digit; f - double; s - *char (a string); etc… • %-#014.5lf • will left justify a (decimal) long double over 14 character position with 5 decimal precision.

  7. 91.102 - Computing II - Problem Session - 1. Some Notes on UNIX. login: password: And then what? You will probably end up in CDE (Common Desktop Environment), which looks quite close to Windows or the Macintosh so that a bit of experimentation will get you to do most of what you need. One of the things you will also need is to open a “console” window that will allow you to execute commands. This console window will allow you to perform housekeeping activities that are somewhat harder to do in the window environment.

  8. 91.102 - Computing II - Problem Session - 1. ls -l: lists the contents of the directory with size and date and other info. ls -a: will list the “dot” files too (all files) rm <filename> : will delete a file rmdir <directory_name> : will delete an empty directory cd <directory_name> : change your current working directory to the named one (you can jump) cd .. : change your current working directory to the one hierarchically just above it

  9. 91.102 - Computing II - Problem Session - 1. When you are bored with the simple commands, man ls, man rm, man rmdir, man cd, Will provide you with all the information you never wanted to know you didn’t know on how to use these commands. Another useful command is chmod. In a multi-user environment you must have some mechanism to allow some users access to your files, while denying access to other users.

  10. 91.102 - Computing II - Problem Session - 1. • Here is the syntax(Abrahams): • chmod modespecsfile • Where file is a given file name and modespecs is either • a comma-separated list of permission changes or • an octal number of up to four digits. • Each permission has 3 parts: one or more ‘who’ letters, an operator, and one or more permission letters.

  11. 91.102 - Computing II - Problem Session - 1. The who letters: u - permission for the file’s user; g - permission for the file’s group; o - permission for others (= rest of the world) a - permission for everyone. Operators: + : add these permissions; - : take away these permissions; = : set EXACTLY these permissions, overriding whatever was there previously.

  12. 91.102 - Computing II - Problem Session - 1. The permissions: r - Read; w - Write; x - Execute; Example: chmod og-rwx <filename> Simply means you won’t let the world do anything with your file or directory. You (the user) can still do anything you want. If you want to make a directory world accessible and readable but not changeable:

  13. 91.102 - Computing II - Problem Session - 1. If you want to make a directory world accessible and readable but not changeable: chmod o=rx <directory_name> since you may want to descent into it. A file needs only chmod o=r <filename> since you won’t expect any operations on it other than Read. If it is an executable, you may want chmod o=rx <filename> Which will allow copying and executing.

  14. 91.102 - Computing II - Problem Session - 1. If you don’t want others to change your stuff, make sure none of your files or directories have world write permission. Set group permissions the same as world, unless instructed otherwise.

  15. 91.102 - Computing II - Problem Session - 1. To get the program files: Log in to your Unix account, create a directory and copy the files into it: > mkdir ps1 //create a directory for the files > cd ps1 //make this the current working dir. > cp ~rdirkman/ps1/pointers/* ~/ps1/ > cp ~rdirkman/ps1/linklist/* ~/ps1/ > cp ~rdirkman/ps1/airports/* ~/ps1/ > cp ~rdirkman/ps1/josephus/* ~/ps1/

  16. 91.102 - Computing II - Problem Session - 1. If you want to develop on your own PC - FTP from the Windows Desktop: assumes the files have been copied into your directory StartButton -> run -> open: ftp -> click on OK ftp> open cs ... User: <type_your_username> Password: <type_your_password> ... ftp> lcd a:\ Local directory now A:\ // so you copy INTO the floppy ftp> cd <directory_name> // if you need to change dir ... // on cs ftp> ascii 200 Type set to A.

  17. 91.102 - Computing II - Problem Session - 1. ftp> get pointers.c 200 Port command successful 150 Opening ASCII mode 226 Transfer complete Xxxx bytes received in xx seconds ... // more file transfers via get ftp> get linklist.c ... ftp> get airports.c ... ftp> get selector.c ... ftp> bye //ftp exits > exit //back to your Unix account

  18. 91.102 - Computing II - Problem Session - 1. If you develop your programs on a PC, you must send the finished products to cs to test and for submission. Log in to your Unix account > mkdir ps1done //name as good as any > cd ps1done The go to Windows and start FTP ftp> open cs //login as before ftp> lcd a:\ ftp> pwd //see current working directory ... ftp> cd ps1done //move to the correct directory ftp> send linklist.c //more sends - remaining finished files ftp> bye //terminate

  19. 91.102 - Computing II - Problem Session - 1. Making sure that your programs run. > ls //have you got everything? Airports.c linklist.c selector.c > gcc linklist.c //or g++ linklist.c if you used C++ // this may indicate errors - if so, fix them // if successful you will have compiled and linked > a.out //will (try to) execute your program ... > gcc airports.c //compile and link ... > a.out //again, check > gcc selector.c //compile and link > a.out //again, check ... //if it all works out, submit >submit giam ps1 readme linklist.c airports.c selector.c > exit

  20. 91.102 - Computing II - Problem Session - 1. • Developing programs under Unix requires that you learn to use some editor. CDE has a very simple editor and also access to emacs - which is, arguably, the most sophisticated customizable editor in existence. The learning curve is steep, but anyone who expects to make a living as a code jock (at least for a few years) should learn how to use it. • For the programs: • create a directory (>mkdir ps1) • copy the program files into it • edit each file as needed - emacs, vi, CDE,... • compile the file with gcc (or g++) • test by executing a.out • submit all completed files

  21. 91.102 - Computing II - Problem Session - 1. The process is identical to the one outlined for testing a program developed on a PC, except that all file editing and debugging will take place in some window under Unix, rather than off-line on the PC.

More Related