WARM UP LESSONS BARE BASICS

Similar documents
2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Fundamentals of Programming CS-110. Lecture 2

Getting started with C++ (Part 2)

BITG 1233: Introduction to C++

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

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

CS242 COMPUTER PROGRAMMING

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

C++ Programming: From Problem Analysis to Program Design, Third Edition

Introduction to Programming EC-105. Lecture 2

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

2 nd Week Lecture Notes

The C++ Language. Arizona State University 1

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Introduction to C++ (Extensions to C)

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

CHAPTER 3 Expressions, Functions, Output

UNIT- 3 Introduction to C++

CS2141 Software Development using C/C++ C++ Basics

Programming Language. Functions. Eng. Anis Nazer First Semester

Programming. C++ Basics

CHAPTER 3 BASIC INSTRUCTION OF C++

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Expressions, Input, Output and Data Type Conversions

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

C++ basics Getting started with, and Data Types.

Introduction to the C++ Programming Language

Fundamental of Programming (C)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Fundamentals of Programming

LECTURE 02 INTRODUCTION TO C++

Chapter 4 - Notes Control Structures I (Selection)

Objectives. In this chapter, you will:

Chapter 3. Numeric Types, Expressions, and Output

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Computer Programming : C++

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

A First Program - Greeting.cpp

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

I/O Streams and Standard I/O Devices (cont d.)

1.3b Type Conversion

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

The Hyderabad Public School, Begumpet, Hyderabad, A.P

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Programming Language A

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

CPE Summer 2015 Exam I (150 pts) June 18, 2015

C Programming

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

C++ Programming: From Problem Analysis to Program Design, Third Edition

3.1. Chapter 3: The cin Object. Expressions and Interactivity

Definition Matching (10 Points)

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

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

Engineering Problem Solving with C++, Etter/Ingber

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

Chapter 3 Problem Solving and the Computer

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Program Organization and Comments

Chapter 4: Basic C Operators

Honors Computer Science C++ Mr. Clausen Program 6A, 6B, 6C, & 6G

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Chapter 1 INTRODUCTION

Unit 3. Constants and Expressions

READ THIS NOW! Do not start the test until instructed to do so!

Introduction to C. Systems Programming Concepts

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Ch 6. Functions. Example: function calls function

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

Fast Introduction to Object Oriented Programming and C++

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

CS3157: Advanced Programming. Outline

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

Why VC++ instead of Dev C++?

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Input And Output of C++

Transcription:

WARM UP LESSONS BARE BASICS CONTENTS Common primitive data types for variables... 2 About standard input / output... 2 More on standard output in C standard... 3 Practice Exercise... 6 About Math Expressions / Operands / Operators... 7 Casting... 7 Practice Exercise... 8 Base Conversion... Error! Bookmark not defined. Convert other bases to base-10... Error! Bookmark not defined. Practice exercises... Error! Bookmark not defined. 1

COMMON PRIMITIVE DATA TYPES FOR VARIABLES int float char string (C++) Samples for variables declaration: char grade; // declaring the variable symbol age as integer type int age; // declaring the variable symbol age as integer type int x, y, z; // declaring multiple of them with the same data type float amount; // float means allowing the data to contain decimal places. Bool isvalid; // Boolean variable to contain either true or false // you can do declaration and initialization together in a single expression. E.g. char grade = A ; int age = 90; int x=10, y=50, z; float amount = 10.94; bool isvalid = true; To stay within scope of this workshop, these primitive data types will be sufficient. Later, you may learn how to create your own data types. ABOUT STANDARD INPUT / OUTPUT C scanf, getc, gets, etc. Printf Review the content from the C book C++ cin, cin.getline(), etc. cout Look into Basic C++ I/O Stream document here. Sample 1: In C standard Output stream version: #include <stdio.h> printf( "I am alive! Beware.\n" ); Note: the difference in the header include expression and cout vs printf In C++ standard Output stream version: #include <iostream> #include <iomanip> using namespace std; cout << "I am alive! Beware.\n"; 2

MORE ON STANDARD OUTPUT IN C STANDARD use printf from the example above : printf ( "The area of this rectangle is : %d metres.\n", area ) ; Other display format strings %d print the value of a decimal (based 10) integer %5d - print the decimal integer in five character positions, right justified. %-5d - left justified. %c - print a single character. %s - print a sequence of character. (more on characters in later days) %f - print the value of a signed, decimal, floating point number. %6.2f - print the decimal floating point number in six character positions, right justified and to an accuracy of two decimal places Sample 2: Try the following and see they all look like: IMPORTANT: Do NOT just use the keyboard Copy & Paste. You must type them all in yourself. In C syntax: // don t forget the header files void main() int abc = 97; printf("1(%5d)\n", abc); printf("2(%-5d)\n", abc); printf("3(%10c)\n", 'A'); printf("4(%-10c)\n", 'A'); printf("5(%20s)\n", "hello!"); printf("6(%-20s)\n", "hello!"); printf("7(%*d)\n", 10, abc); printf("8(%*c)\n", 10, abc); return; In C++ syntax: // don t forget the header files void main() int abc = 97; cout << "1(" << setw(5) << abc << ')' << '\n'; cout << "2(" << setiosflags(ios::left) << setw(5) << abc << ')' << '\n'; cout << "3(" << setiosflags(ios::right) << setw(10) << 'A' << ')' << '\n'; cout << "4(" << left << setiosflags(ios::left) << setw(10) << 'A' << ')' << '\n'; cout << "5(" << right << setw(20) << "hello!" << ')' << '\n'; cout << "6(" << left << setiosflags(ios::left) << setw(20) << "hello!" << ')' << '\n'; cout << "7(" << right<< setw(10) << abc << ')' << '\n'; cout << "8(" << setw(10) << (char) abc << ')' << '\n'; return; 3

Sample 3: (do NOT forget the header files) C version: int n0, n1, n2, n3; char term = ','; printf(" Enter four digits: e.g. 6,10,49,3 \n"); scanf("%d, %d, %d, %d", &n0, &n1, &n2, &n3); // the following will generate an error, fix it printf("you have entered %d, %d, %d, %d\n", n, n1, n2, n3); // C++ version: int n0, n1, n2, n3; char term = ','; cout << " Enter four digits: e.g. 6,10,49,3 \n"; cin >> n0 >> term >> n1 >> term >> n2 >> term >> n3; // the following will generate an error, fix it cout << "You have entered " << n << " " << n1 << " " << n2 << " " << n3 << endl; Sample 4 ( in C++ version) // don t forget to enter the basic standard IO header files. // See samples at the beginning of this doc. // you will need include a math.h in order to use the function sqrt( ) #include <math.h> void main() int base; int height; char ch; cout << "Enter Base, height: "; cin >> base >> ch >> height; cout << endl << "base = " << base << ", height = " << height << ", hypotenuse = " << sqrt((base * base) + (height * height)) << endl; return; 4

PRACTICE EXERCISE 1. Rewrite this sample 4 with C standard Output. 2. Create a program to ask user to enter a the base and height, then display the result of hypotenuse. 3. Create a program to ask user to enter a number as Fahrenheit. Your program will convert this to Celsius. Fahrenheit to Celsius : C = ( F 32) 5/9 4. Then, create another program to ask user to enter Celsius. Your program will convert this to Fahrenheit. 5

ABOUT MATH EXPRESSIONS / OPERANDS / OPERATORS Expressions can be as simple as a single value and as complex as a large calculation or even some conditional expressions (will be covered a bit later), etc. They are made up of two elements, operators and operands. Operators: Arithmetic operators : Binary : + - / * % o e.g. remainder = num1 % num2 ; unary : ++ or or + & - by itself o e.g. ++x; o --x; prefix vs posfix unary : --x vs x o e.g. int x = 10, y; o y = --x; o y = x--; // what is the final value of x & y Relational operators : (will be further discussed on Day 2) == > < >= <=! Logical operators: (will be further discussed on Day 2) && LValue rule / Assignment y = x + 10; x + 10 = y // correct // invalid int x = 10; int y = 20, z = 30; bool what; x = (y = (z = 40) ); what = ( y= (z==30) ); // what is the value of x after assignment CASTING force the compiler to regard a value being of a different type by the use of casting int i = 3, j = 2 ; float fraction ; fraction = (float) i / (float) j ; cout << "fraction :" << fraction << endl ; Try this one. Compile and run. Look at the answer. int i = 3, j = 2 ; float fraction ; fraction = i / j ; cout << "ans:" << fraction << endl ; After you have tried the left one, try this one. Compile and run. See what happen to the answer. int i = 3, j = 2 ; float fraction ; fraction = (float) i / (float) j ; cout << "ans :" << fraction << endl ; 6

PRACTICE EXERCISE 1) Write a program to: - ask user to provide the radius of a circle - calculate the circumference and area. - display all values 2) Plotting the * triangle. Write a function that outputs a right triangle of height and width n, so n = 6 would look like the image on the right. a. You can assume there are only 6 levels if you are not familiar with loop structure. Otherwise, you should do (b) only. b. Should allow user to decide the number of levels. * ** *** **** ***** ****** Tip: If you have difficulty in figuring out the loop structure, hardcode the layout of 6 levels, find the pattern, and convert to loop structure. 3) Like (2), except the triangle is an isosceles triangle see the image on the right. * *** ***** ******* ********* 4) You borrow $1000. Your annual interest rate is 12%. You pay back $100 a month. If it is based on compound interest rate, how much do you still own after 4 months? The display should be something like this:.e.g. interest you own at 1 st month = $1000 * (12%/12) = $10 You pay off $100. Thus you still owe $910 at the end of 1 st month i.e. $1000 + $10 - $100. interest you own at 2 nd month = $910 * (12%/12) = $9.10 You pay off $100. Thus you still owe $910 at the end of 1 st month i.e. $910 + $9.10 - $100. Output should look like this: Month total interest paid still owe 1 $ 10.00 $910.00 2 $ 9.10 $819.10 3..etc. 5) Write a program to perform factorial of 2 to 6. Display the result backward as shown on the right. (note: your program must do the calculation, not hardcode the result. 6!= 720 5!= 120 4!= 24 3!= 6 2!= 2 7