arduino training - day 1.ppt

Post on 01-Dec-2015

61 Views

Category:

Documents

8 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Fundamentals of C-Programming Introduction to C Programming Integrated Development Environment

(IDE) Structure of C Programming Elements of Style Variables in C Control Structure Loop Structure

Computer Programming -- is often called coding. It is the process of writing, testing, debugging and maintaining the source code of computer programs.

Computer Program – is known as software program. It is a structured collection of instructions to the computer.

Computer Programmer – is a professional who writes programs.

It is a software application that provides comprehensive facilities to computer programmers for software development.

An IDE normally consists of a source code editor, a compiler and/or interpreter, build automation tools, and usually a debugger.

This window contains the interface 'Projects' which will in the following text be referred to as the project view.

This view show all the projects opened in CodeBlocks at a certain time.

The 'Symbols' tab of the Management window shows symbols and variables.

A part of the environment where we can write the codes

It is a computer program that translates text written in a computer language (the source language) into another computer language (the target language)

It is a computer program that is used to test and debug other programs

Preprocessor directive main(){VariablesStatements

}

Head

Body

Preprocessor CommandsFunctionsVariablesStatements & ExpressionsComments

These commands tells the compiler to do preprocessing before doing actual compilation.

#include <stdio.h>int main(){

double C, F;/*read in value for C*/printf(“Enter Celsius temp: “);scanf(“%lf”,&C);

/* Calculate F */ F = (9/5) * C + 32; /* Display the value of F */ printf(“The equivalent Fahrenheit is %lf \n“, F);

return 0;}

are main building blocks of any C Program

#include <stdio.h>#include <stdio.h>int main()int main(){ {

double C, F;double C, F;/*read in value for C*//*read in value for C*/printf(“Enter Celsius temp: “);printf(“Enter Celsius temp: “);scanf(“%lf”,&C);scanf(“%lf”,&C);

/* Calculate F */ /* Calculate F */ F = (9/5) * C + 32;F = (9/5) * C + 32; /* Display the value of F */ /* Display the value of F */ printf(“The equivalent Fahrenheit is %lf \n“, F);printf(“The equivalent Fahrenheit is %lf \n“, F);

return 0;return 0;}}

are used to hold numbers, strings and complex data for manipulation

#include <stdio.h>int main(){

double C, F;/*read in value for C*/printf(“Enter Celsius temp: “);scanf(“%lf”,&C);

/* Calculate F */ F = (9/5) * C + 32; /* Display the value of F */ printf(“The equivalent Fahrenheit is %lf \n“, F);

return 0;}

Expressions combine variables and constants to create new values.

Statements are expressions, assignments, function calls, or control flow statements which make up C programs.

#include <stdio.h>int main(){

double C, F;/*read in value for C*/printf(“Enter Celsius temp: “);scanf(“%lf”,&C);

/* Calculate F */ F = (9/5) * C + 32; /* Display the value of F */ printf(“The equivalent Fahrenheit is %lf \n“, F);

return 0;}

Doing it the right way...

Write one statement per line. Put spaces before and after each arithmetic operator,

just like you put spaces between words when you write.

/* Poor programming practice */

biggest=-l;first=0;count=57;init_key_words();

If (debug)open_log_files();table_size=parse_size+lexsize;

/* Better programming practice*/

biggest=-l;

first=0;

count=57;

init_key_words();

if(debug)

open_log_files();

table_size=parse_size + lex_size;

Change a long, complex statement into several smaller, simpler statements.

/* This is a big mess */gain = (old_value - new_value) /(total_old - total_new) * 100.0;

/* Good practice */delta_value = (old_value - new_value);delta_total = (total_old - total_new);gain = delta_value / delta_total * 100.0;

Declaration and Data Types

Naming VariablesDeclaring VariablesUsing VariablesThe Assignment Statement

Variables – use represent some unknown value

Example : Perimeter = 2L + 2W;

Variables in programming can be represented containing multiple charactersExample: full_name = first_name + last_name;

Variable names in C◦May only consist of letters, digits, and underscores

◦May be as long as you like, but only the first 31 characters are significant

◦May not begin with a number◦May not be a C reserved word (keyword)

auto break case char const continue default do double else enum extern float for goto if

int longregister returnshort signedsizeof staticstruct switchtypedef unionunsigned voidvolatile while

Begin variable names with lowercase letters Use meaningful identifiers Separate “words” within identifiers with

underscores or mixed upper and lower case. ◦ Examples: surfaceArea surface_Area

surface_area Be consistent!

Use all uppercase for symbolic constants (used in #define preprocessor directives).

Examples:

#define PI 3.14159 #define AGE 52

C is case sensitive◦Example:

areaAreaAREAArEa

All are seen as different variables by the compiler.

Syntax : <data_type> variable_name;

Example:int num_of_meatballs;

float area;

Integers (whole numbers)◦int, long int, short int, unsigned int

Floating point (real numbers)◦float, double

Characters◦char

Data Type

Number of Bytes

Minimum Value Maximum Value

char 1 -128 127

int 4 -2147483648 2147483647

float 4 1.17549435e-38 3.40282347e+38

double 8 2.225073858072014e-308

1.7976931348623157e+308

Format Description Example

%c Single character A

%i or %d Integer or decimal Number

1024

%f Floating point number or real

3.145

%lf Double Long range of floating value

%.2f Floating point number with 2 decimal places

3.10

Examples:

int length = 7 ;float diameter = 5.9 ;char initial = ‘A’ ;double cash = 1058635.5285

Good Programming Practice◦a comment is always a good idea◦Example:

int height = 4; // rectangle height

int width = 6 ; // rectangle width

int area ; // rectangle area

Place each variable declaration on its own line with a descriptive comment.

Example : float inches ; // number of inches deep

Place a comment before each logical “chunk” of code describing what it does.

Example :/* Get the depth in fathoms from the user*/printf(“Enter the depth in fathoms: ”);

Do not place a comment on the same line as code (with the exception of variable declarations).

Use blank lines to enhance readability.

Use spaces around all arithmetic and assignment operators.◦Examples:feet = 6 * fathoms ;a = b + c;

#include <stdio.h>int main( ){ float inches ; // number of inches deep float feet ; // number of feet deep float fathoms ; // number of fathoms deep

// Get the depth in fathoms from the user

printf (“Enter the depth in fathoms : ”) ; scanf (“%f”, &fathoms) ;

// Convert the depth to inches

feet = 6 * fathoms ; inches = 12 * feet ;

.

.

.return 0;}

Assignment statement is represented by an equal sign (=)

Examples:diameter = 5.9 ;area = length * width ;

#include <stdio.h> int main( ) { int inches, feet, fathoms ;

fathoms = 7 ; feet = 6 * fathoms ; inches = 12 * feet ;

.

.

.

It is a statement for choosing two alternative action

Basic Syntax:

if (conditional expr)statement_1;

elsestatement_2;

Conditional Expression Execute S_1

Execute S_2

T

F

Write a program that reads in an integer and then display whether the number is positive or negative.

Write a program that reads weight in pounds and output the weight in kilograms. Moreover, if the weight in kilogram exceeds 60 kilos, then display the message “GO ON DIET”, else “YOU ARE IN GOOD SHAPE”

Used to check the value of a certain item that may have more than one outcome

Basic Syntax:

if (conditional expr)statement_1;

else if (conditional expr)statement_2;

else if (conditional expr)statement_3;

…else if (conditional expr)

statement_n;

Write a program that reads in an integer and then display whether the number is positive or negative or neither when the user enters 0.

Write a program that reads the student’s average grade in Programming and then output his grade standing according to the following schemeAVERAGE GRADE GRADE STANDING

Below 75 FAt least 75 but below 85 CAt least 85 but below 95 BAt least 95 A

Write a program to display a menu system for an elementary arithmetic operation and then ask the user to select the operation he or she wants

Elementary Arithmetic Operations1. Addition2. Subtraction3. Multiplication4. DivisionInput your choice (1-4):The user then asked to type two integers and the correct

answer. Your program should also inform the user whether the answer is correct or not.

Allows to execute a series of statement based on the value of an expression

Basic Syntax:

switch (expression) { case constant 1;

statement 1;statement 2;…statement n;break;

case constant 2;statement 1;statement 2;…statement n;break;

case constant n;statement 1;statement 2;…statement n;break;

default:statement;

}

Create a Number Base converter program that allows the user to select from the four options givenNUMBER BASE CONVERTER

1. Decimal to Hexadecimal2. Hexadecimal to Decimal3. Decimal to Octal4. Octal to Decimal

Input your choice (1-4):

Write a program that reads the student’s average grade in Programming and then output his grade standing according to the following schemeAVERAGE GRADE GRADE STANDING

Below 75 FAt least 75 but below 85 CAt least 85 but below 95 BAt least 95 A

It does not use a counter variable to loop a number of times

The general from of while

initialize loop counter;while (test loop counter using a condition){

statement_1;statement_2;increment loop counter;

}

Write a program that reads in an integer and then calculate the sum of the positive integers from 1 to N.

Write a program that reads in an integer and then count the number of positive and negative numbers

The conditional expression is placed at the end of the loop

The general from of do…while

do { statement_1; statement_2; … statement_2;

} while (conditional expression);

Write a program that output all even numbers between 1 to 15.

Write a program that reads in 10 positive integers and then calculate its sum.

It lets you execute a group of statements a certain number of times

The general from of for loop

for( initialization; test counter; increment counter)

{ statement_1; statement_2;… statement_n;

}

Create a program that displays a multiplication table of 5.

Sample OutputSample Output5*1=55*2=105*3=15…5*10=50

Modify the sample problem so that it will ask the user for the number to multiply and the number of terms.

top related