Presentation is loading. Please wait.

Presentation is loading. Please wait.

Further C Programming Variable types Loops Conditional statements

Similar presentations


Presentation on theme: "Further C Programming Variable types Loops Conditional statements"— Presentation transcript:

1 Further C Programming Variable types Loops Conditional statements
Local and Global variables Arrays Pointers

2 Variable Types Types char character
short short integer to 32767 int integer -/+2,147,483,647 long int long integer ~ -/+ 9E18 float floating point 1.2E-38 to 3.4E38 double double precision 2.2E-308 to 1.8E308

3 for loop Can set up loops to repeat sections of code
for (count = 1 ; count <=3 ; count++) { printf("%d \n", count); } count++ is short hand for count=count+1

4 While Loop Useful when we don’t know how many times the loop will repeat. Eg while (a>0) { fscanf(fin,"%d %d",&a,&b) ; c = a*b; printf("%d * %d = %d \n", a,b,c); } Use to read a file to the end. while(!feof(fin)){fscanf(fin,”%d %d”,&a,&b);}

5 Conditional Statements
Most common example is the if…else construction Eg if(mark >= 40) printf(“pass\n”); else printf(“fail\n”); Boolian operators: = = && || ! Equals And Or Not

6 Functions and Variables
Common mathematical functions are defined in the header file math.h # include <math.h> Often useful to define your own functions. Functions must be prototyped before the main() loop is reached.

7 /* and the sine of the product*/ # include <stdio.h>
/* Program to calculate the product of two numbers */ /* and the sine of the product*/ # include <stdio.h> # include <math.h> int product(int, int); int main(void) { int a,b,c; printf ("Enter a number between 1 and 100: "); scanf ("%d", &a); scanf ("%d", &b); c = product(a,b); printf("%d times %d = %d \n", a, b, c); printf("sin(%d times %d)= %f\n",a,b,sin(c)); return(0); } int product(int x, int y) int z; z=x*y; return(z);

8 Variables declared within a function are local to that function.
i.e. their values are only know within the function. Changes made to the variables are not known outside the function. Variables declared above the main() loop are global variables. i.e. their values are known anywhere in the program. Changes can be made anywhere in the program.

9 /* and the sine of the product*/ # include <stdio.h>
/* Program to calculate the product of two numbers */ /* and the sine of the product*/ # include <stdio.h> # include <math.h> int product(void); int a,b; int main(void) { int c; printf ("Enter a number between 1 and 100: "); scanf ("%d", &a); scanf ("%d", &b); c = product(); printf("%d times %d = %d \n", a, b, c); printf("sin(%d times %d)= %f\n",a,b,sin(c)); return(0); } int product(void) int z; z=a*b; return(z);

10 Arrays If you have lots of variables of the same type it is inconvenient to have to define each separately Eg. data1, data2, data3,……… Arrays are a way of storing a set of like variables Eg double data[100]; Values can be assigned directly Eg. double x[4]={23.4, 54.6, 66.1, 76.2} or using loops Eg for(i=0; i<4;i++) x[i]=sin(i*M_PI/4);

11 For an array x[N] of size N, the elements are labelled
x[0], x[1], x[2], …… x[N-2], x[N-1] Can be used just as other variables Eg. Sum2=0; for(i=0;i<N;i++) sum2=sum2+x[i]*x[i]; A common mistake is to access array elements that don’t exist. Eg. x[N]

12 Multidimensional Arrays
int random_array[10][10][10]; for(a=0;a<10;a++) { for(b=0;b<10;b++) for(c=0;c<=10;c++) random_array[a][b][c]=rand(); }

13 Pointers When we define a variable x, the computer labels a part of the memory x and stores the value of x in that part of memory. We can then use x as a variable in our calculations. Another way of accessing the variable x would be to know where it is stored, i.e. its address. We can define a “pointer” to x as a new type of variable that stores the address of the variable x.

14 #include <stdio.h>
int main(void) { double x; double *p; p=&x; x=2.0; printf(“x=%f\n”, x); *p=3.0; return(0); }

15 Pointers can be passed to functions to allow them to change more than one variable.
Eg. # include <stdio.h> void increase(int *, int *, int *); int main(void) { int x,y,z; increase(&x, &y, &z);. } void increase(int *px, int *py, int *pz) *px=*px+1; *py=*py+1; *pz=*pz+1;

16 Pointers and arrays are closely related.
x[0], x[1], x[2]……. are variables (eg. doubles). &x[0], &x[1], &x[2]……are their addresses. BUT x is a pointer to x[0]. Arrays elements can be accessed in two ways x[i] is the same as *(x+i)

17 Arrays can be passed between functions simply by passing a pointer to the first array element.
Much quicker than copying one array to another. If you change elements of the array within the function, these changes will be known everywhere in the code.

18 #include <stdio.h>
#define N 5 void mean(double *); int main(void) { int i; double x[N]; for(i=0;i<N;i++) printf(“input value %d: ”,i); scanf(“%f”,&x[i]); } mean(x); return(0); void mean(double *x) { int i; double sum; sum=0.0; for(i=0;i<N;i++) sum=sum+x[i]; printf(“Mean = %f\n”,sum/(N)); }


Download ppt "Further C Programming Variable types Loops Conditional statements"

Similar presentations


Ads by Google