180 likes | 268 Views
Further C Programming Variable types Loops Conditional statements Local and Global variables Arrays Pointers. Types. Variable Types char character short short integer -32767 to 32767 int integer -/+2,147,483,647 long int long integer ~ -/+ 9E18
E N D
Further C Programming • Variable types • Loops • Conditional statements • Local and Global variables • Arrays • Pointers
Types Variable Types • char character • short short integer -32767 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
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
WhileLoop • 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);}
Conditional Statements • Most common example is theif…elseconstruction • Eg • if(mark >= 40) • printf(“pass\n”); • else • printf(“fail\n”); • Boolian operators: • = = && || ! • Equals And Or Not
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.
/* 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); printf ("Enter a number between 1 and 100: "); 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); }
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.
/* 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); printf ("Enter a number between 1 and 100: "); 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); }
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);
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]
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(); } } }
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.
#include <stdio.h> int main(void) { double x; double *p; p=&x; x=2.0; printf(“x=%f\n”, x); *p=3.0; printf(“x=%f\n”, x); return(0); }
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; • }
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)
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.
#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)); }