Exercise 1.1 Hello world

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

Exercise: Inventing Language

pointers + memory double x; string a; int x; main overhead int y; main overhead

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

CS1500 Algorithms and Data Structures for Engineering, FALL Virgil Pavlu, Jose Annunziato,

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

Week 3: Pointers (Part 2)

VARIABLES & ASSIGNMENTS

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Lecture on pointers, references, and arrays and vectors

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

Arrays. Week 4. Assylbek Jumagaliyev

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Integer Data Types. Data Type. Data Types. int, short int, long int

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

Introduction to C ++

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

6. Pointers, Structs, and Arrays. 1. Juli 2011

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 1

REVIEW. The C++ Programming Language. CS 151 Review #2

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

LECTURE 02 INTRODUCTION TO C++

C++ For Science and Engineering Lecture 15

2 nd Week Lecture Notes

A First Program - Greeting.cpp

C Pointers. 7.2 Pointer Variable Definitions and Initialization

Chapter 15 - C++ As A "Better C"

Discussion 1H Notes (Week 4, April 22) TA: Brian Choi Section Webpage:

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Streams. Ali Malik

CSc Introduction to Computing

Pointers II. Class 31

CMSC 202 Midterm Exam 1 Fall 2015

struct Buffer { Buffer(int s) { buf = new char[s]; } ~Buffer() { delete [] buf; } char *buf; };

CS242 COMPUTER PROGRAMMING

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

CS31 Discussion 1E. Jie(Jay) Wang Week5 Oct.27

C++ Arrays. To declare an array in C++, the programmer specifies the type of the elements and the number of elements required by an array as follows

C and C++ 7. Exceptions Templates. Alan Mycroft

My First Command-Line Program

This watermark does not appear in the registered version - Slide 1

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Object Oriented Programming COP3330 / CGS5409

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

today cs3157-fall2002-sklar-lect05 1

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

Introduction to Programming using C++

Pointers and Strings. Adhi Harmoko S, M.Komp

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

EE 355 Lab 4 - Party Like A Char Star

Programming with C++ as a Second Language

The C++ Language. Arizona State University 1

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

Intermediate Programming, Spring 2017*

Chapter 2. Outline. Simple C++ Programs

Object Reference and Memory Allocation. Questions:

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

C++ For Science and Engineering Lecture 2

CSC 126 FINAL EXAMINATION FINAL Spring 2012 B. Name (last, First) Instructor. Total Possible. Received

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

Engineering Problem Solving with C++, Etter

CS 103 Lab 6 - Party Like A Char Star

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

Looping and Counting. Lecture 3 Hartmut Kaiser hkaiser/fall_2012/csc1254.html

Computer Science II Lecture 1 Introduction and Background

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

C++ For Science and Engineering Lecture 12

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS

CMPS 221 Sample Final

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Programming in C++: Assignment Week 8

6.096 Introduction to C++ January (IAP) 2009

Looping and Counting. Lecture 3. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

Lab # 02. Basic Elements of C++ _ Part1

Engineering Problem Solving with C++, Etter/Ingber

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

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Binomial pricer (1.1)

Programmazione. Prof. Marco Bertini

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

Strings in C++ Dr. Ferdin Joe John Joseph Kamnoetvidya Science Academy

Learning Objectives. Introduction to Arrays. Arrays in Functions. Programming with Arrays. Multidimensional Arrays

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Scientific Computing

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

CHAPTER 3 BASIC INSTRUCTION OF C++

CSCE 110 PROGRAMMING FUNDAMENTALS

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

8. The C++ language, 1. Programming and Algorithms II Degree in Bioinformatics Fall 2017

Transcription:

Exercise 1.1 Hello world The goal of this exercise is to verify that computer and compiler setup are functioning correctly. To verify that your setup runs fine, compile and run the hello world example #include <iostream> using namespace std ; int main() { cout << Hello World << endl ; return 0 ;

Exercise 1.2 Counting chars in a string The goal of this exercise is to learn basic pointer skills. We will write a C++ program that analyzes the character content of a string that is given to you by the user of your program. The program should display the number of uppercase and lowercase characters, the number of digits and the number of other characters. Approach a) Step 1: write a small program that reads in a string from the terminal up to a newline character and prints that same string again to the terminal. You can use the code fragment below include <iostream> int main() { // Allocate buffer char buf[20] ; // Read in string from terminal std::cout << "Enter String: " ; std::cin.getline(buf,20) ; // Write string back to terminal std::cout << buf << std::endl ; return 0 ;

Exercise 1.2 Counting chars in a string b) Step 2: Add code that loops over the string and looks at the string one character at a time through a pointer. You are not allowed to use an integer index variable in the loop, you must use a pointer as loop variable Think about the following: How do you create a pointer and how do you make it point to the first character of the string? How do you make the pointer point to the next character in the string? How do you know that you re at the end of your string? c) Step 3: Finally add code that determines if each given character is uppercase, lowercase, a digit or otherwise. Think about how character literals work. First write a line of code that determines if a given character is the uppercase A. Now modify this code to determine if the character is any upper case letter A-Z. Before you start this find out what the ASCII table looks like (e.g. Google ASCII table) and exploit the ordering of the ASCII characters. Add similar code for lowercase characters, digits and other characters and make your program print how many of each are in the string.

The goal of this exercise is to learn how to use pointers and references with functions. The task you will need to perform is to implement an algorithm that sorts the contents of an array of 10 integers or strings in increasing order Part 1 Integer sorting a) Step 1 - Write a small main program that allocates an array of 10 integers and fill them with random values. #include <iostream> using namespace std ; int main() { // Array in random order int arr[10] = { 9, 8, 5, 2, 4, 3, 6, 1, 7, 0 ; return 0 ;

b) Step 2 - Add code to the program that prints the content of the array arr to the screen. We will need this later in the exercise to verify that the sorting algorithm is working. c) Step 3 Now we need to introduce a sorting algorithm to order the array. The goal of this exercise is to learn how to program an existing algorithm called bubble sort. This is not the fastest algorithm that exists, but it is very simple to code: Here is how it works: given an array A of length n, you can sort its contents by executing the following double loop: for (i=0; i<n-1; i++) { for (j=0; j<n-1-i; j++) { // if A[j+1]>A[j] swap A[j] and A[j+1] This loop considers every possible pair of elements A[i] and A[j] in the array and swaps them if they are in the incorrect order. Note that the actual code for swapping is not in the example above, it is a C++ comment that explains what should be done here (the actual code will depend on the data type in array A) d) Introduce in your program a function sort(int* A, int lena) that implements the code above. Be sure to add both the forward declaration and the function in your program. Run the program and confirm that nothing happens yet, because the sorting algorithm is not yet complete: the code that compares and swaps the elements is still missing.

e) Step 4 Now we will provide the missing piece of the sort algorithm: the compare and swap. Instead of coding it in the swap function we will delegate both steps to a new function called order(). Add a declaration and an implementation of an order() function that takes references to two ints as input arguments. In the implementation of the function add code that compares the values of the 1 st and 2 nd argument and that swaps their values if the 2 nd argument is greater than the 1 st argument. Note that the order() routine you re asked to develop here has a different functionality than the swap() routine that appeared in this mornings course material: swap() always swaps the elements, the order() function only does that if the order is incorrect Run the program and verify that the code works correctly.

Part 2 String sorting a) Make a copy of the main program and change it so that it allocates an array of 10 const char* pointers and initialize them with 10 string literals. For example const char* arr[10] = { "C++", "Fortran", "Pascal","Algol","Visual Basic","Python", "Java","Eiffel","Assembly", C" ; b) Adjust the arguments of the sort() and order() functions to accept the new type of array. What type of arguments should the order() now take? The easiest way to figure it out is to think of const char* as the new data type that replaces the data type int, and exactly replace int with const char*. Compile and run your program. Verify if your program works correctly by running it.

Part 2 String sorting c) Your program compiles but does not function correctly because the operation a<b for const char* is legal C++ but does not do what you think it should do: it compares the memory addresses of the pointers a and b and thus sorts them according by position in memory. To perform a lexographical sort (that means we sort according to alphabetical order of the string content), we can use a C++ helper function that will tell us of the content of string a is alphabetically lower or higher than that of string b To use this function we need to adjust the order() function. First include the header file <string.h> in your program, as this contains the declaration of the function we need. Next use the function int strcmp(const char* a,const char* b) to construct a new condition statement that replaces the if (a>b) statement to determine if the first or second argument of the order() function should come first. The strcmp() function is designed such that if the content of a is alphabetically before the content of b the return value is >0. If a is alphabetically behind b then the return value is <0. Run the program and observe that it now work correctly.