C Programming

These notes are in addition to the text book used in the class of BAA2013 Computer Programming in C++.

Thursday, May 25, 2006

Project 1 and Project 2 Due Dates

Project 1 : Simply supported beam
Deadline: Tuesday 30th May, 4.00pm at DK 4

Project 2 :
Deadline : Tuesday 13th June 2006.,
4.00 pm

Classes on 29th-31st May 2006

Next week all classes will be combined for both sections : AC and AA

Monday 9.00am - 1.00pm at DK 3
Lessons 13 and 14

Tuesday 10.00am - 12.01 pm at DK3
Test 2, please bring along your laptops / notebooks

2.30pm - 4.00pm at DK4
Submission of Project 1

Wednesday
9.00am - 12.00pm DK 3
2.15pm - 4.15pm DK4
Project submission (and interview for group projects)

Tuesday, May 23, 2006

Function Exercise 1

Function celcius to convert the temprature value from fahrenheit to celcius. This function is to receive an input and return a value.

Function Exercise 2

Function to calculate the area of a circle. This function will receive a value but return no value. The function will print the result of the calculation in the body of the function.

Function Exercise 3



Problem : Simply supported beam.
One point load.
Write the respective functions for the simply supported beam as shown in the diagram

1. Function to calculate the reactions, prints the reactions of both supports and returns the value of R of left support
2. Function to calculate and print the SFs (4)
3. Function to calculate and print the BM at point load position and mid-span

Monday, May 22, 2006

Test 1

Test 1 will be held on Thursday 25th May 2006
at 2.15pm

Topics will be from Lesson 1 to Lesson 8

Flow chart for Nested Loop


Write a program that uses nested loops to produce the following output:

A1B1C1C2C3A1B2C1C2C3A1B3C1C2C3
A2B1C1C2C3A2B2C1C2C3A2B3C1C2C3
A3B1C1C2C3A3B2C1C2C3A3B3C1C2C3

Sunday, May 21, 2006

max and minimum number

Store the max and minimum numbers in their respective declared variable

void main()
{
int Num;
int Max=0;
int Min=1000;
//

cin>>Num;
if (Num > Max)
Max=Num;
if (Num < Min)
Min=Num;
//
}

Saturday, May 20, 2006

Project 1 : Simply Supported Beam

This is a series of questions / mini projects given to you after every lesson starting from Lesson 5.

For each set of questions you will need to:-
1. submit a report which has to consist of the following four (4) elements:
a. defining diagram of the problem
b. flow-chart or algorithmn
c. Pseudo Code
d. source code

2. all mini projects will be evaluated to be in running properly during hands-on session class in the following respective lesson, (for example project given in Lesson 6 will be evaluated during the next class, ie Lesson 7).

If you have any questions, please post in the "comments" of this section.

Wednesday, May 10, 2006

output special characters and format

Special characters



Character sequence
Result

\n
Moves cursor to next line (same effect as endl

\t
Generates a tab character to move the cursor to the next tab stop

\\
Prints a backslash (\)

\’
Prints a single quotation mark (‘)

\”
Prints a double quotation mark (“)







Formatting options


Option
Description

left
Left justifies the output

right
Right justifies the output

showpoint
Displays decimal point and trailing zeros for all floating point numbers, even if the decimal places are not needed

uppercase
Desplays the “e” in E-notation as “E” rather than “e”

showpos
Desplays a leading plus sign before positive values

scientific
Displays floating point numbers in scientific (“E”) notation

fixed
Displays floating point numbers in normal notation

Lesson 5 input and output


Example of using special characters


// specchar.cpp
// example of new line and special characters
#include
int main()
{
j=25;
cout<< j<< ‘\n’; //single quotes because it is a single character
cout<< “String\n”; //double quotes because it is part of a string
cout << “The numbers on the following line are separated by tabs.\n”;
cout << “1 \t 2 \t 3 \t 4 \n”;
// the following lines use endl
cout << “in C++, you output backslashes (\\)” << endl;
cout << “you can also print single quotes (\’) and “ << endl;
cout << “double quotes (\”).” << endl;
return 0;
}

Example of formatted output

// coutsetf.cpp
#include
int main()
{
float x = 24.0;
cout << x << endl; //displays 24
cout.setf(ios::showpoint);
cout << x << endl; // displays 24.0000
cout.setf(ios::showpos);
cout << x << endl; // displays +24.00
cout.setf(ios::scientific);
cout << x << endl; // displays +2.400000e+001
return 0;
}

Tuesday, May 09, 2006

Lecture 4

Program Data

PROGRAM DATA

Variables, constants and literals
A variable is the name given to a collection of memory cells, designed to store a particular data item. It is called a variable because the value stored in those memory cells may change or vary as the program executes. For example, the variable total_amount may contain several values during the execution of the program.

A constant is a data item with a name and a value that remain the same during the execution of the program. For example, the name 'fifty' may be given to a data item that contains the value 50.

A literal is a constant whose name is the written representation of its value. For example, the program may contain the literal '50'.

Data Types
At the beginning of a program, the programmer must clearly define the form or type of the data to be collected. The data types can be elementary data items or data structures.

Elementary data items
An elementary data item is one containing a single variable that is always treated as a unit. These data items are usually classified into data types. A data type consists of a set of data values and a set of operations that can be perfiormed on these values. The most common elementary data types are:

  • integer :
    representating a set of whole numbers, positive, negative or zero.
    eg. 4, 123, -56
  • real :
    representing a set of numbers, positive or negative, which may include values before or after a decimal point. These are sometimes referred to as floating point numbers.
    eg. 12..5 , 2.3E+04 , -98
  • character :
    representing the set of characters on the keyboard, plus some special characters
    eg. 'A' , 'c' , '$'
  • Boolean:
    representing a control flag or switch, which may contain one of only two possible values ; true or false.

Data Structures
A data structure is an aggregate of other data items. The data items that it contains are its components, which may be elementary data items or another data structure. In a data structure, data is grouped together in a particular way, which reflects the situation with which the program is concerned. The most common data structures are:

  • record:
    a collection of data items or fields that all bear some relationship to one another. For example, a student record may contain the student's number, name, address and enrolled subjects.
  • file:
    a collection of records. For example, a student file may contain a collection of the above student records.
  • array:
    a data structure that is made uo of a number of variabkes r data itenms that all have the same data tyoe and are accessed by the same nane. For example, an array called 'scores' may contain a collection of students' exam scores. Access to the individual items in the array is made by the use of an index or subscript beside the name of the array - for example, scores (3).
  • string:
    a collection of characters that can be fixed or variable. For example, the string 'Jenny Parker' may repersent a student's name.

Files
A popular method of storing information is to enter and store data on a file. The major advantages of using files are:

  • several different programs can access the same data
  • the data can be entered and reused several times
  • the data can be easily updated and maintained
  • the accurary of the data is easier to enforce.

There are two different methods of storing data on files:

  • sequential or text files, where data is stored and retrieved sequentially
  • direct or random-access files, where data is stored and retrieved randomly, by a key or index.

Sequential files may be opened to read or to write, but not both operations on the same file.
Random-access files can be opened to read and write on the same file.


Tuesday, May 02, 2006

Class Schedule

14 Days of Lectures and Labs of 4hrs each day.

proposed Dates:
2nd May
9th -11th May
16th -18th May
22nd - 25th May
29th May - 1st June

Monday, May 01, 2006

Introduction to Computers

Revison on Computer Appreciation.

Computer Literacy

•Have basic understanding of what computer can/cannot do
•Can use computers for personal or professional usage
•Can interact with computers and use software such as word processor, database, spreadsheet and others
•Have some ideas about social implications by current and future computer technology


Computer Areas

•General business
•Bank
•Education
•Government
•Military
•Medical
•Games

What is a Computer?

•A machine that can be programmed to accept data, process it into useful information, and store it away
–Data : raw facts representing people and events
–Information : data that is organized, meaningful, and useful

Computer System
  • Hardware
  • Software
  • Human
Computing Environment

•Personal Computer (PC) environment
•Time Sharing environment
•Client/Server environment
•Distributed Computing

Why Programming?

•A program (software) is a set of detailed, step-by-step instructions that directs the computer to do what you want it to do.
•Programs are written in a programming language – a set of rules that provides a way of telling the computer what operations to perform

Computer Languages

•Machine Language
•Assembly Language
•High-level Language
•Very High-Level Language
•Natural Language