Array Application Programming. COMP102 Prog. Fundamentals I: Arrays Application / Slide 2 Character Array: Ex. 1 l Note: a palindrome is a word (or words)

Slides:



Advertisements
Similar presentations
Passing Streams to Functions. Passing Streams to Functions One Rule: always pass a stream as a reference.
Advertisements

C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Computer Science 101 Data Encryption And Computer Networks.
Programming Functions: Passing Parameters by Reference.
CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
Programming File I/O. COMP102 Prog. Fundamentals File I/O / Slide 2 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson.
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
Chapter 10.
Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files (Review) * A computer file n is stored secondary storage devices (hard drive, CD) n can.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
File I/O. COMP104 File I/O / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
Programming Scope of Identifiers. COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 2 Scope l A sequence of statements within { … } is considered.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
Monday, 9/23/02, Slide #1 CS 106 Intro to CS 1 Monday, 9/23/02  QUESTIONS??  Today:  Discuss Lab 3  Do Exercises  Introduction to functions  Reading:
Character I/O. COMP104 Character I/O Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable.
Characters. COMP104 Lecture 21 / Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable assignment.
Functions:Passing Parameters by Value Programming.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
C++ plus. 2 Goals Some general C++ tips 3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
Programming File I/O. COMP102 Prog. Fundamentals File I/O / Slide 2 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson.
Programming is instructing a computer to perform a task for you with the help of a programming language.
Cryptography Programming Lab
CSC 107 – Programming For Science. Today’s Goal ALL  Understand why ALL I/O is file I/O  Common bugs to avoid when coding with files in C++  Get a.
Programming Initializing Data of Struct Type. COMP102 Prog. Fundamentals: initialize struct type/ Slide 2 Ex. 10: Initialize Data of struct Type l By.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 4 Mathematical Functions, Characters, and Strings.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
1 CS161 Introduction to Computer Science Topic #13.
Topics 1.File Basics 2.Output Formatting 3.Passing File Stream Objects to Functions 4.More Detailed Error Testing 5.Member Functions for Reading and 6.Writing.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
Chapter 15 Strings as Character Arrays
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Sahar Mosleh California State University San MarcosPage 1 One Dimensional Arrays: Structured data types.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Exception Handling How to handle the runtime errors.
Objective Write simple computer program in C++ Use simple Output statements Become familiar with fundamental data types.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
1 Huffman Codes Computing an Optimal Code for a Document.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
1 Huffman Codes Using Binary Files. 2 Getting Started Last class we extended a program to create a Huffman code and permit the user to encode and decode.
Computer Programming II Lecture 9. Files Processing - We have been using the iostream standard library, which provides cin and cout methods for reading.
Chapter Topics The Basics of a C++ Program Data Types
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Chapter 1.2 Introduction to C++ Programming
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Basic File I/O and Stream Objects
Strings A collection of characters taken as a set:
Text Files All the programs you've seen so far have one thing in common: Any data the program uses or calculates is lost when the program ends. In order.
Chapter 9 File Streams Computing Fundamentals with C++ 3rd Edition
Standard Input/Output Stream
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
File I/O.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CHAPTER 4 File Processing.
Reading from and Writing to Files
Reading from and Writing to Files
Presentation transcript:

Array Application Programming

COMP102 Prog. Fundamentals I: Arrays Application / Slide 2 Character Array: Ex. 1 l Note: a palindrome is a word (or words) which read the same forward and backward n kayak n solos n radar n racecar n deified n evil olive n Malayalam n lonely Tylenol n Re-divider n Madam I'm Adam n step on no pets n won ton? not now Run Example 1 to find out which words the computer considers a palindrome

COMP102 Prog. Fundamentals I: Arrays Application / Slide 3 An Encoded Message l What does the following message represent? YMJ%HTRU657%HQFXX%NX%YTT%JFX^& Answer: THE COMP102 CLASS IS EASY! l Problem statement: Write a function that encodes a message (in English) using some encryption method, and another function that decodes the encoded message.

COMP102 Prog. Fundamentals I: Arrays Application / Slide 4 Offset Cipher l We use the simplest method, the “offset cipher”. l Define offset value, which will be used in both the encoding and decoding processes. l The encoding function shifts each character in the message higher by the offset amount. Example: if offset is 1, then a becomes b. l The decoding function shifts each character in the encoded message lower by the same offset amount. Example: if offset is 1, then b becomes a.

COMP102 Prog. Fundamentals I: Arrays Application / Slide 5 Ex. 2: Main Function int main() { char wait; //Temporary input control character // Print out original plain text dump("input.txt"); cout << endl << endl; cout << "Please press a key to encode this file:"; cin >> wait; // Call encode function encode1("input.txt", "cipherfile.txt");

COMP102 Prog. Fundamentals I: Arrays Application / Slide 6 Ex. 2: Main Function // Print out encoded (cipher) text dump("cipherfile.txt"); cout << endl << endl; cout << "Please press a key to decode this file:"; cin >> wait; // Call decode function decode1("cipherfile.txt", "output.txt"); // Print out decoded text dump("output.txt"); return 0; }

COMP102 Prog. Fundamentals I: Arrays Application / Slide 7 Ex. 2: encode1 Function void encode1(const char plaintext[], const char ciphertext[]) { // plaintext is filename where the original // message (in English) is stored // ciphertext is filename in which to store // the encoded message const int offset = 5; // secret offset value char tempchar; // declare input and output file streams ifstream ins; ofstream outs; // open input and output data files ins.open(plaintext); outs.open(ciphertext);

COMP102 Prog. Fundamentals I: Arrays Application / Slide 8 Ex. 2: encode1 Function // read original text message character-by-char ins.get(tempchar); while (!ins.eof()) { // encode the character tempchar += offset; // write encoded character to // the output file outs.put(tempchar); // read next character ins.get(tempchar); } // close files ins.close(); outs.close(); }

COMP102 Prog. Fundamentals I: Arrays Application / Slide 9 Ex. 2: decode1 Function void decode1(const char ciphertext[], const char plaintext[]) { // ciphertext is filename where the // encoded message is stored // plaintext is filename in which to store // the decoded message const int offset = 5; // same offset value char tempchar; // declare input and output file streams ifstream ins; ofstream outs; // open input and output files ins.open(ciphertext); outs.open(plaintext);

COMP102 Prog. Fundamentals I: Arrays Application / Slide 10 Ex. 2: decode1 Function // read encoded message character-by-character ins.get(tempchar); while (!ins.eof()) { // decode the character tempchar -= offset; // write decoded character //to the output file outs.put(tempchar); // read next encoded character ins.get(tempchar); } // close files ins.close(); outs.close(); }

COMP102 Prog. Fundamentals I: Arrays Application / Slide 11 Ex. 2: Dump File to Screen void dump(const char filename[]) { // this function outputs the contents of a file // whose name is given in the parameter filename char tempchar; ifstream ins; // declare input file stream ins.open(filename); // open input file // output the file content character by character ins.get(tempchar); while (!ins.eof()) { cout << tempchar; ins.get(tempchar); } // close input file ins.close(); }

COMP102 Prog. Fundamentals I: Arrays Application / Slide 12 Cipher Improvement l The simple offset cipher is very easy to break (for example, by using frequency analysis to determine the constant offset value). l Better security is provided by a cipher where a key sequence is used for offset values. n The first character in the plain text is offset by the first key value in the key sequence. The second is offset by the second key value, and so on. n The longer the key sequence, the harder it is to break the encryption. n Both the sender and receiver must share the same secret key sequence.

COMP102 Prog. Fundamentals I: Arrays Application / Slide 13 A New Message l What does the following message represent? Z¡¬ L¥]X6¼}un¢┤^╨ÑIXƒ[osåæ Answer: THE COMP102 CLASS IS FUN!

COMP102 Prog. Fundamentals I: Arrays Application / Slide 14 Ex. 3: Main Function int main() { char wait; //Temporary input control character int key[KEY_SIZE], index;// declare key array // declare the input and output file names char input_file_name[16], output_file_name[16]; // user supplies the input and output file names cout << "Enter input file name (max 15 chars):\n"; cin >> input_file_name; cout << "Enter output file name (max 15 char):\n"; cin >> output_file_name;

COMP102 Prog. Fundamentals I: Arrays Application / Slide 15 Ex. 3: Main Function // Print out original plain text dump(input_file_name); cout << endl << endl; //create the seed for the random number generator srand(time(NULL)); //Create decode key array; for (index = 0; index < KEY_SIZE; index++) { //For plain ASCII text, //the offset should be less than 128 key[index] = rand()% MAX_OFFSET; //Print out the random number //cout << key[index] << " "; }

COMP102 Prog. Fundamentals I: Arrays Application / Slide 16 Ex. 3: Main Function cout << "Please press a key to encode this file: "; cin >> wait; // Call encode function encode(input_file_name, "cipherfile.txt", key); // Print out encoded (cipher) text dump("cipherfile.txt"); cout << endl << endl;

COMP102 Prog. Fundamentals I: Arrays Application / Slide 17 Ex. 3: Main Function cout << "Please press a key to decode this file: "; cin >> wait; // Call decode function decode("cipherfile.txt", output_file_name, key); // Print out decoded text dump(output_file_name); cout << endl << endl; return 0; }

COMP102 Prog. Fundamentals I: Arrays Application / Slide 18 Ex. 3: encode Function void encode(const char plaintext[], const char ciphertext[], const int keylist[]) { // plaintext is filename where the input // message (in English) is stored // ciphertext is filename in which to // store the encoded message // keylist is the array with the coding key char tempchar; int index = 0;

COMP102 Prog. Fundamentals I: Arrays Application / Slide 19 Ex. 3: encode Function // declare input and output file streams ifstream ins; ofstream outs; // open input and output data files ins.open(plaintext); outs.open(ciphertext);

COMP102 Prog. Fundamentals I: Arrays Application / Slide 20 Ex. 3: encode Function // read original text character-by-character ins.get(tempchar); while (!ins.eof()) { // encode the character tempchar += keylist[index]; //Use the key sequence in a cycle index = (index+1) % KEY_SIZE; // write encoded character to the output file outs.put(tempchar); // read next character ins.get(tempchar); } // close files ins.close(); outs.close(); }

COMP102 Prog. Fundamentals I: Arrays Application / Slide 21 Ex. 3: decode Function void decode(const char ciphertext[], const char plaintext[], const int keylist[]) { // ciphertext is filename where the encoded // message is stored // plaintext is filename in which to store // the decoded message // keylist is the array with the coding key char tempchar; int index = 0;

COMP102 Prog. Fundamentals I: Arrays Application / Slide 22 Ex. 3: decode Function // declare input and output file streams ifstream ins; ofstream outs; // open input and output data files ins.open(ciphertext); outs.open(plaintext);

COMP102 Prog. Fundamentals I: Arrays Application / Slide 23 Ex. 3: decode Function // read encoded message character-by-character ins.get(tempchar); while (!ins.eof()) { tempchar -= keylist[index]; // decode char //Use the key sequence in a cycle index = (index+1) % KEY_SIZE; // write decoded character to output file outs.put(tempchar); // read next encoded character ins.get(tempchar); } // close files ins.close(); outs.close(); }

COMP102 Prog. Fundamentals I: Arrays Application / Slide 24 Ex. 3: Dump File to Screen void dump(const char filename[]) { // this function outputs the contents of a file // whose name is given in the parameter filename char tempchar; ifstream ins; // declare input file stream ins.open(filename); // open input file // output the file content character by character ins.get(tempchar); while (!ins.eof()) { cout << tempchar; ins.get(tempchar); } // close input file ins.close(); }

COMP102 Prog. Fundamentals I: Arrays Application / Slide 25 Cipher Improvement l Much stronger encryption systems exist (such as PGP, DES, etc.) today. ( For more information on data encryption in general, refer to the book by David Kahn, “The Code-Breakers” The Mac Millan New York 1976.)