- 86 Views
- Uploaded on
- Presentation posted in: General

Programming Training

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Programming Training

Main Points:

- C Statements

- Problems with selections.

- Problems with repetitions.

printf(“format”, var)

EACH VARIABLE REQUIRES A SPECIFICATOR

printf(“text %sp \n”, var1);

printf(“text %s1 text %s2 text …\n”, var1,var2,…);

- format specificators: %d, %ld, %f, %lf.

- \n special character for newline.

Remark: Use \n to jump to a new line.

scanf(“format”,&var)

EACH VARIABLE REQUIRES A SPECIFICATOR

EACH VARIABLE IS PREFIXED BY &

scanf(“%sp”,&var); No text or space is allowed

Remark: READ ONLY ONE VARIABLE.

Read a variable var:

printf(”var="); scanf("%sp", &var);

Write a variable var:

printf(”var = %sp \n”,var);

Write a text:

printf(”My Text \n”);

Write a line of start:

printf(”\n\n\n******** \n\n\n”);

# include <stdio.h>

# include <math.h>

# include <conio.h>

int main(int args, char ** argc)

{

double a, b, max;

printf(”\n***************\n”);

printf("a="); scanf("%lf", &a);

printf("b="); scanf("%lf", &b);

sum = a+b;

printf(”sum=%lf \n\n", sum);

printf(”\n***************\n”);

return 1;

}

Aritmetics operators:

- +, -, *, /
- ++, -- for incrementing and decrementing.
- % is the remainder of the integer division.
Relational operators:

- <, >, <=, >=, ==, !=
Logical: &&=and, ||= or, !=not

In addition of those operators there are several math routines in math.h

sqrt,

sin, cos,

asin, acos etc.

- Find the inputs and outputs of the problem.
- Ask yourself if you have done something similar.
- Identify the elements of the solution.
- Take a numerical example and see the changes of the variables.
- Write the code.

Inputs are the elements / variables to start from.

- Inputs must be read.

- Input can be arguments within a function.

Output(s) is represented by the problem result.

- Outputs must be printed.

- Outputs can be returned by a function.

- Ask yourself if you have done something similar.
- Solve the problem mathematically.

- Identify the elements of the solution:
- Break the problem into small blocks that you know how to.
- Write functions / find snippets of code for each block.
- Find the calculations / selections / repetitions

- Write a pseudo-code in plain English.

#include<stdio.h>

#include<conio.h>

#include<math.h>

int main (int args,char ** argc){

double a, b, c, A, B, C, per, ar, r, R;

printf("***************************\n\n");

printf("a=");scanf("%lf",&a);

printf("b=");scanf("%lf",&b);

printf("c=");scanf("%lf",&c);

A=acos((b*b+c*c-a*a)/(2*b*c))*180/3.141592654;

B=acos((a*a+c*c-b*b)/(2*a*c))*180/3.141592654;

C=acos((a*a+b*b-c*c)/(2*a*b))*180/3.141592654

printf("\nThe angle 'A' is = %lf degres \n", A);

printf("The angle 'B' is = %lf degres\n", B);

printf("The angle 'C' is = %lf degres\n",C);

printf(”\n\n****************************\n\n");

return1;

}

C statements give the flow of computation within C programs.

- Simple statements:
- expression; - to evaluate a C expression.
- block {} - used to collect several statements in if, for, while.
- Jump:
- break - jumps outside of repetitions.
- goto - jumps to a label.
- return - gets outside of functions/routines

- Selections
- simple if - selects an alternative
- complete if - selects from two alternatives
- switch - selects from multiple alternatives.

If statement to select one choice

if(test)

{

statement1;

statement2;

…

}

If statement to choose between two alternatives

if(test)

{

statement1;

statement2;

…

}

else

{

statement 1;

statement 2;

…

}

Example: Find the maximum/minimum value of a,b

if(a<b)

{

max=b; min=a;

}

else

{

max=a; min=b;

}

Given for integer numbers a, b, c, d then write a program to calculate the maximum value of them.

Inputs: a, b, c, d – int

Output: max – int.

How to do it?

read a, b, c, d;

find max1 as the maximum of a and b

find max 2 as the maximum of c and d

find max as the maximum of max1 and max2

write mac

// find max between the two max-s

if( max1>max2 )

{

max=max1;

}

else

{

max=max2;

}

// print max

printf("\n The maximun 'max'=%d \n",max);

return1;

}

#include<stdio.h>

#include<conio.h>

int main (int args,char ** argc){

int a,b,c,d,max1,max2,max,min,min1,min2;

//reading the variables

printf("a=");scanf("%d",&a);

printf("b=");scanf("%d",&b);

printf("c=");scanf("%d",&c);

printf("d=");scanf("%d",&d);

// find max between a and b

if( b>a )

{

max1=b;

}

else

{

max1=a;

}

// find max between c and d

if( d>c )

{

max2=d;

}

else

{

max2=c;

}

#include<stdio.h>

#include<conio.h>

int main (int args,char ** argc){

double a,b,c,;

int ans=1;

printf("a=");scanf("%lf",&a);

printf("b=");scanf("%lf",&b);

printf("c=");scanf("%lf",&c);

// test negative sides

if( (a<=0) || (b<=0) || (c<=0))

{

ans=0;

}

//test triangle inequality

if( (a>=b+c) || (b>=c+a) || (c>=a+b))

{

ans=0;

}

if(ans==1)printf("\n The numbers form a triangle");

else printf("\n The numbers do NOT form a triangle");

return1;

}

- Read more about if from the e-tutorial.
- Solve the HW problems.