Content of Topic 1 EP241 Computer Programming

Document Sample
Content of Topic 1 EP241 Computer Programming Powered By Docstoc
					                                                                            Content of Topic 1
             EP241 Computer Programming
                                                                                Introduction

    Topic 1                                                                     The Course

                                                                                The Computer

                                                                                The Computer Programming
 Dr. Ahmet BİNGÜL
                                            Modifications by                    First C++ Program
 Department of                              Dr. Andrew BEDDALL
 Engineering Physics
                                            Department of Electric and          Compiling and Running a First C++ Program
 University of Gaziantep                    Electronics Engineering

                               Sep 2012

                                                                  Sayfa 1                                                              Sayfa 2

 Computer programming is an essential part of the
  work of many scientists and engineers.

 This semester we will teach
  C++ Programming Language which

      is not easy to learn (compared to Fortran)
      has a lot of technical programming details
      is good at system and hardware programming
      is mostly prefered by engineers and young programmers!
      is widely used in the software industry

                                                                  Sayfa 3                                                              Sayfa 4

                                                                            Web Resources for the Course
                                                                             Course web page (for Exam questions/solutions/results)

                                                                             University of Gaziantep C++ resource page
                                                                                  - Tutorials, Sample Programs, Howtos

                           THE COURSE                                        C++ Resources Network:

                                                                             C++ Reference:

                                                                             Türkçe „C Programlama Dili‟ne Giriş‟:

                                                                             Also search Google and Wikipedia for any
                                                                              of the expressions used in this course.

                                                                  Sayfa 5                                                              Sayfa 6

The Course Books                                                   The Course Content
                  Programming with C++                                 Introduction to Programming and C++
                                                                       Structure of C++
                  John R. Hubbard
                                                                       C++ Basics
                  Schaum Outline Series (2000)
                                                                       Control Structures: Selection & Loops
                  ~ 15 $                                               Functions
                                                                       Arrays

                  Practical C++ Programming                            References & Pointers
                                                                       Dynamic Arrays and Vectors
                  Steve Qualline
                                                                       File Processing
                  O‟Reilly Media (2003)
                                                                       Basic Classes
                  ~ 25 $
                                                        Sayfa 7                                                                Sayfa 8

Lectures, Labs, Attendance, Exams                                  Lectures, Labs, Attendance, Exams
FIRST EDUCATION                                                    SECOND EDUCATION
 Thursdays                                                         Thursdays
   two-hour lecture                                                   two-hour lecture
   13:00-15:00 Engineering of Physics Salon 3                         20:40-22:20 Engineering of Physics Salon 3

 Fridays                                                           Fridays
   two-hour programming session                                       two-hour programming session
   08:30-10:20 BIM lab                                                17:00-18:50 BIM lab

 Attendance                                                        Attendance
   You must attend at least 70% of the course                         You must attend at least 70% of the course

 Exams                                                             Exams
   All written exams                                                  All written exams
    1st midterm       25%     ?? October 2012                         1st midterm       25%     ?? October 2012
    2nd midterm       25%     ?? December 2012                        2nd midterm       25%     ?? December 2012
    Final             40%     ?? January 2013                         Final             40%     ?? January 2013
    Quiz              10%                                             Quiz              10%

                                                        Sayfa 9                                                                Sayfa 10

Grading                                                            Computer Labs
FIRST & SECOND EDUCATION                                            We will learn C++ using the Windows operating system
Independent from the arithmetic mean of the class,                   You may also use a Linux environment, e.g. Ubuntu or gul4.bim .
the following rules will be applied for each student:
                                                                    Every week you should write a few programs yourself
                                                                     in the BIM lab (and more in your free time).
    00 – 24 >>          FF
    25 – 39 >>          FD                                         In the BIM Computer Labs your programs will be
                                                                     deleted when the computer reboots. So you need
    40 – 45 >>          DD                                          to save them on a flash drive or on the internet.
    46 – 54 >>          DC
    55 – 59 >>          CC
    60 – 64 >>          CB
    65 – 74 >>          BB                                                         DO NOT FORGET TO BRING
    75 – 84 >>          BA                                                    YOUR LAPTOP or FLASH DISK WITH YOU
    85 – 100 >>         AA
                                                        Sayfa 11                                                               Sayfa 12

                                                                            The Computer

                                                                             A computer is a machine that
                                                                              manipulates data according to
                                                                              a set of instructions.
                        THE COMPUTER
                                                                             First computers
                                                                              were developed in 1940–1945,
                                                                              they were very large in size.
                                                                             Modern computers are
                                                                              based on integrated circuits
                                                                              making them very fast
                                                                              and small in size.

                                                                 Sayfa 13                                                         Sayfa 14

                                                                            Parts of a Digital Computer
                                                                            A computer can be divided into two main parts:
                                                                            Hardware and Software.

                                                                            Hardware (=Donanım)

                                                                                  Hardware is the electronic and
                                                                                  mechanical parts of the computer.

                                                                                         - Storage Units
                                                                                         - Input Units
                                                                                         - Output Units
                                                                                         - Process Units

                       EP241 - Computer Programming              Sayfa 15                                                         Sayfa 16

Parts of a Digital Computer                                                 Parts of a Digital Computer
                                                                            Software (=Yazılım)

                                                                            Software consists of programs loaded from storage units.
                                                                            The programs execute on the computer hardware forming,
                                                                            for example,
  A program is input from an HDD(1) and executed in RAM(2).
  Data is input from a keyboard(3) which is again stored in RAM(4).
                                                                                Operating System (OS)
                                                                                Compilers
  The CPU operates on the program and data in RAM(5) and
                                                                                Application Programs
  outputs results to the HDD(6) as well as the monitor(7).
  This is all controlled by the CPU requiring only basic
  data flow instructions from the programmer.
                                                                 Sayfa 17                                                         Sayfa 18

Parts of a Digital Computer

     Many programming languages require a compiler.
     A compiler translates the statements of program written in
     a high level language into a low level language (called the                 INTRODUCTION TO PROGRAMMING
     machine code).
     Examples are:
                                                            Sayfa 19                                                                        Sayfa 20

Computer Programming                                                   Generating an Executable File
See                                       set of instructions written by the programmer
                                                                                            (high level code) “hello.cpp”
Computer programming (coding) is the process of
    writing,                                                                                Translates the source code into machine
    Testing / debugging / troubleshooting                                                   code (low level code)
    maintaining
the source code of computer programs.                                                        The compiled code
                                                                                             (low level code) ”hello.obj” or ”hello.o”
The source code is written in a programming language, e.g.
               // A simple C++ program
                                                                                             Generates a machine code
               #include <iostream>
                                                                                             (executable code or executable program)
                int main(){
                  cout << "Hello World!\n";
                  return 0;                                                                  The executable program that you run under
                }                                                                            an operating system “hello.exe”
                                                            Sayfa 21                                                                        Sayfa 22

Problem Solving with Computers                                         Algorithms & Flow Charts
Problem solving with computers involves several steps:                  Algorithm
                                                                         gives a step-by-step description
                                                                         of the solution
1.    Clearly define the problem
2.    Analyse the problem and formulate a method to solve it             S1:   Start
                                                                         S2:   Input N
3.    Describe the solution in the form of an algorithm.                 S3:   Set M = 1 and F = 1
4.    Draw a flowchart of the algorithm                                  S4:   Set F = F*M
                                                                         S5:   If M = N GOTO S7
5.    Write the computer program                                         S6:   Set M = M + 1 and GOTO S4
6.    Compile and run the program (debugging)                            S7:   Output F
                                                                         S8:   End
7.    Test the program (debugging)
8.    Interpretation of results                                         Flow chart
                                                                         gives the logical flow of the
                                                                         solution in a diagrammatic form.

                                                            Sayfa 23                                                                        Sayfa 24

                    Beginning or end of an algorithm

                    Input or output of information

                    A computation
                                                                                                        A FIRST C++ PROGRAM
                    Decision making

                   The beginning of the repetition structure.

                   The direction of flow of the algorithm.
                   Circles with arrows connect the flowchart between pages.

                                                                             Sayfa 25                                                       Sayfa 26

What is C++?                                                                            A First C++ Program
                                                                                                 // First C++ program
 C++ (pronounced "C plus plus")                                                                 #include <iostream>
      is a general-purpose and middle-level programming language                                using namespace std;
      is an enhancement to C
      was developed by Danish                                                                   int main()
       computer scientist Bjarne Stroustrup
       in 1979 (called C with Classes)
       at Bell Labs (named C++ in 1983)                                                             cout << "Hello World!";
      was ratified in 1998 ISO/IEC 14882:1998                                                      return 0;
      and in 2003 ISO/IEC 14882:2003                                                            }

 A revised ISO C++ standard, known informally as C++0x                                     Program source file name: hello.cpp
  has been produced.
                                                                                            In general, C++ files have extentions:
  You can find a recent talk given by him at CERN:                                             .cpp, .c++, .cxx, .cc

                                                                             Sayfa 27                                                       Sayfa 28

              1:    // First C++ program
              2:    #include <iostream>                                                 Compile and Run
              3:    using namespace std;                                                We will use Dev-C++ compiler
              5:    int main()                                                
              6:    {
              7:       cout << "Hello World!";
              8:       return 0;
              9:    }
   Line 1: Lines starting with // are considered as comment.                                                                             output
   Line 2: Lines starting with # are directives for preprocessor
    #include <iostream> tells the compiler to include the
    iostream file containing declarations of basic input output.
   Line 3: All elements (variables, objects, ...) of C++ library declared
    in std namespace. cout is the part of std namespace.
   Line 4: An empty line does nothing but helps readability.
   Line 5: Actual program, program starts with main() function.
    Each C++ program must have only one main() function.
    The beginning and end of the main() block is indicated by braces { }.
   Line 7: Outputs “Hello World” to the user screen.
   Line 9: return statement terminates the function (here main program).
    return 0 sends a message to OS: “program ends without an error”
                                                                             Sayfa 29                      EP241 - Computer Programming     Sayfa 30

 Our Second C++ Program                                                            Standard Input and Output (I/O)
       // Calculates the sum of two integers                                        The standard C++ library includes the header file
       #include <iostream>
       using namespace std;                                                          iostream, where the input and output stream
                                                                                     objects are declared.
       int main() {
         int a, b, total;                                                              cout to output data to the screen
         cout << "Enter two integers: ";
         cin >> a >> b;                                                                cin to input data from the keyboard.
         total = a + b;
         cout << "The sum is "                                                      This specific file (iostream) can be found usually
              << total << endl;
         return 0;
                                                                                     under the folder:
                                                                                              for Linux (GCC): /usr/include/
                                                                                      for Windows (Dev-C++): C:\Dev-Cpp\include\

2009                      EP241 - Computer Programming                  Sayfa 31                                                                 Sayfa 32

 Standard Input and Output (I/O)                                                   Standard Input and Output (I/O)
  Basic Output                                                                     Basic Input
   cout    <<   "Hello World!";             Outputs: Hello World!                    cin >> a;           reads a variable from keyboard to a variable a
   cout    <<   "Hello " << "World!";       Outputs: Hello World!                    cin >> a >> b;      reads variables from keyboard to a and b
   cout    <<   1453;                       Outputs the number: 1453
                                                                                      // Calculates the sum of two integers
   cout    <<   x;                          Outputs the content of: x
                                                                                      #include <iostream>
                                                                                      using namespace std;
  Line break on output
   cout << "University of ";                    outputs:                              int main()
   cout << "Gaziantep";                          University of Gaziantep              {
                                                                                        int a, b, total;
   cout << "University of\n ";                    University of                         cout << "Enter two integers: ";
   cout << "Gaziantep";                           Gaziantep                             cin >> a >> b;
                                                                                        total = a + b;
                                                                                        cout << "The sum is " << total << endl;
   cout << "University of " << endl;              University of
                                                                                        return 0;
   cout << "Gaziantep“;                           Gaziantep

                                                                        Sayfa 33                                                                 Sayfa 34

 Standard Input and Output (I/O)
 If you remove the line “using namespace std;” then                                TO DO BEFORE THE NEXT LECTURE
 the source code on the previous pages needs modifying
                                                                                   1. Browse through the course website
 as follows:                                                             

       // Calculates the sum of two integers                                       2. Do the homework:
       #include <iostream>                                               

       int main()                                                                  3. Prepare for lab
         int a, b, total;
         std::cout << "Enter two integers: ";
         std::cin >> a >> b;
                                                                                   4. Print next lecture’s notes:
         total = a + b;                                                  
         std::cout << "The sum is " << total << std::endl;               
         return 0;

                                                                        Sayfa 35                                                                 Sayfa 36


Shared By:
wang nianwu wang nianwu http://
About wangnianwu