CS-103 COMPUTER PROGRAMMING

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 1 – Introduction to Computers and C++ Programming Outline 1.6 Machine Languages, Assembly Languages,
File I/O in C++. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk);is stored on a secondary storage device.
Introduction Kingdom of Saudi Arabia Shaqra University
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline History of C and C++ C++ Standard Library Object Technology Basics.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
Three types of computer languages
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Chapter 01: Introduction to Computer Programming
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
History of C and C++ C++ evolved from C ANSI C C++ “spruces up” C
Computer Engineering 1 nd Semester Dr. Rabie A. Ramadan 2.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 1 February 8, 2005.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Week 1 Algorithmization and Programming Languages.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:
INTRODUCTION Kingdom of Saudi Arabia Princess Nora bint Abdul Rahman University College of Computer Since and Information System CS240.
File handling in C++ BCA Sem III K.I.R.A.S. Using Input/Output Files Files in C++ are interpreted as a sequence of bytes stored on some storage media.
FILE I/O IN C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
Chapter 2 part #1 C++ Program Structure
CHAPTER 1: INTRODUCTION C++ Programming. CS 241 Course URL: Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
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.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
1 CS 192 Lecture 4 Winter 2003 December 8-9, 2003 Dr. Shafay Shamail.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
CSC201: Computer Programming
1.2 What is a Computer? Computer Computer programs Hardware Software
Introduction to C Language
Chapter 2 - Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
Beginning C++ Programming
Computer Engineering 1nd Semester
Copyright © 2003 Pearson Education, Inc.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
1.13 The Key Software Trend: Object Technology
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Programming Fundamentals Lecture #3 Overview of Computer Programming
Programs written in C and C++ can run on many different computers
Capitolo 1 – Introduction C++ Programming
Chapter 2 - Introduction to C Programming
Introduction to C Programming
File I/O in C++ II.
Chapter 1 c++ structure C++ Input / Output
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

CS-103 COMPUTER PROGRAMMING LECTURE 1 Hafiza Maria Maqsood FAST NU – CHINIOT FSD campus Hafiza Maria Maqsood, FAST-NU, Chiniot - FSD Campus

Tentative Evaluation Breakdown Assignments 10 Quizzes 20 Project 5 Sessional - 1 12.5 Sessional - 11 Final 40 Note: The evaluation Breakdown plus course outline for all sections will be same Grading will be combined so don’t rely on your class position look for the batch position Hafiza Maria Maqsood, FAST-NU, Chiniot - FSD Campus

Text Books “Object Oriented Programming in C++”, Author (s): Robert Lafore. “C++ How to program”, Author (s): M. Deitel & Dietel. “Turbo C Programming”, Author (s): Robert Lafore. Hafiza Maria Maqsood, FAST-NU, Chiniot - FSD Campus

Making Software Load into memory and start execution Problem Executable file Develop Algorithm Compile the code Express Algorithm in the form of pseudo code or flow chart Write algorithm in C/C++/Java … Language Writing code in “C++” language means, we must have to follow a certain rules of “C++” language. This is called syntax of “C++” language. Hafiza Maria Maqsood, FAST-NU, Chiniot - FSD Campus

Making Software Load into memory and start execution Compiler “Turbo C” in DOS “gcc” or “g++” in Linux “Visual C++” in windows “Borland” in DOS/Windows. Problem Executable file Develop Algorithm Compile the code Express Algorithm in the form of pseudo code or flow chart Write algorithm in C/C++/Java … Language Writing code in “C++” language means, we must have to follow a certain rules of “C++” language. This is called syntax of “C++” language. Hafiza Maria Maqsood, FAST-NU, Chiniot - FSD Campus

Basics of a Typical C++ Environment Loader Primary Memory Program is created in the editor and stored on disk. Preprocessor program processes the code. Loader puts program in memory. CPU takes each instruction and executes it, possibly storing new data values as the program executes. Compiler Compiler creates object code and stores it on disk. Linker links the object code with the libraries, creates a.out and stores it on disk Editor Preprocessor Linker   CPU . Disk Phases of C++ Programs: Edit Preprocess Compile Link Load Execute Hafiza Maria Maqsood, FAST-NU, Chiniot - FSD Campus

2 // A first program in C++ 1 // Fig. 1.2: fig01_02.cpp 2 // A first program in C++ 3 #include <iostream> 4 5 int main() 6 { 7 std::cout << "Welcome to C++!\n"; 8 9 return 0; // indicate that program ended successfully 10 } Comments Written between /* and */ or following a //. Improve program readability and do not cause the computer to perform any action. preprocessor directive Message to the C++ preprocessor. Lines beginning with # are preprocessor directives. #include <iostream> tells the preprocessor to include the contents of the file <iostream>, which includes input/output operations (such as printing to the screen). C++ programs contain one or more functions, one of which must be main Parenthesis are used to indicate a function int means that main "returns" an integer value. More in Chapter 3. A left brace { begins the body of every function and a right brace } ends it. Prints the string of characters contained between the quotation marks. The entire line, including std::cout, the << operator, the string "Welcome to C++!\n" and the semicolon (;), is called a statement. All statements must end with a semicolon. return is a way to exit a function from a function. return 0, in this case, means that the program terminated normally. Welcome to C++! Hafiza Maria Maqsood, FAST-NU, Chiniot - FSD Campus

A Simple Program: Printing a Line of Text std::cout Standard output stream object “Connected” to the screen std:: specifies the "namespace" which cout belongs to std:: can be removed through the use of using statements << Stream insertion operator Value to the right of the operator (right operand) inserted into output stream (which is connected to the screen) std::cout << “Welcome to C++!\n”; \ Escape character Indicates that a “special” character is to be output

A Simple Program: Printing a Line of Text

File Handling stream - a sequence of characters interactive (iostream)  cin - input stream associated with keyboard.  cout - output stream associated with display. file (fstream)  ifstream - defines new input stream (normally associated with a file).  ofstream - defines new output stream (normally associated with a file). Hafiza Maria Maqsood, FAST-NU, Chiniot - FSD Campus

To access file handling routines: #include <fstream.h> 2: To declare variables that can be used to access file: ifstream in_stream; ofstream out_stream; 3: To connect your program's variable (its internal name) to an external file : in_stream.open("infile.txt"); out_stream.open("outfile.txt"); 4: To see if the file opened successfully: if (in_stream.fail()) { cout << "Input file open failed\n"; exit(1); // requires <stdlib.h>}

To get data from a file (one option), must declare a variable to hold the data and then read it using the extraction operator: int num; in_stream >> num; [Compare: cin >> num;] 6: To put data into a file, use insertion operator: out_stream << num; [Compare: cout << num;] NOTE: Streams are sequential – data is read and written in order – generally can't back up. 7: When done with the file: in_stream.close(); out_stream.close();

File Modes Name Description ios::in Open file to read ios::out Open file to write ios::app All the date you write, is put at the end of the file. It calls ios::out ios::ate All the date you write, is put at the end of the file. It does not call ios::out ios::trunc Deletes all previous content in the file. (empties the file) ios::nocreate If the file does not exist, opening it with the open() function gets impossible. ios::noreplace If the file exists, trying to open it with the open() function, returns an error.

File I/O Example: Writing #include <fstream> using namespace std; int main(void) { ofstream outFile(“fout.txt"); outFile << "Hello World!"; outFile.close(); return 0; }

File I/O Example: Reading #include <iostream> #include <fstream> int main(void) { ifstream openFile(“data.txt"); //open a text file data.txt char ch; while(!OpenFile.eof()) OpenFile.get(ch); cout << ch; } OpenFile.close(); return 0;

File I/O Example: Reading #include <iostream> #include <fstream> #include <string> int main(void) { ifstream openFile(“data.txt"); //open a text file data.txt string line; if(openFile.is_open()){ // while(!openFile.eof()){ getline(openFile,line);//read a line from data.txt and put it in a string cout << line; } else{ cout<<“File does not exist!”<<endl; exit(1);} openFile.close(); return 0;