Palindromes revisited Here's a simpler program for checking palindromes: int nums[100]; int i = 0, a; cin >> a; while(a > 0) { nums[i++] = a; cin >> a;

Slides:



Advertisements
Similar presentations
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Advertisements

ITC 240: Web Application Programming
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 CIS 205 Practice Test George Lamperti A word that has a predefined meaning in a C++ program and cannot be used as a variable name is known as.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
CS150 Introduction to Computer Science 1
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Multiple-Subscripted Array
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Chapter 9: Arrays and Strings
Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the.
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
CS161 Topic #14 1 Today in CS161 Lecture #14 Practicing! Writing Programs to Practice Write a program that counts the number of vowels in a sentence, ended.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Chapter 8 Arrays and Strings
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
Fundamental Programming Fundamental Programming for Loops.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays.
POINTERS.
 for loop  while loop  do-while loop for (begin point; end point ; incrementation ) { //statements to be repeated }
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Gator Engineering Google Code Jam 2015 Copyright © 2008 W. W. Norton & Company. All rights reserved. 1.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
CS161 Introduction to Computer Science
New Structure Recall “average.cpp” program
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Your questions from last session
Fundamental Programming
Fundamental Programming
Presentation transcript:

Palindromes revisited Here's a simpler program for checking palindromes: int nums[100]; int i = 0, a; cin >> a; while(a > 0) { nums[i++] = a; cin >> a; } for(int j=0; j<i; j++) if(nums[j] != nums[i-1-j]) { cout << "no" << endl; return 0; } cout << "yes" << endl;

Text Palindromes Instead of using numbers, let's use letters, or chars. Also, we will read in until the end of the user input, rather than reading in until a sentinel value. The expression: cin >> a; (where a is a char) will return a value equivalent to true if a character is read in and a value equivalent to false if nothing is read in. We can use that in a test.

Palindrome Code char lets[100], a; int i = 0; while(cin >> a) lets[i++] = a; for(int j=0; j<i; j++) if(lets[j] != lets[i-1-j]) { cout << "no" << endl; return 0; } cout << "yes" << endl; To indicate the end of input, the user would type ^Z (in Windows) or ^D (in Unix).

Another Example Here's the problem: Let's read in characters, just like in the last program, but then print out a histogram showing the number of a's, b's, c's,,... read in. For example, if the program read in the text: This is a test. This is only a test. In the event of an actual emergency, you would be told where to tune on your radio dial.

Example (cont'd) a: ******* b: * c: ** d: **** e: ************ f: * g: * h: **** i: ****** j:

Example Code We will need a counter for each letter, a – z. The counters should be initialized to 0. Every time a char is read in, if it is a letter (a-z, or A-Z), the appropriate counter should be incremented. This requires a read-to-end-of-input loop. Once everything is read in, print out the histogram.

Converting chars to ints We need to convert the chars read in to be ints, since arrays are indexed by ints. This is easy to do in C++. Any arithmetic operation applied to a char automatically converts it to an int. The value of the char is given by the ASCII (American Standard Code for Information Interchange) code. For example, 'A' is 65, and 'a' is 97, but we don't need to know that.

Converting chars to ints (cont'd) What we need to know is how to convert 'a' to 0, and 'b' to 1, and 'c' to 3,... (and likewise 'A' to 0 and 'B' to 1,...). The expression c – 'a' will automatically convert the value of c to its ASCII value, and also 'a' to its ASCII and subtract the two values. This does exactly what we want for lower case letters. c – 'A' works for upper case letters.

Example Code int count[26]; char c; for(int i=0; i<26; i++) count[i] = 0; while(cin >> c) if('a' <= c && c <= 'z') count[c - 'a']++; else if('A' <= c && c <= 'Z') count[c - 'Z']++; for(int i=0; i<26; i++) { cout << (char)(i+'a') << ": "; printStars(count[i]); }

More About Arrays Arrays must be declared to be a fixed size (in most compilers – in some, arrays which are local variables, can be declared with a variable) Usually named constants are used to denote the size: const int NUMCHARS = 26; int a[NUMCHARS]; Array always start at 0, not 1. Arrays may be initialized: int primes[4] = {2, 3, 5, 7};

Using Indexed Variables as Args. Index variables (array elements) may be used as arguments in function calls. They may correspond to either call-by-value or call- by-reference parameters. If the indexed variable has a variable subscript, say a[i], the subscript is evaluated when the function is called, and that data object is used.

Using Whole Arrays as Arguments A whole array may be used an argument in a function call. Whole arrays are always call-by-reference parameters, even without writing the “&” in the parameter list. A parameter which is an array variable doesn't cause a data object to be created – it becomes another name of the array argument. No need to give the size of the array parameter.

Example int foo(int x[], int size); int main(void) { int a[10], s = 10; // read in array a; foo(a,s); }

Exercise Write a function, maxElement, that is passed an array of ints, a, and the number of elements in that array, size, and returns the maximum element of the array.