Homework 1 Answers. Due Date: Friday, April 12, 2002 Points: 100

Size: px
Start display at page:

Download "Homework 1 Answers. Due Date: Friday, April 12, 2002 Points: 100"

Transcription

1 Homework 1 Answers Due Date: Friday, April 12, 2002 Points: 100 UNIX System 1. (10 points) Give a one-line UNIX command that capitalizes the vowels (the letters a, e, i, o, and u ) in a file. Answer: The simplest answer uses the command tr(1): tr aeiou AEIOU 2. (10 points) Use a UNIX program to compute the remainder of 7358^349 when divided by Answer: The program dc(1) is an infinite-precision calculator. Entering the following command: gives the answer, 33. C Programming ^ 1345 % p 3. (25 points) Write a program that asks the user to enter two integers, then calculates and displays their greatest common divisor (GCD). The input and output must look like this: Enter two integers: The GCD of 14 and 49 is 7. where 14 and 49 are numbers the user types and 7 is the corresponding GCD. Be sure to check the input for validity, and terminate the program when the user types an end of file character! (Hint: The classic algorithm for computing the GCD, known as Euclid s Algorithm, goes as follows. Let m and n be variables containing the two numbers. Divide m by n. Save the divisor in m, and save the remainder in n. If n is 0, stop; m contains the GCD. Otherwise, repeat the process, starting with the division of m by n.) Answer: The following program is my solution. Yours may differ! gcd -- compute the GCD of pairs of integers History 1.0 Matt Bishop; original program #include <stdio.h> macros #define BAD_GCD-1 error in arguments -- MUST be non-positive This function returns the greatest common divisor of its arguments Notes: (1) if m = n = 0, the GCD is undefined -- so we return BAD_GCD (2) if m < 0 or n < 0, then gcd(m,n) > 0; so we can just make m and n both positive (3) if m = 0 and n!= 0, gcd(m,n) = n (and vice versa) int gcd(int m, int n) Version of May 6, :03 am Page 1 of 6

2 int rem; remainder for Euclid's algorithm special cases error check -- if both 0, undefined if (m == 0 && n == 0) return(bad_gcd); make all negatives positive if (m < 0) m = -m; if (n < 0) n = -n; now apply Euclid's algorithm while(n > 0) rem = m % n; m = n; n = rem; got it return(m); the main routine int m, n; numbers to take the GCD of int g; the GCD of m and n loop, asking for numbers and printing the GCD while(printf("enter two numbers: "), scanf("%d %d", &m, &n)!= EOF) print the result -- note that if the input is invalid, gcd() simply returns BAD_GCD printf("the GCD of %d and %d is ", m, n); if ((g = gcd(m, n)) == BAD_GCD) printf("undefined.\n"); else printf("%d.\n", g); clean up output and exit putchar('\n'); Version of May 6, :03 am Page 2 of 6

3 4. (30 points) Every computer is limited in the amount of precision it can represent for floating-point numbers. At some point, where epsilon is very small, the following expression will be true: 1.0 == epsilon Write a program to find the largest value of epsilon on your computer. Note that the value of epsilon may be different for floats and doubles. Find both values (and the value for long doubles if your compiler supports them). Also, use 1.0 rather than 0.0 to test epsilon because most computers have special hardware instructions for handling zero arithmetic. (Hint: for gcc(1), a long double is the same as a double.) Answer: This program prints epsilon for both floats and doubles. eps -- a program for determining the largest number d such that 1.0 == d and f such that 1.0F == 1.0F + f History 1.0 Matt Bishop; original version float f; the floating point number we're looking for double d; the double precision number we're looking for loop, dividing the number by 2, until it's negligable then print it for(f = 1.0F; 1.0F!= 1.0F + f; f /= 2) null ; d = f; printf requires a double, not a float printf("for float, epsilon is %g\n", d); loop, dividing the number by 2, until it's negligable then print it for(d = 1.0; 1.0!= d; d /= 2) null ; printf("for double, epsilon is %g\n", d); bye-bye! For both floats and doubles on the CSIF Linus systems, epsilon is Debugging 5. (25 points) On the class web page is the source for a C program named vis.c that reads characters from the standard input and prints them on the standard output after expanding any non-printing characters to their C character escape sequence. Version of May 6, :03 am Page 3 of 6

4 The relevant characters, and the C escape sequences to be printed when those characters are encountered, are: character print as character print as newline \n backslash \\ horizontal tab \t vertical tab \v backspace \b carriage return \r form feed \f bell \a NUL \0 anything else \ooo The anything else entry means that any non-printing character other than the ones named in the table is to be printed as a sequence of three octal digits preceded by a backslash. When the escape sequence for a newline is printed, the program is to skip to the next line. Unfortunately, the program as saved in vis.c will not even compile, let alone run. And the programmer thoughtlessly left off all the comments. Hence, your mission: comment the program, and fix it so it works as described above! You are to turn in a corrected source program, with comments describing the changes you made to get it to work. Answer: The following is the commented, debugged program. Visualize Non-printing Characters Prints all non-printing ASCII characters as C escapes, or (if no such escape), as a 3-digit octal number; also, prints \ as \\. This is useful if you have a file with non-printing chars in it, and need to find out what they are. Note the compiler decides what prints as \a \b \f \n \r \t \v \0 and what prints as a 3-digit octal number Version 1.0 Matt Bishop (bishop@cs.ucdavis.edu) #include <stdio.h> #include <ctype.h> this is the main routine arguments: ignored return: exits with 0 function: print the non-printing and escape characters as visible ASCII sequences exceptions: none int ch; input character do the input a character at a time, Version of May 6, :03 am Page 4 of 6

5 expanding chars using the escape sequences of C; note all are nonprinting EXCEPT for the escape, \ while((ch = getchar())!= EOF) BUG #1 here's the original line if (isprint(ch)) now, as \ is printing, it prints on this branch of the "if" as \ and not in the switch in the else part, where it would print as \\ if (ch!= '\\' && isprint(ch)) putchar(ch); else switch (ch) case '\t': printf("\\t"); break; case '\b': printf("\\b"); break; case '\f': printf("\\f"); break; case '\0': printf("\\0"); break; case '\\': printf("\\\\"); break; case '\v': case '\r': case '\a': case '\n': default: printf("\\v"); break; printf("\\r"); break; BUG #2 you need a : after the '\a' case '\a' printf("\\a"); break; printf("\\a"); break; printf("\\n\n"); break; printf("\\%03o", ch); break; exit gracefully Extra Credit 6. (10 points) In question 4, the hint says that a long double and a double use the same number of bits. Write a program to demonstrate that the size of a long double is the same as that of a double. Answer: The following program uses the sizeof operator to report the number of bytes in each type. Print size of data types Print the number of bytes in a float, a double, and a long double Version 1.0 Matt Bishop (bishop@cs.ucdavis.edu) #include <stdio.h> Version of May 6, :03 am Page 5 of 6

6 this is the main routine arguments: ignored return: exits with 0 function: prints the number of bytes in float, double, long double exceptions: none print the three sizes printf("float is %d bytes\n", sizeof(float)); printf("double is %d bytes\n", sizeof(double)); printf("long double is %d bytes\n", sizeof(long double)); bye! On the CSIF Linux systems, floats are 4 bytes, longs are 8 bytes, and long doubles are 12 bytes. So the hint was wrong. Version of May 6, :03 am Page 6 of 6

7 Homework 1 Answers Due Date: Friday, April 12, 2002 Points: 100 UNIX System 1. (10 points) Give a one-line UNIX command that capitalizes the vowels (the letters a, e, i, o, and u ) in a file. Answer: The simplest answer uses the command tr(1): tr aeiou AEIOU 2. (10 points) Use a UNIX program to compute the remainder of 7358^349 when divided by Answer: The program dc(1) is an infinite-precision calculator. Entering the following command: gives the answer, 33. C Programming ^ 1345 % p 3. (25 points) Write a program that asks the user to enter two integers, then calculates and displays their greatest common divisor (GCD). The input and output must look like this: Enter two integers: The GCD of 14 and 49 is 7. where 14 and 49 are numbers the user types and 7 is the corresponding GCD. Be sure to check the input for validity, and terminate the program when the user types an end of file character! (Hint: The classic algorithm for computing the GCD, known as Euclid s Algorithm, goes as follows. Let m and n be variables containing the two numbers. Divide m by n. Save the divisor in m, and save the remainder in n. If n is 0, stop; m contains the GCD. Otherwise, repeat the process, starting with the division of m by n.) Answer: The following program is my solution. Yours may differ! gcd -- compute the GCD of pairs of integers History 1.0 Matt Bishop; original program #include <stdio.h> macros #define BAD_GCD-1 error in arguments -- MUST be non-positive This function returns the greatest common divisor of its arguments Notes: (1) if m = n = 0, the GCD is undefined -- so we return BAD_GCD (2) if m < 0 or n < 0, then gcd(m,n) > 0; so we can just make m and n both positive (3) if m = 0 and n!= 0, gcd(m,n) = n (and vice versa) int gcd(int m, int n) Version of May 6, :03 am Page 1 of 6

8 int rem; remainder for Euclid's algorithm special cases error check -- if both 0, undefined if (m == 0 && n == 0) return(bad_gcd); make all negatives positive if (m < 0) m = -m; if (n < 0) n = -n; now apply Euclid's algorithm while(n > 0) rem = m % n; m = n; n = rem; got it return(m); the main routine int m, n; numbers to take the GCD of int g; the GCD of m and n loop, asking for numbers and printing the GCD while(printf("enter two numbers: "), scanf("%d %d", &m, &n)!= EOF) print the result -- note that if the input is invalid, gcd() simply returns BAD_GCD printf("the GCD of %d and %d is ", m, n); if ((g = gcd(m, n)) == BAD_GCD) printf("undefined.\n"); else printf("%d.\n", g); clean up output and exit putchar('\n'); Version of May 6, :03 am Page 2 of 6

9 4. (30 points) Every computer is limited in the amount of precision it can represent for floating-point numbers. At some point, where epsilon is very small, the following expression will be true: 1.0 == epsilon Write a program to find the largest value of epsilon on your computer. Note that the value of epsilon may be different for floats and doubles. Find both values (and the value for long doubles if your compiler supports them). Also, use 1.0 rather than 0.0 to test epsilon because most computers have special hardware instructions for handling zero arithmetic. (Hint: for gcc(1), a long double is the same as a double.) Answer: This program prints epsilon for both floats and doubles. eps -- a program for determining the largest number d such that 1.0 == d and f such that 1.0F == 1.0F + f History 1.0 Matt Bishop; original version float f; the floating point number we're looking for double d; the double precision number we're looking for loop, dividing the number by 2, until it's negligable then print it for(f = 1.0F; 1.0F!= 1.0F + f; f /= 2) null ; d = f; printf requires a double, not a float printf("for float, epsilon is %g\n", d); loop, dividing the number by 2, until it's negligable then print it for(d = 1.0; 1.0!= d; d /= 2) null ; printf("for double, epsilon is %g\n", d); bye-bye! For both floats and doubles on the CSIF Linus systems, epsilon is Debugging 5. (25 points) On the class web page is the source for a C program named vis.c that reads characters from the standard input and prints them on the standard output after expanding any non-printing characters to their C character escape sequence. Version of May 6, :03 am Page 3 of 6

10 The relevant characters, and the C escape sequences to be printed when those characters are encountered, are: character print as character print as newline \n backslash \\ horizontal tab \t vertical tab \v backspace \b carriage return \r form feed \f bell \a NUL \0 anything else \ooo The anything else entry means that any non-printing character other than the ones named in the table is to be printed as a sequence of three octal digits preceded by a backslash. When the escape sequence for a newline is printed, the program is to skip to the next line. Unfortunately, the program as saved in vis.c will not even compile, let alone run. And the programmer thoughtlessly left off all the comments. Hence, your mission: comment the program, and fix it so it works as described above! You are to turn in a corrected source program, with comments describing the changes you made to get it to work. Answer: The following is the commented, debugged program. Visualize Non-printing Characters Prints all non-printing ASCII characters as C escapes, or (if no such escape), as a 3-digit octal number; also, prints \ as \\. This is useful if you have a file with non-printing chars in it, and need to find out what they are. Note the compiler decides what prints as \a \b \f \n \r \t \v \0 and what prints as a 3-digit octal number Version 1.0 Matt Bishop (bishop@cs.ucdavis.edu) #include <stdio.h> #include <ctype.h> this is the main routine arguments: ignored return: exits with 0 function: print the non-printing and escape characters as visible ASCII sequences exceptions: none int ch; input character do the input a character at a time, Version of May 6, :03 am Page 4 of 6

11 expanding chars using the escape sequences of C; note all are nonprinting EXCEPT for the escape, \ while((ch = getchar())!= EOF) BUG #1 here's the original line if (isprint(ch)) now, as \ is printing, it prints on this branch of the "if" as \ and not in the switch in the else part, where it would print as \\ if (ch!= '\\' && isprint(ch)) putchar(ch); else switch (ch) case '\t': printf("\\t"); break; case '\b': printf("\\b"); break; case '\f': printf("\\f"); break; case '\0': printf("\\0"); break; case '\\': printf("\\\\"); break; case '\v': case '\r': case '\a': case '\n': default: printf("\\v"); break; printf("\\r"); break; BUG #2 you need a : after the '\a' case '\a' printf("\\a"); break; printf("\\a"); break; printf("\\n\n"); break; printf("\\%03o", ch); break; exit gracefully Extra Credit 6. (10 points) In question 4, the hint says that a long double and a double use the same number of bits. Write a program to demonstrate that the size of a long double is the same as that of a double. Answer: The following program uses the sizeof operator to report the number of bytes in each type. Print size of data types Print the number of bytes in a float, a double, and a long double Version 1.0 Matt Bishop (bishop@cs.ucdavis.edu) #include <stdio.h> Version of May 6, :03 am Page 5 of 6

12 this is the main routine arguments: ignored return: exits with 0 function: prints the number of bytes in float, double, long double exceptions: none print the three sizes printf("float is %d bytes\n", sizeof(float)); printf("double is %d bytes\n", sizeof(double)); printf("long double is %d bytes\n", sizeof(long double)); bye! On the CSIF Linux systems, floats are 4 bytes, longs are 8 bytes, and long doubles are 12 bytes. So the hint was wrong. Version of May 6, :03 am Page 6 of 6

So far we have considered only numeric processing, i.e. processing of numeric data represented

So far we have considered only numeric processing, i.e. processing of numeric data represented Chapter 4 Processing Character Data So far we have considered only numeric processing, i.e. processing of numeric data represented as integer and oating point types. Humans also use computers to manipulate

More information

How To Write Portable Programs In C

How To Write Portable Programs In C Writing Portable Programs COS 217 1 Goals of Today s Class Writing portable programs in C Sources of heterogeneity Data types, evaluation order, byte order, char set, Reading period and final exam Important

More information

The programming language C. sws1 1

The programming language C. sws1 1 The programming language C sws1 1 The programming language C invented by Dennis Ritchie in early 1970s who used it to write the first Hello World program C was used to write UNIX Standardised as K&C (Kernighan

More information

Informatica e Sistemi in Tempo Reale

Informatica e Sistemi in Tempo Reale Informatica e Sistemi in Tempo Reale Introduction to C programming Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 25, 2010 G. Lipari (Scuola Superiore Sant Anna)

More information

Pemrograman Dasar. Basic Elements Of Java

Pemrograman Dasar. Basic Elements Of Java Pemrograman Dasar Basic Elements Of Java Compiling and Running a Java Application 2 Portable Java Application 3 Java Platform Platform: hardware or software environment in which a program runs. Oracle

More information

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters The char Type ASCII Encoding The C char type stores small integers. It is usually 8 bits. char variables guaranteed to be able to hold integers 0.. +127. char variables mostly used to store characters

More information

Sources: On the Web: Slides will be available on:

Sources: On the Web: Slides will be available on: C programming Introduction The basics of algorithms Structure of a C code, compilation step Constant, variable type, variable scope Expression and operators: assignment, arithmetic operators, comparison,

More information

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void 1. Explain C tokens Tokens are basic building blocks of a C program. A token is the smallest element of a C program that is meaningful to the compiler. The C compiler recognizes the following kinds of

More information

Secrets of printf. 1 Background. 2 Simple Printing. Professor Don Colton. Brigham Young University Hawaii. 2.1 Naturally Special Characters

Secrets of printf. 1 Background. 2 Simple Printing. Professor Don Colton. Brigham Young University Hawaii. 2.1 Naturally Special Characters Secrets of Professor Don Colton Brigham Young University Hawaii is the C language function to do formatted printing. The same function is also available in PERL. This paper explains how works, and how

More information

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C Embedded Systems A Review of ANSI C and Considerations for Embedded C Programming Dr. Jeff Jackson Lecture 2-1 Review of ANSI C Topics Basic features of C C fundamentals Basic data types Expressions Selection

More information

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 01 / 21 / 2014 Instructor: Michael Eckmann Today s Topics Introduction Homework assignment Review the syllabus Review the policies on academic dishonesty and improper

More information

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu C / C++ and Unix Programming Materials adapted from Dan Hood and Dianna Xu 1 C and Unix Programming Today s goals ú History of C ú Basic types ú printf ú Arithmetic operations, types and casting ú Intro

More information

2 ASCII TABLE (DOS) 3 ASCII TABLE (Window)

2 ASCII TABLE (DOS) 3 ASCII TABLE (Window) 1 ASCII TABLE 2 ASCII TABLE (DOS) 3 ASCII TABLE (Window) 4 Keyboard Codes The Diagram below shows the codes that are returned when a key is pressed. For example, pressing a would return 0x61. If it is

More information

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer About The Tutorial C is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system.

More information

Introduction to Python

Introduction to Python WEEK ONE Introduction to Python Python is such a simple language to learn that we can throw away the manual and start with an example. Traditionally, the first program to write in any programming language

More information

How to Write a Simple Makefile

How to Write a Simple Makefile Chapter 1 CHAPTER 1 How to Write a Simple Makefile The mechanics of programming usually follow a fairly simple routine of editing source files, compiling the source into an executable form, and debugging

More information

Chapter One Introduction to Programming

Chapter One Introduction to Programming Chapter One Introduction to Programming 1-1 Algorithm and Flowchart Algorithm is a step-by-step procedure for calculation. More precisely, algorithm is an effective method expressed as a finite list of

More information

CS 1133, LAB 2: FUNCTIONS AND TESTING http://www.cs.cornell.edu/courses/cs1133/2015fa/labs/lab02.pdf

CS 1133, LAB 2: FUNCTIONS AND TESTING http://www.cs.cornell.edu/courses/cs1133/2015fa/labs/lab02.pdf CS 1133, LAB 2: FUNCTIONS AND TESTING http://www.cs.cornell.edu/courses/cs1133/2015fa/labs/lab02.pdf First Name: Last Name: NetID: The purpose of this lab is to help you to better understand functions:

More information

FEEG6002 - Applied Programming 5 - Tutorial Session

FEEG6002 - Applied Programming 5 - Tutorial Session FEEG6002 - Applied Programming 5 - Tutorial Session Sam Sinayoko 2015-10-30 1 / 38 Outline Objectives Two common bugs General comments on style String formatting Questions? Summary 2 / 38 Objectives Revise

More information

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements 9 Control Statements 9.1 Introduction The normal flow of execution in a high level language is sequential, i.e., each statement is executed in the order of its appearance in the program. However, depending

More information

C++ Essentials. Sharam Hekmat PragSoft Corporation www.pragsoft.com

C++ Essentials. Sharam Hekmat PragSoft Corporation www.pragsoft.com C++ Essentials Sharam Hekmat PragSoft Corporation www.pragsoft.com Contents Contents Preface 1. Preliminaries 1 A Simple C++ Program 2 Compiling a Simple C++ Program 3 How C++ Compilation Works 4 Variables

More information

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share. LING115 Lecture Note Session #4 Python (1) 1. Introduction As we have seen in previous sessions, we can use Linux shell commands to do simple text processing. We now know, for example, how to count words.

More information

Introduction to Visual C++.NET Programming. Using.NET Environment

Introduction to Visual C++.NET Programming. Using.NET Environment ECE 114-2 Introduction to Visual C++.NET Programming Dr. Z. Aliyazicioglu Cal Poly Pomona Electrical & Computer Engineering Cal Poly Pomona Electrical & Computer Engineering 1 Using.NET Environment Start

More information

As previously noted, a byte can contain a numeric value in the range 0-255. Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

As previously noted, a byte can contain a numeric value in the range 0-255. Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets! Encoding of alphanumeric and special characters As previously noted, a byte can contain a numeric value in the range 0-255. Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets! Alphanumeric

More information

Variables, Constants, and Data Types

Variables, Constants, and Data Types Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class: L&L, 2.1-2.3, App C 1 Primitive Data There are eight

More information

Module 816. File Management in C. M. Campbell 1993 Deakin University

Module 816. File Management in C. M. Campbell 1993 Deakin University M. Campbell 1993 Deakin University Aim Learning objectives Content After working through this module you should be able to create C programs that create an use both text and binary files. After working

More information

1 Description of The Simpletron

1 Description of The Simpletron Simulating The Simpletron Computer 50 points 1 Description of The Simpletron In this assignment you will write a program to simulate a fictional computer that we will call the Simpletron. As its name implies

More information

Chapter 2. Binary Values and Number Systems

Chapter 2. Binary Values and Number Systems Chapter 2 Binary Values and Number Systems Numbers Natural numbers, a.k.a. positive integers Zero and any number obtained by repeatedly adding one to it. Examples: 100, 0, 45645, 32 Negative numbers A

More information

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program. Name: Class: Date: Exam #1 - Prep True/False Indicate whether the statement is true or false. 1. Programming is the process of writing a computer program in a language that the computer can respond to

More information

Introduction to Java Applications. 2005 Pearson Education, Inc. All rights reserved.

Introduction to Java Applications. 2005 Pearson Education, Inc. All rights reserved. 1 2 Introduction to Java Applications 2.2 First Program in Java: Printing a Line of Text 2 Application Executes when you use the java command to launch the Java Virtual Machine (JVM) Sample program Displays

More information

CP Lab 2: Writing programs for simple arithmetic problems

CP Lab 2: Writing programs for simple arithmetic problems Computer Programming (CP) Lab 2, 2015/16 1 CP Lab 2: Writing programs for simple arithmetic problems Instructions The purpose of this Lab is to guide you through a series of simple programming problems,

More information

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T) Unit- I Introduction to c Language: C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating

More information

Member Functions of the istream Class

Member Functions of the istream Class Member Functions of the istream Class The extraction operator is of limited use because it always uses whitespace to delimit its reads of the input stream. It cannot be used to read those whitespace characters,

More information

This is great when speed is important and relatively few words are necessary, but Max would be a terrible language for writing a text editor.

This is great when speed is important and relatively few words are necessary, but Max would be a terrible language for writing a text editor. Dealing With ASCII ASCII, of course, is the numeric representation of letters used in most computers. In ASCII, there is a number for each character in a message. Max does not use ACSII very much. In the

More information

Systems I: Computer Organization and Architecture

Systems I: Computer Organization and Architecture Systems I: Computer Organization and Architecture Lecture 2: Number Systems and Arithmetic Number Systems - Base The number system that we use is base : 734 = + 7 + 3 + 4 = x + 7x + 3x + 4x = x 3 + 7x

More information

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. Exam Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) The JDK command to compile a class in the file Test.java is A) java Test.java B) java

More information

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand: Introduction to Programming and Algorithms Module 2 CS 146 Sam Houston State University Dr. Tim McGuire Introduction To Computers And Java Chapter Objectives To understand: the meaning and placement of

More information

Computer Science 217

Computer Science 217 Computer Science 217 Midterm Exam Fall 2009 October 29, 2009 Name: ID: Instructions: Neatly print your name and ID number in the spaces provided above. Pick the best answer for each multiple choice question.

More information

Objective-C Tutorial

Objective-C Tutorial Objective-C Tutorial OBJECTIVE-C TUTORIAL Simply Easy Learning by tutorialspoint.com tutorialspoint.com i ABOUT THE TUTORIAL Objective-c tutorial Objective-C is a general-purpose, object-oriented programming

More information

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor Vim, Emacs, and JUnit Testing Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor Overview Vim and Emacs are the two code editors available within the Dijkstra environment. While both

More information

CS106A, Stanford Handout #38. Strings and Chars

CS106A, Stanford Handout #38. Strings and Chars CS106A, Stanford Handout #38 Fall, 2004-05 Nick Parlante Strings and Chars The char type (pronounced "car") represents a single character. A char literal value can be written in the code using single quotes

More information

Java Interview Questions and Answers

Java Interview Questions and Answers 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write and compile the java

More information

An Introduction to the C Programming Language and Software Design. Tim Bailey

An Introduction to the C Programming Language and Software Design. Tim Bailey An Introduction to the C Programming Language and Software Design Tim Bailey Preface This textbook began as a set of lecture notes for a first-year undergraduate software engineering course in 2003. The

More information

Stacks. Linear data structures

Stacks. Linear data structures Stacks Linear data structures Collection of components that can be arranged as a straight line Data structure grows or shrinks as we add or remove objects ADTs provide an abstract layer for various operations

More information

8 Primes and Modular Arithmetic

8 Primes and Modular Arithmetic 8 Primes and Modular Arithmetic 8.1 Primes and Factors Over two millennia ago already, people all over the world were considering the properties of numbers. One of the simplest concepts is prime numbers.

More information

Unix Scripts and Job Scheduling

Unix Scripts and Job Scheduling Unix Scripts and Job Scheduling Michael B. Spring Department of Information Science and Telecommunications University of Pittsburgh spring@imap.pitt.edu http://www.sis.pitt.edu/~spring Overview Shell Scripts

More information

C++ Language Tutorial

C++ Language Tutorial cplusplus.com C++ Language Tutorial Written by: Juan Soulié Last revision: June, 2007 Available online at: http://www.cplusplus.com/doc/tutorial/ The online version is constantly revised and may contain

More information

Basics of I/O Streams and File I/O

Basics of I/O Streams and File I/O Basics of This is like a cheat sheet for file I/O in C++. It summarizes the steps you must take to do basic I/O to and from files, with only a tiny bit of explanation. It is not a replacement for reading

More information

Arithmetic in MIPS. Objectives. Instruction. Integer arithmetic. After completing this lab you will:

Arithmetic in MIPS. Objectives. Instruction. Integer arithmetic. After completing this lab you will: 6 Objectives After completing this lab you will: know how to do integer arithmetic in MIPS know how to do floating point arithmetic in MIPS know about conversion from integer to floating point and from

More information

1. Define: (a) Variable, (b) Constant, (c) Type, (d) Enumerated Type, (e) Identifier.

1. Define: (a) Variable, (b) Constant, (c) Type, (d) Enumerated Type, (e) Identifier. Study Group 1 Variables and Types 1. Define: (a) Variable, (b) Constant, (c) Type, (d) Enumerated Type, (e) Identifier. 2. What does the byte 00100110 represent? 3. What is the purpose of the declarations

More information

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system. http://www.tutorialspoint.com/java/java_quick_guide.htm JAVA - QUICK GUIDE Copyright tutorialspoint.com What is Java? Java is: Object Oriented Platform independent: Simple Secure Architectural- neutral

More information

The C Programming Language course syllabus associate level

The C Programming Language course syllabus associate level TECHNOLOGIES The C Programming Language course syllabus associate level Course description The course fully covers the basics of programming in the C programming language and demonstrates fundamental programming

More information

An Incomplete C++ Primer. University of Wyoming MA 5310

An Incomplete C++ Primer. University of Wyoming MA 5310 An Incomplete C++ Primer University of Wyoming MA 5310 Professor Craig C. Douglas http://www.mgnet.org/~douglas/classes/na-sc/notes/c++primer.pdf C++ is a legacy programming language, as is other languages

More information

First Java Programs. V. Paúl Pauca. CSC 111D Fall, 2015. Department of Computer Science Wake Forest University. Introduction to Computer Science

First Java Programs. V. Paúl Pauca. CSC 111D Fall, 2015. Department of Computer Science Wake Forest University. Introduction to Computer Science First Java Programs V. Paúl Pauca Department of Computer Science Wake Forest University CSC 111D Fall, 2015 Hello World revisited / 8/23/15 The f i r s t o b l i g a t o r y Java program @author Paul Pauca

More information

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall-2015 1

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall-2015 1 QUIZ-II Write a program that mimics a calculator. The program should take as input two integers and the operation to be performed. It should then output the numbers, the operator, and the result. (For

More information

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming 1 2 Foreword First of all, this book isn t really for dummies. I wrote it for myself and other kids who are on the team. Everything

More information

Numbering Systems. InThisAppendix...

Numbering Systems. InThisAppendix... G InThisAppendix... Introduction Binary Numbering System Hexadecimal Numbering System Octal Numbering System Binary Coded Decimal (BCD) Numbering System Real (Floating Point) Numbering System BCD/Binary/Decimal/Hex/Octal

More information

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)! The Tower of Hanoi Recursion Solution recursion recursion recursion Recursive Thinking: ignore everything but the bottom disk. 1 2 Recursive Function Time Complexity Hanoi (n, src, dest, temp): If (n >

More information

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint) TN203 Porting a Program to Dynamic C Introduction Dynamic C has a number of improvements and differences compared to many other C compiler systems. This application note gives instructions and suggestions

More information

Number Representation

Number Representation Number Representation CS10001: Programming & Data Structures Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Topics to be Discussed How are numeric data

More information

C Language Tutorial. Version 0.042 March, 1999

C Language Tutorial. Version 0.042 March, 1999 C Language Tutorial Version 0.042 March, 1999 Original MS-DOS tutorial by Gordon Dodrill, Coronado Enterprises. Moved to Applix by Tim Ward Typed by Karen Ward C programs converted by Tim Ward and Mark

More information

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE PROGRAMMING IN C CONTENT AT A GLANCE 1 MODULE 1 Unit 1 : Basics of Programming Unit 2 : Fundamentals Unit 3 : C Operators MODULE 2 unit 1 : Input Output Statements unit 2 : Control Structures unit 3 :

More information

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC Example Assume the following specification: Input: read a number N > 0 Output:

More information

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification: Example Introduction to Programming (in C++) Loops Assume the following specification: Input: read a number N > 0 Output: write the sequence 1 2 3 N (one number per line) Jordi Cortadella, Ricard Gavaldà,

More information

Programming Project 1: Lexical Analyzer (Scanner)

Programming Project 1: Lexical Analyzer (Scanner) CS 331 Compilers Fall 2015 Programming Project 1: Lexical Analyzer (Scanner) Prof. Szajda Due Tuesday, September 15, 11:59:59 pm 1 Overview of the Programming Project Programming projects I IV will direct

More information

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine 7 Objectives After completing this lab you will: know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine Introduction Branches and jumps provide ways to change

More information

ASSEMBLY PROGRAMMING ON A VIRTUAL COMPUTER

ASSEMBLY PROGRAMMING ON A VIRTUAL COMPUTER ASSEMBLY PROGRAMMING ON A VIRTUAL COMPUTER Pierre A. von Kaenel Mathematics and Computer Science Department Skidmore College Saratoga Springs, NY 12866 (518) 580-5292 pvonk@skidmore.edu ABSTRACT This paper

More information

LSN 2 Number Systems. ECT 224 Digital Computer Fundamentals. Department of Engineering Technology

LSN 2 Number Systems. ECT 224 Digital Computer Fundamentals. Department of Engineering Technology LSN 2 Number Systems Department of Engineering Technology LSN 2 Decimal Number System Decimal number system has 10 digits (0-9) Base 10 weighting system... 10 5 10 4 10 3 10 2 10 1 10 0. 10-1 10-2 10-3

More information

Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage

Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage Outline 1 Computer Architecture hardware components programming environments 2 Getting Started with Python installing Python executing Python code 3 Number Systems decimal and binary notations running

More information

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON PROBLEM SOLVING WITH SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON Addison Wesley Boston San Francisco New York London

More information

Kevin James. MTHSC 412 Section 2.4 Prime Factors and Greatest Comm

Kevin James. MTHSC 412 Section 2.4 Prime Factors and Greatest Comm MTHSC 412 Section 2.4 Prime Factors and Greatest Common Divisor Greatest Common Divisor Definition Suppose that a, b Z. Then we say that d Z is a greatest common divisor (gcd) of a and b if the following

More information

The C Programming Language

The C Programming Language Chapter 1 The C Programming Language In this chapter we will learn how to write simple computer programs using the C programming language; perform basic mathematical calculations; manage data stored in

More information

System Security Fundamentals

System Security Fundamentals System Security Fundamentals Alessandro Barenghi Dipartimento di Elettronica, Informazione e Bioingegneria Politecnico di Milano alessandro.barenghi - at - polimi.it April 28, 2015 Lesson contents Overview

More information

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner. Handout 1 CS603 Object-Oriented Programming Fall 15 Page 1 of 11 Handout 1 Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner. Java

More information

Calculator. Introduction. Requirements. Design. The calculator control system. F. Wagner August 2009

Calculator. Introduction. Requirements. Design. The calculator control system. F. Wagner August 2009 F. Wagner August 2009 Calculator Introduction This case study is an introduction to making a specification with StateWORKS Studio that will be executed by an RTDB based application. The calculator known

More information

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be... What is a Loop? CSC Intermediate Programming Looping A loop is a repetition control structure It causes a single statement or a group of statements to be executed repeatedly It uses a condition to control

More information

Computer Programming Tutorial

Computer Programming Tutorial Computer Programming Tutorial COMPUTER PROGRAMMING TUTORIAL by tutorialspoint.com tutorialspoint.com i ABOUT THE TUTORIAL Computer Prgramming Tutorial Computer programming is the act of writing computer

More information

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct Dr. Martin O. Steinhauser University of Basel Graduate Lecture Spring Semester 2014 Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct Friday, 7 th March

More information

Programming Languages CIS 443

Programming Languages CIS 443 Course Objectives Programming Languages CIS 443 0.1 Lexical analysis Syntax Semantics Functional programming Variable lifetime and scoping Parameter passing Object-oriented programming Continuations Exception

More information

Tutorial on C Language Programming

Tutorial on C Language Programming Tutorial on C Language Programming Teodor Rus rus@cs.uiowa.edu The University of Iowa, Department of Computer Science Introduction to System Software p.1/64 Tutorial on C programming C program structure:

More information

Lexical Analysis and Scanning. Honors Compilers Feb 5 th 2001 Robert Dewar

Lexical Analysis and Scanning. Honors Compilers Feb 5 th 2001 Robert Dewar Lexical Analysis and Scanning Honors Compilers Feb 5 th 2001 Robert Dewar The Input Read string input Might be sequence of characters (Unix) Might be sequence of lines (VMS) Character set ASCII ISO Latin-1

More information

Programming Microcontrollers in C

Programming Microcontrollers in C Programming Microcontrollers in C Second Edition Ted Van Sickle A Volume in the EMBEDDED TECHNOLOGY TM Series Eagle Rock, Virginia www.llh-publishing.com Programming Microcontrollers in C 2001 by LLH Technology

More information

IBM Emulation Mode Printer Commands

IBM Emulation Mode Printer Commands IBM Emulation Mode Printer Commands Section 3 This section provides a detailed description of IBM emulation mode commands you can use with your printer. Control Codes Control codes are one-character printer

More information

The Answer to the 14 Most Frequently Asked Modbus Questions

The Answer to the 14 Most Frequently Asked Modbus Questions Modbus Frequently Asked Questions WP-34-REV0-0609-1/7 The Answer to the 14 Most Frequently Asked Modbus Questions Exactly what is Modbus? Modbus is an open serial communications protocol widely used in

More information

The Euclidean Algorithm

The Euclidean Algorithm The Euclidean Algorithm A METHOD FOR FINDING THE GREATEST COMMON DIVISOR FOR TWO LARGE NUMBERS To be successful using this method you have got to know how to divide. If this is something that you have

More information

How to represent characters?

How to represent characters? Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. How to represent characters?

More information

JavaScript: Introduction to Scripting. 2008 Pearson Education, Inc. All rights reserved.

JavaScript: Introduction to Scripting. 2008 Pearson Education, Inc. All rights reserved. 1 6 JavaScript: Introduction to Scripting 2 Comment is free, but facts are sacred. C. P. Scott The creditor hath a better memory than the debtor. James Howell When faced with a decision, I always ask,

More information

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions Phys4051: C Lecture 2 & 3 Functions (Review) Comment Statements Variables & Operators Branching Instructions Comment Statements! Method 1: /* */! Method 2: // /* Single Line */ //Single Line /* This comment

More information

Outline Basic concepts of Python language

Outline Basic concepts of Python language Data structures: lists, tuples, sets, dictionaries Basic data types Examples: int: 12, 0, -2 float: 1.02, -2.4e2, 1.5e-3 complex: 3+4j bool: True, False string: "Test string" Conversion between types int(-2.8)

More information

Levent EREN levent.eren@ieu.edu.tr A-306 Office Phone:488-9882 INTRODUCTION TO DIGITAL LOGIC

Levent EREN levent.eren@ieu.edu.tr A-306 Office Phone:488-9882 INTRODUCTION TO DIGITAL LOGIC Levent EREN levent.eren@ieu.edu.tr A-306 Office Phone:488-9882 1 Number Systems Representation Positive radix, positional number systems A number with radix r is represented by a string of digits: A n

More information

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner java.util.scanner java.util.scanner is a class in the Java API used to create a Scanner object, an extremely versatile object that you can use to input alphanumeric characters from several input sources

More information

Project 2: Bejeweled

Project 2: Bejeweled Project 2: Bejeweled Project Objective: Post: Tuesday March 26, 2013. Due: 11:59PM, Monday April 15, 2013 1. master the process of completing a programming project in UNIX. 2. get familiar with command

More information

Lex et Yacc, exemples introductifs

Lex et Yacc, exemples introductifs Lex et Yacc, exemples introductifs D. Michelucci 1 LEX 1.1 Fichier makefile exemple1 : exemple1. l e x f l e x oexemple1. c exemple1. l e x gcc o exemple1 exemple1. c l f l l c exemple1 < exemple1. input

More information

SYMETRIX SOLUTIONS: TECH TIP August 2015

SYMETRIX SOLUTIONS: TECH TIP August 2015 String Output Modules The purpose of this document is to provide an understanding of operation and configuration of the two different String Output modules available within SymNet Composer. The two different

More information

Factoring & Primality

Factoring & Primality Factoring & Primality Lecturer: Dimitris Papadopoulos In this lecture we will discuss the problem of integer factorization and primality testing, two problems that have been the focus of a great amount

More information

SUGI 29 Coders' Corner

SUGI 29 Coders' Corner Paper 074-29 Tales from the Help Desk: Solutions for Simple SAS Mistakes Bruce Gilsen, Federal Reserve Board INTRODUCTION In 19 years as a SAS consultant at the Federal Reserve Board, I have seen SAS users

More information

An Introduction to the WEB Style of Literate Programming

An Introduction to the WEB Style of Literate Programming An Introduction to the WEB Style of Literate Programming Literate Programming by Bart Childs One of the greatest needs in computing is the reduction of the cost of maintenance of codes. Maintenance programmers

More information

C++ Input/Output: Streams

C++ Input/Output: Streams C++ Input/Output: Streams 1 The basic data type for I/O in C++ is the stream. C++ incorporates a complex hierarchy of stream types. The most basic stream types are the standard input/output streams: istream

More information

CSI 333 Lecture 1 Number Systems

CSI 333 Lecture 1 Number Systems CSI 333 Lecture 1 Number Systems 1 1 / 23 Basics of Number Systems Ref: Appendix C of Deitel & Deitel. Weighted Positional Notation: 192 = 2 10 0 + 9 10 1 + 1 10 2 General: Digit sequence : d n 1 d n 2...

More information