DIT 0109: Introduction to Computer Programming

Prerequisite: None

Course learning outcomes

At the end of the course students should be able to: -

  1. Define techniques of structured programming
  2. Describe the concepts of constants, variables, data types and operators
  3. Develop programs using input and output operations
  4. Create different looping and branching statements

Course content

Introduction to computer: input, output, processing, storage; problem solving: algorithms, flowchart, pseudo code; programming languages: machine language, high level languages, interpreters, compilers (edit/compile/debug process); introduction to clusplus; program structure: statements, simple data types; string data types, variables, arithmetic , logic, assignment, compound operators, expressions; control structures: decisions (if, if…else, switch), loops (while, do…while, for); functions: functions/subroutine/procedure, string functions, string manipulation; data structures: array, record, pointers; programming styles: structured and object oriented programming. 

Required readings

1. Dale, N. B., & Weems, C. (2005). Programming and problem solving with C++. Jones & Bartlett Learning.

2. Zak, D. (2012). Introduction to Programming with C++. Cengage Learning.

Recommended readings:

1. Sprankle, M.  and Hubbard, J. (2011). Problem solving and programming concepts. Prentice Hall, ninth Edition.

2. King, K. N. (2010). C programming: a modern approach. Granite Hill Publishers.

3. Savitch, W. (2008). Problem solving with C++. Addison-Wesley Publishing Company.

How to install Code Blocks 

 

 Lecture notes


Introduction to computer: input, output, processing, storage;

Computer is an electronic device that accept data as input, processing data into information, output information and stored information for future use. Data can be considered as a what people get interested about things/objects surrounding them. Data is what we know about things surrounding us. For example, the height of a tree is five meters high, the name of that person is Shaban, The team that won the last match was Yanga etc.  the terms five, Shaban and Yanga in this context are data. Data is normally represented using symbols and signs as agreed among people of a certain society. for example the number of years of an individual can be represented using the symbols 27. When computer process data means that it operate using the operators such as addition (+), subtraction (-), multiplication (*), division (/), equate (=), less than (<), greater than (>) and logic operators such as and, or and not. Computer accept data using keyboard, mouse, mic etc. Computer output information through monitor, printer, speaker etc. Data is also stored permanently using devices such as hard disk, CD, DVC, floppy disk, flash disk and memory card.

Variable

In computer programming, a variable is a storage location paired with an associated symbolic name (an identifier), which contains some known or unknown quantity of information referred to as a value.

A variable is like a container that is used to keep valuable things. Normally, the containers are given names in order to refer to them. The name to identify the containers are used such as the first number, the blue glass, the last room, the seat x, etc. When we want to get access to the things (value) stored in these containers we normally use container identifiers. One can say give me the milk stored in the blue glass, taken the milk in the blue glass and pour it into the the red bowl. These are the operations that are normally done to put things on the containers, take things away from the container, mix things from two or more containers and keep the mixture in another container. The same style is used in computer data storage and processing. The values (data) are stored in the containers (variables). 

Problem solving: algorithms, flowchart, pseudo code;

Problem solving consists of using generic or ad hoc methods, in an orderly manner, for finding solutions to problems. Also
Problem-solving is a mental process that involves discovering and analyzing a potential problem and then coming up with the best possible solution. A computer program is generally designed to solve a problem. A problem exists when what you have (DATA) is not the same as what you want (INFORMATION).

Problem Solving is the ability to understand what you have, what you want, and creating a set of instructions to change what you have into what you want. Good Problem Solving Skills are based on knowledge, experience and logic

The programmers (the one who create programs) when developing solution to the problems have to be aware that:

  1. A computer needs very simple step-by-step instructions to function
  2. A person can usually solve problems in quick and complex ways
  3. A programmer creates solutions to problems
  4. A programmer then breaks the solution down into its simplest steps
  5. These steps are then translated into computer code
  6. Creating solutions to problems, and then creating simple step-by-step instructions for a computer based on the solutions is the job of a Computer Programmer

The solution is translated into computer code using a text editor and a specific Computer Language. The computer code (known as “Source Code”) is Compiled. This creates a machine language file with instructions the computer can understand (called an “Object File”). The Object File is then Linked and an Executable File is created, which can be run by the computer

An algorithm is a procedure or formula for solving a problem. The standard ways of representing the algorithm is by using pseudocode or flowchart. A Flowchart is a Visual Representation of an algorithm. A Flowchart uses easy-to-understand symbols to represent actions on data and the flow of data. Flowcharts aid in breaking down a problem into simple steps.

The Basic Flowchart Symbols

  1. The terminator shows where a specific piece of an algorithm beings and ends
  2. A process is a basic action or actions on data
  3. A decision is a logical test with two possible data paths resulting from one
  4. Input/Output gets data or displays information to the user of the algorithm
  5. A connector connects two or more data flows into one
  6. A data flow shows the direction of data’s movement through the algorithm

“Pseudo” means “pretend” or “false”. Pseudo Code is pretend or false computer code; generic English-like terms that are
somewhat like computer cod. Pseudo Code is not as standardized as flowcharts, and does not facilitate the breaking down of problems as well as a flowchart does.

Any problem, regardless of how complex, can be broken down into three basic CONSTRUCTS

  1. SEQUENCE - performing one step after another
  2. SELECTION – decision-making construct (if something is true take this action, otherwise take that action)
  3. ITERATION – looping construct (while something is true, do this, otherwise stop)

 Drills

1. Write the algorithm to add two numbers
2. Write the algorithm that reads two numbers and displays the numbers read in decreasing order

Programming languages:

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs to control the behavior of a machine or to express algorithms.

Machine language [sometimes referred to as machine code or object code, machine language] is a collection of binary digits or bits that the computer reads and interprets. Machine language is the only language a computer is capable of understanding.

0000 1001 1100 0110 1010 1111 0101 1000
1010 1111 0101 1000 0000 1001 1100 0110
1100 0110 1010 1111 0101 1000 0000 1001
0101 1000 0000 1001 1100 0110 1010 1111

Symbolic Languages. E.g. Assembly language

Assembly language is a low-level programming language used to interface with computer hardware. Assembly language uses structured commands as substitutions for numbers allowing humans to more easily read the code versus looking at binary. Although easier to read than binary, assembly language is a difficult language and is usually substituted for a higher language such as C. Below is an example of assembly language in a debug routine.

GCD:        TST        B
               BEQ        SIMPLE
               MOV        A, R5   
               SXT        R4   
               DIV        B, R4
               MOV        B, A
               MOV     R5, B
               CALL    GCD
SIMPLE:    RETURN

High Level Languages

A programming language such as C, FORTRAN, or Pascal that enables a programmer to write programs that are more or less independent of a particular type of computer. Such languages are considered high-level because they are closer to human languages and further from machine languages

Compiled vs. Interpreted Languages

Programming languages generally fall into one of two categories: Compiled or Interpreted. With a compiled language, code you enter is reduced to a set of machine-specific instructions before being saved as an executable file. With interpreted languages, the code is saved in the same format that you entered. Compiled programs generally run faster than interpreted ones because interpreted programs must be reduced to machine instructions at runtime. However, with an interpreted language you can do things that cannot be done in a compiled language. For example, interpreted programs can modify themselves by adding or changing functions at runtime. It is also usually easier to develop applications in an interpreted environment because you don't have to recompile your application each time you want to test a small section

 Structured programming:

Structured programming (sometimes known as modular programming) is a subset of procedural programming that enforces a logical structure on the program being written to make it more efficient and easier to understand and modify. Structured programming frequently employs a top-down design model, in which developers map out the overall program structure into separate subsections. Program flow follows a simple hierarchical model that employs looping constructs such as "for," "repeat," and "while."

Structured programming can also be defined as a technique for organizing and coding computer programs in which a hierarchy of modules is used, each having a single entry and a single exit point, and in which control is passed downward through the structure without unconditional branches to higher levels of the structure. Three types of control flow are used: sequential, test (decision or selection), and iteration.

Assignments


 

 

Sample questions and past-papers


Computer is an electronic device that can accept data (input), process data (using operators such as addition, multiplication, division, subtraction, equality, less than, greater than etc) into information, print out (output) information and store information for future use. 

          1. What do you understand by the following concepts about computer programming?
          2. Data
          3. Information
          4. Algorithm
          5. Flowchart
          6. Machine language
          7. Assembly language
          8. Array
          9. Function
          10. Struct
          11. Program
          12. Programmer
          13. Programming
          14. Class in Object oriented programming
          15. Object in object oriented programming
          16. Encapsulation
          17. Inheritance
          18. Polymorphism
        1. Describe any three programming constructs you know
        2. Int, double, bool and char are collectively know as________________.
        3. For statement, while statement and do...while statement are collectively known as _________.
        4. If statements and switch statement are collectively known as __________.
        5. State if the following statements represents truth about computer programming in C++!
          1. In C++, the statement cin>>"myage"; is used to input age data from the user through computer keyboard______.
          2. All statements in C++ program ends with a semi-colon____.
          3. While loop is similar to for loop as in both cases the number of iteration is known in advance______.
          4. Initialization statements combine both assignment statement and declaration statements_______.
          5. In C++ equal sign (=) is always used to compare data values_____.
          6. Juma was asked to write a program that will find the average marks of 153 students of DIT year 1 for a course DIT0109. Juma was right by using switch statement control structure for developing the program_______.
          7. The statement x = x + 10 can also be rewritten as x += 10_______.
          8. When a function if defined as a void function then it is said to return integer value_______.
        6. What is the difference between for loop statement and while loop statement?
        7. What is the similarity of for loop statement and while loop statement
        8. Mention four rules for writing a variable name (identifier) in C++
        9. What is the similarity of array and structure in C++?
        10. What will be the output?#include <iostream>
          using namespace std;
          int main(){
                int a = 0;
                int b = 0;
                for (int m = 0; m < 5; m++){
                       a +=  10;   //the same as a = a + 10
                       b +=  5;     //the same as a = a + 5
                }
                cout<<"At the end of the loop a = "<<a<<", b = "<<b;
                return 0;
          }
        11. What will be the output?#include <iostream>
          using namespace std;
          int main()
          {
               int mylist[9]={4,2,7,9,5,1,12,99,70};
               int pos;
               for (int m = 0; m< 9; m++)
               {
                       if(mylist[m] == 7)
                       {
                             pos = m+1;
                              break;
                       }
               }
              cout<<"The number 7 is found at "<<pos<<endl;
               return 0;
          }
        12. In terms of programming what do you understand by the saying that “computer process data into information”. Provide an example of data and information and what happens when one transform data into information
        13. Write an algorithm that input two numbers from user keyboard and then output to the screen the same numbers by in decreasing order (starting with large and end with small number)
        14. Write down C++ program that will be able to output the message “Hello World” one hundred times each on its own line
        15. With one typical example each, describe the three programming construct
        16.  Draw an algorithm using flowchart that shows how a person draws money from ATM machine. You should consider all possible cases that one can encounter such as when one has entered wrong PIN, when there is no money enough money in the account, etc. 
        17. List down two variables that can be designed when writing a program for converting temperature values from Centigrade units to Fahrenheit units
        18. Using C++ programming language, write a program that display the word “Hello” a hundred times
        19. Using C++ programming language, write a program what will count even numbers that are there between 100 and 100,000
        20. Write declaration of all possible variables that could have been in each of the following problems.
          1. Convert the temperature from Celsius degree to Fahrenheit degree
          2. Read and output marks of three hundred students
          3. Calculate the area of a triangle
        21. Kendra has implemented a While loop within her C++ program. Assess the code statement below and select which answer best summarizes the output Kendra will experience once the while statement is executed.

           
          int count = 1;
          while (count <=25) { cout<<count; count = count –1; } cout<<"Done";

 

        1. The while loop will execute 25 times and print the numbers 1 thru 25 and finish with the printing of Done.
        2. The while loop will execute 25 times and print the numbers 25 down to 1 and finish with the printing of Done
        3. The while statement will not function correctly due to a missing semicolon(;) after the statement while (count <=25)
        4. The while statement will execute by counting down from 1 until infinity and result in an infinite loop