Download C Programming Fundamentals: Introduction to Variables, Data Types, and Input/Output and more Slides Programming for Engineers in PDF only on Docsity!
Programming Fundamentals for Engineers
1. Introduction and C
Fundamentals
Muntaser Abulafi
Dr. Labib Arafeh
Yacoub Sabatin
- Pseudo-code:
- An outline of a program or algorithm, written in a form that can easily be converted into real programming statements;
- E.g. the pseudo-code for a bubble sort routine might be written: while not at end of list compare adjacent elements if second is greater than first switch them get next two elements if elements were switched repeat for entire list
- Code the program -- write the program;
- Test the program --run it;
- Debug the program (fix errors if any);
- Document completed program (final pseudo-code, flowchart, print chart, etc.). 2. Flow Charts:
- A flow chart is defined as a pictorial representation describing a procedure;
- Provides people (Developers, programmers , etc.) with a common language or symbolic reference point; 4 (Use: MS Visio)
5
3. An algorithm is a formula or set of steps for solving a particular problem;
- A clear set of rules; and
- Have a clear stopping point.
- Algorithms can be expressed in any language, from natural languages like English or French or Hindi to programming languages like QBASIC, C, etc. 4. Example : Add Two numbers, display both numbers and the sum: **1. START
- Input number1, number
- Sum = number1 + number
- Display heading message āNumber1, Number2, Sumā
- Display number1, number2, Sum
- End**
5. Pseudo-code: An outline of a program or algorithm, written in a form that can easily be converted into real programming statements; 1. Example : The pseudo-code to add two numbers might be written: Start Input number1, number Sum = number1 + number Display heading message āNumber1, Number2, Sumā Display number1, number2, Sum End 2. Can not be compiled nor executed, and there are no real formatting or syntax rules. 6. Caution: Never write your program before either Algorithming, flowcharting or pseudo-coding it. 7
Develop a C program to Compute the Volume and Dimensional weight of a box?
1. Analysis:
1) Outputs: Box_Volume, Box_D_Weight;
2)Inputs:
Height, length, width;
3) Processing:
1) Box_Volume = height * length * width;
2) Box_D_Weight = (Box_volume + 165)/166;
/* A C program to compute the Volume & Dimensional weight Dimensional weight of a box */ _/ STEP 1: START PART /_
#include <stdio.h>
main()
int height, length, width, Box_Volume,
Box_D_Weight; /declaring variables/
_/ STEP 2: Reading Data/_**
printf("Enter height of box: ");
scanf("%d", &height);
printf("Enter length of box: ");
scanf("%d", &length);
10
printf("Enter width of box: ");
scanf("%d", &width);
*/ * STEP 3: Computation */*
Box_Volume = height * length * width;
Box_D_Weight = (Box_Volume + 165) / 166;
_/ step 4 OUTPUT/_**
printf ("Dimensions: %d x %d x %d \n", length,
width, height);
printf ("Volume (cubic inches): %d \n",
Box_Volume); /* printing expression) */
printf ("Dimensional weight (pounds): %d \n",
Box_D_Weight);
_/ STEP 5 END/_**
return 0;
Benefits of learning C
- (^) You will be able to read and write code for a large number of platforms -- everything from microcontrollers to the most advanced scientific systems can be written in C, and many modern operating systems are written in C.
- (^) The jump to the object oriented C++ language becomes much easier. C++ is an extension of C, and it is nearly impossible to learn C++ without learning C first.
C History
- (^) Developed between 1969 and 1973 along with Unix
- (^) Due mostly to Dennis Ritchie
- (^) Designed for systems programming
- (^) Operating systems
- (^) Utility programs
- (^) Compilers
- (^) Filters
āHello Worldā in C #include <stdio.h> main() { printf(āHello, world!\nā); return 0; } #include ļØ C preprocessor command that lets user includes the code from another file (such as a library) stdio.h ļØ standard input/output library #include <stdio.h> ļØ allows you to use code from the standard input/output library that contains printf function
- (^) main ļØ Every program must have a function named main which contains statements enclosed in braces { and }
- (^) printf ļØ A function from the standard I/O library that prints a string (f stands for formatted)
- (^) \n ļØ A new line character. The \n inside the string indicates a new line
- (^) Every function must return a value. Main is a function and returns a value taken by the operating system as the program terminates
- (^) return 0 ļØ Used to signal OS that the program terminated normally
General C program! # directives main() { statements }
- (^) Statements ļØ Commands that are carried out as program runs; - (^) printf is a call statement displaying string on screen : - (^) _printf (āhelloā); / These are comments /_ - (^) _printf (āworld! \nā); / Can go any where in a C /_
- Statements end with a semicolon ( ; )
Some definitions
āHello, World!\nā
- (^) Character ļØ A single letter, number, punctuation mark, etc.
- (^) String ļØ Series of characters grouped together;
- (^) Some special kinds of characters exist:
- (^) \t ā tab character;
- (^) \n ā new line character.