C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.

Slides:



Advertisements
Similar presentations
User-Defined Functions Like short programs Can operate on their own data Can receive data from callers and return data to callers.
Advertisements

This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Introducing C++ Elements. CSCE 1062 Outline Main algorithms’ constructs General form of a C++ program {section 2.5} C++ language elements {section 2.1}
Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
CS150 Introduction to Computer Science 1
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
CS107 Introduction to Computer Science Java Basics.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Programming is instructing a computer to perform a task for you with the help of a programming language.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
CS107 Introduction to Computer Science Java Basics.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
EPSII 59:006 Spring Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.
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.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Khalid Rasheed Shaikh Computer Programming Theory 1.
CS Class 03 Topics  Sequence statements Input Output Assignment  Expressions Read pages Read pages 40 – 49 for next time.
Objective: Students will be able to: Declare and use variables Input integers.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Structured Programming (4 Credits) HNDIT Week 2 – Learning Outcomes Design an algorithmic solution for simple problem such as computation of a factorial,
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Constants, Data Types and Variables
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ Programming
What Actions Do We Have Part 1
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
C++ Arrays.
Object-Oriented Programming (OOP) Lecture No. 32
Introduction to the C Language
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
Introduction to C++ Programming
Counting Loops.
Programming Funamental slides
الوحدة الرابعة البرمجة وصياغة حل المسائل البرمجة وأهميتها أهداف الدرس الأول مفهوم البرمجة. الفرق بين المبرمج ومستخدم البرنامج. الحاجة إلى البرامج.
Introduction to C++ Programming
Programs written in C and C++ can run on many different computers
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Fundamental Programming
Fundamental Programming
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
CSCE 206 Lab Structured Programming in C
Presentation transcript:

C++ Basics March 10th

A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation //print output to user } Notes: what follows after // on the same line is considered comment indentation is for the reader; compiler ignores all spaces and new line ; the delimiter for the compiler is the semicolon all statements ended by ; Lower vs. upper case matters!! –Void is different than void –Main is different that main

Variable declaration type variable-name; Meaning: variable will be a variable of type Where type can be: –int//integer –double//real number –char//character Example: int a, b, c; double x; int sum; char my-character;

Input statements cin >> variable-name; Meaning: read the value of variable from the user Example: cin >> a; cin >> b >> c; cin >> x; cin >> my-character;

Output statements cout << variable-name; Meaning: print the value of variable to the user cout << “any message “; Meaning: print the message within quotes to the user cout << endl; Meaning: print a new line Example: cout << a; cout << b << c; cout << “This is my character: “ << my-character << “ he he he” << endl;

If statements if (condition) { S1; } else { S2; } S3; condition S1 S2 S3 TrueFalse

Boolean conditions..are built using Comparison operators == equal != not equal < less than > greater than <= less than or equal >= greater than or equal Boolean operators && and || or ! not

Examples Assume we declared the following variables: int a = 2, b=5, c=10; Here are some examples of boolean conditions we can use: if (a == b) … if (a != b) … if (a <= b+c) … if(a <= b) && (b <= c) … if !((a < b) && (b<c)) …

If example #include void main() { int a,b,c; cin >> a >> b >> c; if (a <=b) { cout << “min is “ << a << endl; } else { cout << “ min is “ << b << endl; } cout << “happy now?” << endl; }

While statements while (condition) { S1; } S2; condition S1 S2 TrueFalse

While example //read 100 numbers from the user and output their sum #include void main() { int i, sum, x; sum=0; i=1; while (i <= 100) { cin >> x; sum = sum + x; i = i+1; } cout << “sum is “ << sum << endl; }

Arrays Used to store a collection of elements (variables) type array-name[size]; Meaning: This declares a variable called which contains elements of type The elements of an array can be accessed as: array-name[0],…array- name[size-1] Example: int a[100]; //a is a list of 100 integers, a[0], a[1], …a[99] double b[50]; char c[10];

Array example //Read 100 numbers from the user #include void main() { int i, a[100], n; i=0; n=100; while (i<n) { cout << “Input element “ << i << “: ”; cin >> a[i]; i = i+1; } //do somehing with it.. }

Problems Write a C++ program to read a sequence of (non-negative) integers from the user ending with a negative integer and write out the average of the numbers the smallest number the largest number the range of the numbers (largest - smallest) Example: –The user enters: 3, 1, 55, 89, 23, 45, -1 –Your program should compute X{3, 1, 55, 89, 23, 45}