Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Similar documents
Intermediate Programming, Spring 2017*

Introduction to string

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

Chapter 9 Strings. With this array declaration: char s[10];

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

20 Dynamic allocation of memory: malloc and calloc

First of all, it is a variable, just like other variables you studied

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Chapter 8 C Characters and Strings

2/28/2018. Overview. The C Programming Language Part 4. Pointers. Pointers. Pointers. Pointers

The C Programming Language Part 4. (with material from Dr. Bin Ren, William & Mary Computer Science, and

NCSU ECE 209 Sections 602 Exam 1 Fall September, I have neither given not received unauthorized assistance on this test.

Personal SE. Functions, Arrays, Strings & Command Line Arguments

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Lesson #8. Structures Linked Lists Command Line Arguments

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

C Language: Review. INFO High Performance Scientific Computing. 26 septembre 2017

High Performance Programming Programming in C part 1

CS 61c: Great Ideas in Computer Architecture

Week 6 Part 1. Kyle Dewey. Monday, July 30, 12

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #28. Functions: Examples 2

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

Chapter 8: Character & String. In this chapter, you ll learn about;

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

BSM540 Basics of C Language

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

ONE DIMENSIONAL ARRAYS

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

CS349/SE382 A1 C Programming Tutorial

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

CS 241 Data Organization Pointers and Arrays

Library Functions. General Questions

8. Characters, Strings and Files

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

BİL200 TUTORIAL-EXERCISES Objective:

Computer Science & Engineering 150A Problem Solving Using Computers

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Tutorial 7. Number Colour Black Brown Red Orange Yellow Green Blue Violet Gray White

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Strings. Chuan-Ming Liu. Computer Science & Information Engineering National Taipei University of Technology Taiwan

CSCI 6610: Review. Chapter 7: Numbers Chapter 8: Characters Chapter 11 Pointers

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

CCE1111 Programming for Engineers [C Programming Language]

today cs3157-fall2002-sklar-lect05 1

COMP1917: 09 Arrays and Strings

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

ESC101N: Fundamentals of Computing End-sem st semester

ECE 15B COMPUTER ORGANIZATION

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

EECE.2160: ECE Application Programming Fall 2017 Exam 3 December 16, 2017

CS 222: Pointers and Manual Memory Management

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Data Types. Data Types. Integer Types. Signed Integers

Arrays and Strings. Arash Rafiey. September 12, 2017

COP 3223 Final Review

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

C PROGRAMMING. Characters and Strings File Processing Exercise

ECE2049 E17 Lecture 2: Data Representations & C Programming Basics

EL2310 Scientific Programming

Contents. A Review of C language. Visual C Visual C++ 6.0

C programming basics T3-1 -

Warmup January 9th, What is the value of the following C expression? 8*9 % 10/ 2

Single Dimension Arrays

Lesson 7. Reading and Writing a.k.a. Input and Output

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Strings. Daily Puzzle

C library = Header files + Reserved words + main method

CS16 Exam #1 7/17/ Minutes 100 Points total

Flow Control. CSC215 Lecture

Course organization. Course introduction ( Week 1)

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Lab 1 Introduction to UNIX and C

SWEN-250 Personal SE. Introduction to C

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Computer Programming: Skills & Concepts (CP) Strings

Computers Programming Course 11. Iulian Năstac

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CS 33. Introduction to C. Part 5. CS33 Intro to Computer Systems V 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Figure 1 Ring Structures

Lecture 10 Arrays (2) and Strings. UniMAP SEM II - 11/12 DKT121 1

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

Q1: Multiple choice / 20 Q2: C input/output; operators / 40 Q3: Conditional statements / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Transcription:

Announcements Thursday Extras: CS Commons on Thursdays @ 4:00 pm but none next week No office hours next week Monday or Tuesday Reflections: when to use if/switch statements for/while statements Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Claim: Any for statement can be translated into code using a while loop. A. True always B. True usually (but not always) C. True in some cases D. Never True

Consider the basic syntax with for statements for (initialization; condition; update(s)) { loop body } This translates directly to initialization while (condition) { loop body update(s) }

Claim: Any while statement can be translated into code using a for loop. A. True always B. True usually (but not always) C. True in some cases D. Never True

Consider the basic syntax with while statements while (condition) { loop body } This translates directly to for ( ; condition; ) { loop body }

what does a while statement highlight? what does a for statement highlight?

On our lab machines, the code segment char ch1 = 'a'; char ch2 = 'z'; printf (" char = %c, code = %d\n", ch1, ch1); printf (" char = %c, code = %d\n", ch2, ch2); prints the following output char = a, code = 97 char = z, code = 122 Which of the following segments prints the alphabet (in lower case) on all computers (internationally) running C? A. for (char ch = 'a'; ch <= 'z'; ch++) printf ("%c", ch); B. for (int i = 'a'; i <= 'z'; i++) printf ("%c", i); C. for (int i = 97; i <= 122; i++) printf ("%c", i); A. A, B, and C B. A and B C. A and C D. B and C E. Something else

Suppose a is declared as an array of 10 characters: char a [10] and suppose the beginning address for this block of characters is 100. Which of the follow is true? A. a stores the address 100; that is, a == 100 B. the address of a is 100; that is, &a == 100 C. the address of a[0] is 100; that is &a[0] == 100 D. exactly two of the above E. all of the above

Suppose a program contains the following declarations: char a [10]; char * b = a; Suppose further that the beginning address for the a array is 100, and supposed b has address 200. Which of the follow is true? A. b == 100 B. &b == 200 C. &b[0] == 100 D. exactly two of the above E. all of the above

Consider the following declarations and initializations /* declaration and initialization */ int arr1 [5] = {3, 1, 4}; // line 1 int arr2 [ ] = {2, 7, 1, 8, 2};// line 2 int arr3 [ ] = arr2; // line 3 int arr4 [5]; // line 4 arr4 = arr2; // line 5 int * arr5; // line 6 arr5 = arr1; // line 7 int arr6 [5] = arr5; // line 8 In 4 votes, which lines compile (e.g., no errors)? Vote 1 1. 1 and 2 2. 1 not 2 3. 2 not 1 4. neither 1 nor 2 Vote 2 1. 3 and 4 2. 3 not 4 3. 4 not 3 4. neither 3 nor 4 Vote 3 1. 5 and 6 2. 5 not 6 3. 6 not 5 4. neither 5 nor 6 Vote 4 1. 7 and 8 2. 7 not 8 3. 8 not 7 4. neither 7 nor 8

A character usually requires 1 byte of storage. sizeof (from stdlib.h) indicates the storage required. strlen (from string.h) indicates the string's logical length. What is printed by the following code? char str [ ] = "computer"; printf ("size %d, strlen %d, str[2] %c\n", (int) sizeof (str), (int) strlen (str), str[2]); A. size 8, strlen 8, str[2] o B. size 8, strlen 8, str[2] m C. size 9, strlen 9, str[2] o or size 9, strlen 9, str[2] m D. size 9, strlen 8, str[2] o E. size 9, strlen 8, str[2] m

Consider the following code segment, and suppose the str2 array is stored immediately before the str1 array in main memory: char str1 [16] = " is exciting!!!"; char str2 [16] = {'c', 'o', 'm', 'p', 'u', 't', 'e', 'r', ' ', 's', 'c', 'i', 'e', 'n', 'c', 'e'}; printf ("A-str2: %s, str1: %s\n", str2, str1); str1[0] = 0; printf ("B-str2: %s, str1: %s\n", str2, str1); What is printed? A. A-str2: computer science, str1: is exciting!!! B-str2: computer science, str1: is exciting!!! B. A-str2: computer science, str1: is exciting!!! B-str2: computer science, str1: C. A-str2: computer science is exciting!!!, str1: is exciting!!! B-str2: computer science, str1:?????? D. A-str2: computer science is exciting!!!, str1: is exciting!!! B-str2: computer science, str1: E. none of the above

Suppose input will contain a name in the first 40 characters of a line, followed by a number. char line [101]; //leaving room for null character char name [41]; int number; Two approaches are proposed reading a line 1. scanf ("%41s %d", name, &number); 2. fgets (line, 101, stdin); strncpy (name, line, 40); name[40] = 0; number = atoi (line+40); Which of these approaches works reliably? A. Approaches 1 and 2 B. Approach 1, not 2 C. Approach 2, not 1 D. Neither 1 nor 2

Suppose you want to read an integer at the start of one line, skip anything else on that line, and then read a double at the start of the next line. int one; double two; #define max 100 char line [max+1]; Two approaches are proposed: 1. scanf ("%d", &one); while (getchar ()!= '\n') ; scanf ("%lf", &two); 2. fgets (line, max+1, stdin); one = atoi (line); while (line[strlen(line)-1]!= '\n') fgets (line, max+1, stdin); fgets (line, max+1, stdin); two = atof (line); Which of these approaches works reliably? A. Approaches 1 and 2 B. Approach 1, not 2 C. Approach 2, not 1 D. Neither 1 nor 2