Lecture 4 Dynamic Memory Allocation and ImageMagick

Size: px
Start display at page:

Download "Lecture 4 Dynamic Memory Allocation and ImageMagick"

Transcription

1 Lecture 4 Dynamic Memory Allocation and ImageMagick

2 sizeof 1 #include <iostream> 2 #include <cstdlib> 3 #include <string> 4 int main() 5 { 6 7 std::cout<<"char "<<sizeof(char)<<" Bytes \n"; 8 std::cout<<"char *"<<sizeof(char *)<<" Bytes \n"; 9 10 std::cout<<"int "<<sizeof(int)<<" Bytes \n"; 11 std::cout<<"int *"<<sizeof(int *)<<" Bytes \n"; std::cout<<"float "<<sizeof(float)<<" Bytes \n"; 14 std::cout<<"float *"<<sizeof(float *)<<" Bytes \n"; std::cout<<"double "<<sizeof(double)<<" Bytes \n"; 17 std::cout<<"double *"<<sizeof(double *)<<" Bytes \n"; std::cout<<"void * "<<sizeof(void *)<<" Bytes \n"; std::cout<<"bool "<<sizeof(bool)<<" Bytes \n"; 22 std::cout<<"std::string "<<sizeof(std::string)<<" Bytes \n"; 23 std::cout<<"std::string *"<<sizeof(std::string *)<<" Bytes \n"; std::cout<<"long double "<<sizeof(long double)<<" Bytes \n"; 26 std::cout<<"long double *"<<sizeof(long double *)<<" Bytes \n"; return EXIT_SUCCESS; 30 } The sizeof function returns the size of a data type in bytes It is useful to make code re-locatable from different systems.

3 What do you notice? 1 char 1 Bytes 2 char *4 Bytes 3 int 4 Bytes 4 int *4 Bytes 5 float 4 Bytes 6 float *4 Bytes 7 double 8 Bytes 8 double *4 Bytes 9 void * 4 Bytes 10 bool 1 Bytes 11 std::string 4 Bytes 12 std::string *4 Bytes 13 long double 16 Bytes 14 long double *4 Bytes

4 32 Bit Leopard 64 Bit Ubuntu 1 char 1 Bytes 2 char *4 Bytes 3 int 4 Bytes 4 int *4 Bytes 5 float 4 Bytes 6 float *4 Bytes 7 double 8 Bytes 8 double *4 Bytes 9 void * 4 Bytes 10 bool 1 Bytes 11 std::string 4 Bytes 12 std::string *4 Bytes 13 long double 16 Bytes 14 long double *4 Bytes 1 char 1 Bytes 2 char *8 Bytes 3 int 4 Bytes 4 int *8 Bytes 5 float 4 Bytes 6 float *8 Bytes 7 double 8 Bytes 8 double *8 Bytes 9 void * 8 Bytes 10 bool 1 Bytes 11 std::string 8 Bytes 12 std::string *8 Bytes 13 long double 16 Bytes 14 long double *8 Bytes

5 Dynamic Memory C++ allows for memory to be allocated dynamically using the following keywords new delete These are the equivalent of the C functions malloc and free As the memory is created at run-time we need to use a pointer to specify a reference to the newly created memory The following program demonstrates the allocation and use of some memory

6 1 #include <iostream> 2 #include <stdlib.h> 3 4 int main() 5 { 6 int *pmyarray; 7 int size; 8 std::cout<<"how big an array would you like?\n"; 9 std::cin >>size; 10 // create an int array of size 11 pmyarray = new int[size]; for(int i=0; i<size; ++i) 14 pmyarray[i]=i; for(int i=0; i<size; ++i) 17 std::cout<<pmyarray[i]<<std::endl; // REMEMBER delete the array when finished 20 delete [] pmyarray; 21 return EXIT_SUCCESS; 22 }

7 new new will allocate memory of any type (including classes) but must be used with a pointer to the data type 1 char *pchar = new char[100]; 2 float *pfloat = new float[100]; 3 std::string *pstring = new std::string[100]; once we have used an array we must remove it (no auto garbage collection in C++) so we use delete

8 how they work char *pintarray; pintarray = new int[9] pintarray sizeof(int)*9 delete [] pintarray when new is called lots of internal things happen we need not worry about When the pointer is returned the internal system also remembers the size and type of the data when delete [] is called this is used to help deallocate the memory.

9 1 #include <iostream> 2 #include <cstdlib> 3 4 int main() 5 { 6 // define an array of chars 7 char *array = new char[28]; 8 9 char a; 10 int i; // create the following data structure 13 // \nabcdefghijklmnopqrstuvwxyz\n 14 array[0]='\n'; 15 for(a='a',i=1; a<='z'; ++i,++a) 16 array[i]=a; 17 array[i+1]='\n'; 18 // now grab the address of the 2nd 19 // array element 20 char *index=&array[1]; // and loop thru the data till the \n 23 while(*(index)!='\n') 24 std::cout<< (*(index++))<<" "; 25 std::cout<<"\n"; // now get the end z 28 // and loop till the \n 29 index=&array[26]; 30 while(*(index)!='\n') 31 std::cout<< (*(index--))<<" "; 32 std::cout<<"\n"; delete [] array; 35 return EXIT_SUCCESS; 36 }

10 void * The void * pointer is a special type of pointer which can assume any type To do this we have to use coercion of the data type to force it to a specific type. In C we can use (char *) (int *) etc to do this However C++ has a safer method using the cast functions

11 1 #include <iostream> 2 #include <cstdlib> 3 4 int main() 5 { 6 void *pmem; 7 8 pmem= new int[10]; 9 for(int i=0; i<10; ++i) 10 static_cast<int *>(pmem)[i]=i; 11 for(int i=0; i<10; ++i) 12 std::cout<<static_cast<int *>(pmem)[i]; 13 std::cout<<std::endl; 14 delete [] static_cast<int *>( pmem ); pmem = new float[20]; for(int i=0; i<20; ++i) 19 static_cast<float *>(pmem)[i]=i/2.0; 20 for(int i=0; i<20; ++i) 21 std::cout<<static_cast<float *>(pmem)[i]<<" "; 22 std::cout<<std::endl; 23 delete [] static_cast<float *>( pmem ); // create an array of strings 26 pmem = new std::string[3]; 27 static_cast<std::string *>(pmem)[0]="hello"; 28 static_cast<std::string *>(pmem)[1]=" void "; 29 static_cast<std::string *>(pmem)[2]=" pointers\n "; std::cout<<static_cast<std::string *>(pmem)[0]; 32 std::cout<<static_cast<std::string *>(pmem)[1]; 33 std::cout<<static_cast<std::string *>(pmem)[2]; 34 delete [] static_cast<std::string *>(pmem); 35 return EXIT_SUCCESS; 36 } Hello void pointers

12 static_cast<>() The static_cast<>() function takes two arguments <> is the type you wish to convert to () is the variable to cast to the type. This is equivalent to the C (char *) type coercion but much safer for C++ programs

13 3196 Width 2634 Height Each Pixel channel is a range from (char) pixel Each pixel has 3 channels (RGB) Therefore we have (3196*2634)*3*sizeof(char) bytes bytes of data Red Green Blue

14 Creating Images Using dynamic memory allocation we can create an array for an RGB image. The easiest way to do this is as follows Create an array based on the Width, Height and Number of Pixels in the Image depth Loop through these and fill in the pixels for each RGB Component Write to some image file format Free the array Most of the steps for this are simple but the saving of the image file relies on another library

15 ImageMagick / Magick++ Magick++ provides a simple C++ API to the ImageMagick image processing library which supports reading and writing a huge number of image formats as well as supporting a broad spectrum of traditional image processing operations. Magick++ provides access to most of the features available from the C API but in a simple object-oriented and well-documented framework. For More details look at the following url : The simplest operation with the Magick++ Library is the dumping of an array to an image file. This will be used in the following example

16 1 #include <iostream> 2 #include <Magick++.h> 3 #include <iostream> 4 #include <math.h> 5 #include <cstdlib> 6 7 // define widht and height of image 8 const int WIDTH=720; 9 const int HEIGHT=576; int main(void) 12 { 13 // allocate and array of char for image 14 // where data is packed in RGB format where 0=no intensity 15 // 255 = full intensity 16 char *image = new char [WIDTH*HEIGHT*3*sizeof(char)]; 17 // index into our image array 18 unsigned long int index=0; 19 // now loop for width and height of image and fill in 20 for(int y=0; y<height; ++y) 21 { 22 for(int x=0; x<width; ++x) 23 { 24 // set red channel to full 25 image[index]=255; 26 // G&B to off 27 image[index+1]=0; 28 image[index+2]=0; 29 // now skip to next RGB block 30 index+=3; 31 } // end of width loop 32 } // end of height loop 33 // now create an image data block 34 Magick::Image output(width,height,"rgb",magick::charpixel,image); 35 // set the output image depth to 16 bit 36 output.depth(16); 37 // write the file 38 output.write("test.tiff"); 39 // delete the image data. 40 delete [] image; 41 return EXIT_SUCCESS; 42 } include the image magick headers allocate space for image data Loop to create image Write image to file

17 Building 1 ####################################################################### 2 # sourcefiles is the list of the files used for the library 3 # we use the Scons Split function to seperate them for the build 4 ####################################################################### 5 6 sourcefiles = Split("""ImageGen.cpp""") 7 8 ####################################################################### 9 # PROGRAM is the name of the executable we wish to create 10 ####################################################################### PROGRAM='ImageGen' ####################################################################### 15 # build an environment to set the build flags 16 ####################################################################### env = Environment( 20 CC='g++', 21 CCFLAGS='-g -Wall' 22 ) ########################################################################### 25 # ParseConfig is a cool function which runs a config program and adds the 26 # flags to the build 27 # in this case we run Magick++-config and scons does the rest 28 ########################################################################### env.parseconfig( 'Magick++-config --cppflags --ldflags --libs') ########################################################################### 34 # and finally build the program 35 ########################################################################### env.program(program,sourcefiles)

18 Why not use char[][]? You will notice that the array used for the image data is a char [] You may think it would be easier to use a two dimensional array for x,y co-ordinates However we will see in various examples this is not the case. It doesn t take much code to allow use to set individual pixels in a single char [] array.

19 Setting Individual Pixels We can set individual pixels by accessing the memory data based on x,y co-ordinates We then set the block of 3 pixels for each R,G,B values The following code does this 1 void SetPixel(char *Data,unsigned int x,unsigned int y, char R,char G, char B) 2 { 3 unsigned int index=(y*width*3)+x*3; 4 Data[index]=R; 5 Data[index+1]=G; 6 Data[index+2]=B; 7 }

20 Setting Background Colour Once the SetPixel function is generated we can use it to set the background colour 1 void SetBGColour(char *Data,char r, char g, char b) 2 { 3 for(unsigned int y=0; y<height; ++y) 4 for(unsigned int x=0; x<width; ++x) 5 SetPixel(Data,x,y,r,g,b); 6 }

21 Exercise Using the previous example as a template create a sequence of frames to make the animation of a red line crossing from left to right The line should be in the center of the screen and have a height of 4 pixels. Each frame output should advance the line by 1 pixel in the x direction. To view the animation we will use the fcheck utility which needs the file names to be in the following format Name.###.tif where ### is a zero padded frame number.

22 Creating Sequences of names The easiest way to create a sequence is a loop an the sprintf C function However it relies on a static array which can cause problems if the string created gets larger than the array 1 char name[100]; 2 3 for(int frame=0; frame<10; ++i) 4 { 5 sprintf(name,"test.%04d.tiff",frame); }

23 boost++ Boost provides free peer-reviewed portable C++ source libraries. It has libraries that work well with the C++ Standard Library. Boost libraries are intended to be widely useful, and usable across a broad spectrum of applications. Some of the elements of Boost will be in the new C ++0x standard when released.

24 boost::format You will notice from the example below we have no static arrays We only use the string data type and the boost::format function Format specifiers (%04d %f etc) are still the same. 1 std::string fname; 2 // outer loop for frames 3 for (unsigned int frame=0; frame<width; ++frame) 4 { 5 fname=(boost::str(boost::format("test.%04d.tiff") % frame)); 6 std::cout <<"doing frame "<<fname<<std::endl; 7 }

25 1 #include <iostream> 2 #include <Magick++.h> 3 #include <iostream> 4 #include <math.h> 5 #include <stdlib.h> 6 #include <boost/format.hpp> 7 8 const unsigned int WIDTH=100; 9 const unsigned int HEIGHT=100; int main(void) 12 { 13 char *image = new char [WIDTH*HEIGHT*3]; 14 std::string fname; 15 // outer loop for frames 16 for (unsigned int frame=0; frame<width; ++frame) 17 { 18 // the below is the same as sprintf(fname,"test.%d.tiff",frame); 19 fname=(boost::str(boost::format("test.%04d.tiff") % frame)); 20 std::cout <<"doing frame "<<fname<<std::endl; SetBGColour(image,255,255,255); for(unsigned int y=(height/2)-2; y<(height/2)+2; y++) 25 for(unsigned int x=0; x<frame; x++) 26 SetPixel(image,x,y,255,0,0); Magick::Image output(width,height,"rgb",magick::charpixel,image); 30 output.depth(16); 31 output.write(fname.c_str()); }// end frame loop 34 delete [] image; 35 return EXIT_SUCCESS; 36 }

26 The % (modulus) Operator The remainder operator (%) returns the integer remainder of the result of dividing the first operand with the second For example the value of 7 % 2 is 1 / /

27 The % (modulus) Operator The magnitude of m % n must always be lest than the division n The table below show some of the results of the % operator 1 3 % 5 = 3 5 % 3 = % 5 = 4 5 % 4 = % 5 = 0 15 % 5 = % 5 = 1 15 % 6 = % 5 = 2 15 % -7 (varies 1 under gcc) 6 8 % 5 = 3 15 % 0 is undefined (core dump under gcc)

28 1 #include <Magick++.h> 2 #include <cmath> 3 #include <cstdlib> 4 5 const int WIDTH=201; 6 const int HEIGHT=201; 7 const int OFFSET=80; 8 9 int main() 10 { 11 char *image = new char [WIDTH*HEIGHT*3]; 12 unsigned long int index=0; 13 for(int y=0; y<height; y++) 14 { 15 for(int x=0; x<width; x++) 16 { 17 if( (y%20) && (x%20)) 18 { 19 image[index]=255; 20 image[index+1]=0; 21 image[index+2]=0; 22 } 23 else 24 { 25 image[index]=255; 26 image[index+1]=255; 27 image[index+2]=255; 28 } 29 index+=3; 30 }// end width loop 31 } // end height loop Magick::Image output(width,height,"rgb",magick::charpixel,image); 34 output.depth(16); 35 output.write("test.tiff"); delete [] image; 38 return EXIT_SUCCESS; 39 } x%40 y %40 x%20 y %10 x%100 y %2

29 Fake Sphere The following function is used to describe a sphere 1 // code modified from Computer Graphics with OpenGL F.S. Hill 2 3 // get the value on the sphere at co-ord s,t 4 5 float FakeSphere(float s, float t) 6 { 7 float r=sqrt((s-0.5)*(s-0.5)+(t-0.5)*(t-0.5)); 8 if(r<0.5) 9 return 1-r/0.5; 10 else 11 return 1.0; 12 }

30 Fake Sphere This function will work for any value of s and t in the range of 0-1. The values will then range from 1.0 outside the sphere to black edges and then to white in the centre as shown on the image above Using the template code add this function and draw a sphere.

31 int main(void) 4 { 5 // create an array of floats for our image data 6 float *image = new float [WIDTH*HEIGHT*3]; 7 // index into our data structure 8 unsigned long int index=0; 9 // Our step in texture space from 0-1 within the width of the image 10 float Sstep=1.0/WIDTH; 11 float Tstep=1.0/HEIGHT; 12 // actual S,T value for texture space 13 float S=0.0; 14 float T=0.0; 15 // loop for the image dimensions 16 for(int y=0; y<height; y++) 17 { 18 for(int x=0; x<width; x++) 19 { 20 // fill the data values with sphere values 21 image[index]=fakesphere(s,t); 22 image[index+1]=fakesphere(s,t); 23 image[index+2]=fakesphere(s,t); 24 // update the S value 25 S+=Sstep; 26 // step to the next image index 27 index+=3; 28 } 29 // update the T value 30 T+=Tstep; 31 // reset S to the left hand value 32 S=0.0; 33 } 34 // write out image and close 35 Magick::Image output(width,height,"rgb",magick::floatpixel,image); 36 output.depth(16); 37 output.write("test.tiff"); 38 delete [] image; 39 return EXIT_SUCCESS; 40 }

32 Repeating Patterns As the previous function works from 0-1 if we make the Sphere values range from 0-8 and only use the part after the decimal point we can create a pattern as shown above To do this we use the C++ function fmod The fmod() functions computes the floating-point remainder of x/ y. Specifically, the functions return the value x-i*y, for some integer i such that, if y is non-zero, the result has the same sign as x and magnitude less than the magnitude of y. So to make the value of T repeat 8 times we would use 1 float ss=fmod(s*8,1); 2 float tt=fmod(t*8,1);

33 int main(void) 4 { 5 // create an array of floats for our image data 6 float *image = new float [WIDTH*HEIGHT*3]; 7 // index into our data structure 8 unsigned long int index=0; 9 // Our step in texture space from 0-1 within the width of the image 10 float Sstep=1.0/WIDTH; 11 float Tstep=1.0/HEIGHT; 12 // actual S,T value for texture space 13 float S=0.0; 14 float T=0.0; 15 // used for the repeat values 16 float ss,tt; 17 // loop for the image dimensions 18 for(int y=0; y<height; y++) 19 { 20 for(int x=0; x<width; x++) 21 { 22 ss=fmod(s*40,1); 23 tt=fmod(t*40,1); // fill the data values with sphere values 26 image[index]=fakesphere(ss,tt); 27 image[index+1]=fakesphere(ss,tt); 28 image[index+2]=fakesphere(ss,tt); 29 // update the S value 30 S+=Sstep; 31 // step to the next image index 32 index+=3; 33 } 34 // update the T value 35 T+=Tstep; 36 // reset S to the left hand value 37 S=0.0; 38 } 39 // write out image and close 40 Magick::Image output(width,height,"rgb",magick::floatpixel,image); 41 output.depth(16); 42 output.write("test.tiff"); 43 delete [] image; 44 return EXIT_SUCCESS; 45 } ss=fmod(s*2,1); tt=fmod(t*4,1); ss=fmod(s*4,2); tt=fmod(t*4,2); ss=fmod(s*6,1); tt=fmod(t*6,1); ss=fmod(s*16,4); tt=fmod(t*16,4);

34 Random Numbers The standard C Library has a simple Pseudo Random number generator function called rand(); The rand() function computes a sequence of pseudo-random integers in the range of 0 to RAND_MAX (as defined by the header file <cstdlib>). It produces the same set of numbers each time unless we set a seed value using the srand() function

35 1 #ifndef RNG_H 2 #define RNG_H 3 4 #include <cmath> 5 #include <cstdlib> 6 7 class RNG 8 { 9 public : static int HALF_RAND=(RAND_MAX / 2); 12 /*! returns a random number between +/ \returns a random number between +/- 1 */ 14 static float RandomNum(void) 15 { 16 int rn; 17 rn = rand(); 18 return ((float)(rn - HALF_RAND) / (float)half_rand); 19 } 20 /*! returns a random number between +/- MaxVal 21 \param Real MaxVal the value to set for the max rand num 22 \returns a random number between +/- 1 */ 23 static float RandomNum(float MaxVal) 24 { 25 return MaxVal * RandomNum(); 26 } 27 /*! returns a positive random number between 0 and MaxVal 28 \param Real MaxVal the value to set for the max rand num 29 \returns a random number between 0 and MaxVal */ 30 static float RandomPosNum(float MaxVal) 31 { 32 return (MaxVal *rand()/(rand_max+1.0f)); 33 } /*! set the random seed */ static void SetSeed(void) 38 { 39 srand((unsigned int)time(null)); 40 } }; #endif

36 1 int main(void) 2 { 3 float *image = new float [WIDTH*HEIGHT*3]; 4 5 int x,y; 6 float r,g,b; 7 8 for(unsigned int i=0; i<500000; ++i) 9 { 10 x=rng::randomposnum(width); 11 y=rng::randomposnum(height); 12 r=rng::randomposnum(1.0); 13 g=rng::randomposnum(1.0); 14 b=rng::randomposnum(1.0); SetPixel(image,x,y,r,g,b); 17 } Magick::Image output(width,height,"rgb",magick::floatpixel,image); 20 output.depth(16); 21 output.write("test.tiff"); delete [] image; 25 return EXIT_SUCCESS; 26 }

37 Drawing Lines To draw a line in raster space we need to specify two points (x1,y1,x2,y2) We then need to determine which elements in the raster are to be set One of the earliest and most simple method to do this is the Bresenham's line algorithm The algorithm determines which points in an n-dimensional raster should be plotted in order to form a close approximation to a straight line between two given points. It uses only integer addition, subtraction and bit shifting all of which are very cheap operations in standard computer architectures. It is one of the earliest algorithms developed in the field of computer graphics.

38 Pseudo Code 1 function line(x0, x1, y0, y1) 2 boolean steep := abs(y1 - y0) > abs(x1 - x0) 3 if steep then 4 swap(x0, y0) 5 swap(x1, y1) 6 if x0 > x1 then 7 swap(x0, x1) 8 swap(y0, y1) 9 int deltax := x1 - x0 10 int deltay := abs(y1 - y0) 11 int error := deltax / 2 12 int ystep 13 int y := y0 14 if y0 < y1 then ystep := 1 else ystep := for x from x0 to x1 16 if steep then plot(y,x) else plot(x,y) 17 error := error - deltay 18 if error < 0 then 19 y := y + ystep 20 error := error + deltax

39 1 #include <algorithm> 2 3 void DrawLine(float *Data,int x0, int y0, int x1, int y1,float r, float g, float b) 4 { 5 bool steep = abs(y1 - y0) > abs(x1 - x0); 6 if(steep) 7 { 8 std::swap(x0,y0); 9 std::swap(x1,y1); 10 } 11 if( x0> x1) 12 { 13 std::swap(x0,x1); 14 std::swap(x1,y1); 15 } 16 int deltax = x1-x0; 17 int deltay = abs(y1-y0); 18 int error = deltax/2; 19 int ystep; 20 int y = y0; 21 if(y0 < y1) 22 ystep=1; 23 else 24 ystep=-1; 25 for(int x=x0; x<x1; ++x) 26 { 27 if(steep) 28 SetPixel(Data,y,x,r,g,b); 29 else 30 SetPixel(Data,x,y,r,g,b); 31 error-=deltay; 32 if(error <0) 33 { 34 y+=ystep; 35 error+=deltax; 36 } 37 } 38 } Implementation use the C++ swap method from <algorithm>

40 Drawing Circles The midpoint circle algorithm is an algorithm used to determine the points needed for drawing a circle. The algorithm is a variant of Bresenham's line algorithm, and is thus sometimes known as Bresenham's circle algorithm, although not actually invented by Bresenham.

41 Implementation 1 void DrawCircle(float *Data,int x0, int y0, int radius,float r, float g, float b) 2 { 3 int f = 1 - radius; 4 int ddf_x = 1; 5 int ddf_y = -2 * radius; 6 int x = 0; 7 int y = radius; 8 9 SetPixel(Data,x0, y0 + radius,r,g,b); 10 SetPixel(Data,x0, y0 - radius,r,g,b); 11 SetPixel(Data,x0 + radius, y0,r,g,b); 12 SetPixel(Data,x0 - radius, y0,r,g,b); while(x < y) 15 { 16 if(f >= 0) 17 { 18 y--; 19 ddf_y += 2; 20 f += ddf_y; 21 } 22 x++; 23 ddf_x += 2; 24 f += ddf_x; 25 SetPixel(Data,x0 + x, y0 + y,r,g,b); 26 SetPixel(Data,x0 - x, y0 + y,r,g,b); 27 SetPixel(Data,x0 + x, y0 - y,r,g,b); 28 SetPixel(Data,x0 - x, y0 - y,r,g,b); 29 SetPixel(Data,x0 + y, y0 + x,r,g,b); 30 SetPixel(Data,x0 - y, y0 + x,r,g,b); 31 SetPixel(Data,x0 + y, y0 - x,r,g,b); 32 SetPixel(Data,x0 - y, y0 - x,r,g,b); } 35 } As this function can produce values outside the range of the raster structure we need to modify the SetPixel function 1 void SetPixel(float *Data, int x,int y, float R,float G, float B) 2 { 3 if(x>=width y>=height) 4 return; 5 unsigned int index=(y*width*3)+x*3; 6 Data[index]=R; 7 Data[index+1]=G; 8 Data[index+2]=B; 9 }

42 References format.html Graphics Gems" (book 1) 1990, page 99 s_line_algorithm bresenh.html

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed CS 150 Introduction to Computer Science I Data Types Today Last we covered o main function o cout object o How data that is used by a program can be declared and stored Today we will o Investigate the

More information

EECS150 - Digital Design Lecture 12 - Video Interfacing. MIPS150 Video Subsystem

EECS150 - Digital Design Lecture 12 - Video Interfacing. MIPS150 Video Subsystem EECS15 - Digital Design Lecture 12 - Video Interfacing Feb 28, 213 John Wawrzynek Spring 213 EECS15 - Lec12-video Page 1 MIPS15 Video Subsystem Gives software ability to display information on screen.

More information

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009 Lecture 5: Multidimensional Arrays CS209 : Algorithms and Scientific Computing Wednesday, 11 February 2009 CS209 Lecture 5: Multidimensional Arrays 1/20 In today lecture... 1 Let s recall... 2 Multidimensional

More information

EECS150 - Digital Design Lecture 15 - Video

EECS150 - Digital Design Lecture 15 - Video EECS150 - Digital Design Lecture 15 - Video March 6, 2011 John Wawrzynek Spring 2012 EECS150 - Lec15-video Page 1 MIPS150 Video Subsystem Gives software ability to display information on screen. Equivalent

More information

Laboratory Exercise 8

Laboratory Exercise 8 Laboratory Exercise 8 Introduction to Graphics and Animation The purpose of this exercise is to learn how to display images and perform animation. We will use the Nios II processor, in the pre-build DE-series

More information

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu History and Evolution of Programming Languages 1. Explain the relationship between machine

More information

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Pointers and Strings Prentice Hall, Inc. All rights reserved. Pointers and Strings 1 sizeof operator Pointer Expressions and Pointer Arithmetic Relationship Between Pointers and Arrays Arrays of Pointers Case Study: Card Shuffling and Dealing Simulation sizeof operator

More information

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); } 1. (9 pts) Show what will be output by the cout s in this program. As in normal program execution, any update to a variable should affect the next statement. (Note: boolalpha simply causes Booleans to

More information

Textures. Texture Mapping. Bitmap Textures. Basic Texture Techniques

Textures. Texture Mapping. Bitmap Textures. Basic Texture Techniques Texture Mapping Textures The realism of an image is greatly enhanced by adding surface textures to the various faces of a mesh object. In part a) images have been pasted onto each face of a box. Part b)

More information

6.096 Introduction to C++ January (IAP) 2009

6.096 Introduction to C++ January (IAP) 2009 MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Welcome to 6.096 Lecture

More information

ISA 563 : Fundamentals of Systems Programming

ISA 563 : Fundamentals of Systems Programming ISA 563 : Fundamentals of Systems Programming Variables, Primitive Types, Operators, and Expressions September 4 th 2008 Outline Define Expressions Discuss how to represent data in a program variable name

More information

Lecture 2 More C++ Jon Macey

Lecture 2 More C++ Jon Macey Lecture 2 More C++ Jon Macey namespaces namespaces allow us to separate program elements into different named logical units By using namespaces we can declare functions, classes and other code that is

More information

Chapter 3 - Functions

Chapter 3 - Functions Chapter 3 - Functions 1 Outline 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3.4 Functions 3.5 Function Definitions 3.6 Function Prototypes 3.7 Header Files 3.8 Random Number

More information

Matlab? Chapter 3-4 Matlab and IPT Basics. Working Environment. Matlab Demo. Array. Data Type. MATLAB Desktop:

Matlab? Chapter 3-4 Matlab and IPT Basics. Working Environment. Matlab Demo. Array. Data Type. MATLAB Desktop: Matlab? Lecture Slides ME 4060 Machine Vision and Vision-based Control Chapter 3-4 Matlab and IPT Basics By Dr. Debao Zhou 1 MATric LABoratory data analysis, prototype and visualization Matrix operation

More information

Programming. C++ Basics

Programming. C++ Basics Programming C++ Basics Introduction to C++ C is a programming language developed in the 1970s with the UNIX operating system C programs are efficient and portable across different hardware platforms C++

More information

Topic 2: C++ Programming fundamentals

Topic 2: C++ Programming fundamentals Topic 2: C++ Programming fundamentals Learning Outcomes Upon successful completion of this topic you will be able to: describe basic elements of C++ programming language compile a program identify and

More information

Dynamic Data Structures. CSCI 112: Programming in C

Dynamic Data Structures. CSCI 112: Programming in C Dynamic Data Structures CSCI 112: Programming in C 1 It s all about flexibility In the programs we ve made so far, the compiler knows at compile time exactly how much memory to allocate for each variable

More information

Lecture 5 Files and Streams

Lecture 5 Files and Streams Lecture 5 Files and Streams Introduction C programs can store results & information permanently on disk using file handling functions These functions let you write either text or binary data to a file,

More information

EECS150 - Digital Design Lecture 15 - Project Description, Part 5

EECS150 - Digital Design Lecture 15 - Project Description, Part 5 EECS150 - Digital Design Lecture 15 - Project Description, Part 5 March 8, 2011 John Wawrzynek Spring 2011 EECS150 - Lec15-proj5 Page 1 Announcements Exam in lab tomorrow evening 6pm. Spring 2011 EECS150

More information

Line Rasterization and Antialiasing

Line Rasterization and Antialiasing Katarina Mitchell, James Rich and Michael Bullis CprE 480x Final Project Line Rasterization and Antialiasing Rubric Code Modified and Added OpenGL Additions New types in SGP_config In utils/include/sgp.h

More information

Lab Instructor : Jean Lai

Lab Instructor : Jean Lai Lab Instructor : Jean Lai Group related statements to perform a specific task. Structure the program (No duplicate codes!) Must be declared before used. Can be invoked (called) as any number of times.

More information

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Data Types Basic Types Enumerated types The type void Derived types

More information

PHY4321 Summary Notes

PHY4321 Summary Notes PHY4321 Summary Notes The next few pages contain some helpful notes that summarize some of the more useful material from the lecture notes. Be aware, though, that this is not a complete set and doesn t

More information

CS 376b Computer Vision

CS 376b Computer Vision CS 376b Computer Vision 09 / 25 / 2014 Instructor: Michael Eckmann Today s Topics Questions? / Comments? Enhancing images / masks Cross correlation Convolution C++ Cross-correlation Cross-correlation involves

More information

Introduction to Programming

Introduction to Programming Introduction to Programming session 6 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Spring 2011 These slides are created using Deitel s slides Sharif University of Technology Outlines

More information

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Topic 6: A Quick Intro To C. Reading. goto Considered Harmful History Topic 6: A Quick Intro To C Reading Assumption: All of you know basic Java. Much of C syntax is the same. Also: Some of you have used C or C++. Goal for this topic: you can write & run a simple C program

More information

Advanced Pointer Topics

Advanced Pointer Topics Advanced Pointer Topics Pointers to Pointers A pointer variable is a variable that takes some memory address as its value. Therefore, you can have another pointer pointing to it. int x; int * px; int **

More information

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja

More information

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University Fundamental Data Types CSE 130: Introduction to Programming in C Stony Brook University Program Organization in C The C System C consists of several parts: The C language The preprocessor The compiler

More information

C Pointers. sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then

C Pointers. sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then 1 7 C Pointers 7.7 sizeof Operator 2 sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then int myarray[ 10 ]; printf( "%d", sizeof(

More information

MAZE A SIMPLE GPU WITH EMBEDDED VIDEO GAME. Designed by:

MAZE A SIMPLE GPU WITH EMBEDDED VIDEO GAME. Designed by: MAZE A SIMPLE GPU WITH EMBEDDED VIDEO GAME Designed by: Joseph Liang Joe Zhang Wei-Chung Hsu David Lau cjl2104@columbia.edu xz2025@columbia.edu wh2138@cs.columbia.edu dsl2012@columbia.edu 1. INTRODUCTION

More information

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements Chapter 5 Looping Building on the foundation Now that we know a little about cout cin math operators boolean operators making decisions using if statements Advantages of Computers Computers are really

More information

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay C++ Basics Data Processing Course, I. Hrivnacova, IPN Orsay The First Program Comments Function main() Input and Output Namespaces Variables Fundamental Types Operators Control constructs 1 C++ Programming

More information

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam Multimedia Programming 2004 Lecture 2 Erwin M. Bakker Joachim Rijsdam Recap Learning C++ by example No groups: everybody should experience developing and programming in C++! Assignments will determine

More information

Computing and Statistical Data Analysis Lecture 3

Computing and Statistical Data Analysis Lecture 3 Computing and Statistical Data Analysis Lecture 3 Type casting: static_cast, etc. Basic mathematical functions More i/o: formatting tricks Scope, namspaces Functions 1 Type casting Often we need to interpret

More information

Introduction. Lecture 5 Files and Streams FILE * FILE *

Introduction. Lecture 5 Files and Streams FILE * FILE * Introduction Lecture Files and Streams C programs can store results & information permanently on disk using file handling functions These functions let you write either text or binary data to a file, and

More information

C++ Tutorial AM 225. Dan Fortunato

C++ Tutorial AM 225. Dan Fortunato C++ Tutorial AM 225 Dan Fortunato Anatomy of a C++ program A program begins execution in the main() function, which is called automatically when the program is run. Code from external libraries can be

More information

Outline. 1 About the course

Outline. 1 About the course Outline EDAF50 C++ Programming 1. Introduction 1 About the course Sven Gestegård Robertz Computer Science, LTH 2018 2 Presentation of C++ History Introduction Data types and variables 1. Introduction 2/1

More information

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces Basic memory model Using functions Writing functions Basics Prototypes Parameters Return types Functions and memory Names and namespaces When a program runs it requires main memory (RAM) space for Program

More information

From python to C++ part 2. Jon Macey

From python to C++ part 2. Jon Macey From python to C++ part 2 Jon Macey http://nccastaff.bournemouth.ac.uk/jmacey/cppintro jmacey@bournemouth.ac.uk namespaces namespaces allow us to separate program elements into different named logical

More information

CSC 1600 Memory Layout for Unix Processes"

CSC 1600 Memory Layout for Unix Processes CSC 16 Memory Layout for Unix Processes" 1 Lecture Goals" Behind the scenes of running a program" Code, executable, and process" Memory layout for UNIX processes, and relationship to C" : code and constant

More information

ECEN 449 Microprocessor System Design. Review of C Programming

ECEN 449 Microprocessor System Design. Review of C Programming ECEN 449 Microprocessor System Design Review of C Programming 1 Objectives of this Lecture Unit Review C programming basics Refresh es programming g skills s 2 1 Basic C program structure # include

More information

Laboratory Exercise 5

Laboratory Exercise 5 Laboratory Exercise 5 Using ASCII Graphics for Animation The purpose of this exercise is to learn how to perform simple animations under Linux. We will use the ARM A9 processor, in the DE1-SoC Computer.

More information

Image I/O and OpenGL Textures

Image I/O and OpenGL Textures Image I/O and OpenGL Textures Creating Images Using dynamic memory allocation we can create an array for an RGB image. The easiest way to do this is as follows Create an array based on the Width, Height

More information

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used. Declaration Fundamental Data Types All variables must be declared before being used. Tells compiler to set aside an appropriate amount of space in memory to hold a value. Enables the compiler to perform

More information

CS475m - Computer Graphics. Lecture 1 : Rasterization Basics

CS475m - Computer Graphics. Lecture 1 : Rasterization Basics CS475m - Computer Graphics Lecture 1 : Rasterization Basics Image Formation Light Source Camera Image World Image Formation Light Source Incident Ray Camera Reflected Ray Image World Transmitted Ray Image

More information

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single Functions in C++ Problem-Solving Procedure With Modular Design: Program development steps: Analyze the problem Develop a solution Code the solution Test/Debug the program C ++ Function Definition: A module

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Lecture 3 - Constants, Variables, Data Types, And Operations Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Outline C Program Data types Variables

More information

Solution Notes. COMP 151: Terms Test

Solution Notes. COMP 151: Terms Test Family Name:.............................. Other Names:............................. ID Number:............................... Signature.................................. Solution Notes COMP 151: Terms

More information

Chapter 3 - Functions. Chapter 3 - Functions. 3.1 Introduction. 3.2 Program Components in C++

Chapter 3 - Functions. Chapter 3 - Functions. 3.1 Introduction. 3.2 Program Components in C++ Chapter 3 - Functions 1 Chapter 3 - Functions 2 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3. Functions 3.5 Function Definitions 3.6 Function Prototypes 3. Header Files 3.8

More information

Functions and Recursion

Functions and Recursion Functions and Recursion 1 Outline Introduction Program Components in C++ Math Library Functions Functions Function Definitions Function Prototypes Header Files Random Number Generation Example: A Game

More information

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long LESSON 5 ARITHMETIC DATA PROCESSING The arithmetic data types are the fundamental data types of the C language. They are called "arithmetic" because operations such as addition and multiplication can be

More information

Chapter 3 - Functions

Chapter 3 - Functions Chapter 3 - Functions 1 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3.4 Functions 3.5 Function Definitions 3.6 Function Prototypes 3.7 Header Files 3.8 Random Number Generation

More information

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C Overview The C Programming Language (with material from Dr. Bin Ren, William & Mary Computer Science) Motivation Hello, world! Basic Data Types Variables Arithmetic Operators Relational Operators Assignments

More information

Programming Fundamentals and Methodology. COMP104: C++ Basics

Programming Fundamentals and Methodology. COMP104: C++ Basics Programming Fundamentals and Methodology COMP104: C++ Basics Dr. Brian Mak Dr. Pedro Sander Dr. Zhen Zhou Department of Computer Science & Engineering The Hong Kong University of Science and Technology

More information

Topic 6: A Quick Intro To C

Topic 6: A Quick Intro To C Topic 6: A Quick Intro To C Assumption: All of you know Java. Much of C syntax is the same. Also: Many of you have used C or C++. Goal for this topic: you can write & run a simple C program basic functions

More information

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University ECEN 449 Microprocessor System Design Review of C Programming 1 Objectives of this Lecture Unit Review C programming basics Refresh programming skills 2 Basic C program structure # include main()

More information

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science) The C Programming Language (with material from Dr. Bin Ren, William & Mary Computer Science) 1 Overview Motivation Hello, world! Basic Data Types Variables Arithmetic Operators Relational Operators Assignments

More information

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah Lecturer Department of Computer Science & IT University of Balochistan 1 Outline p Introduction p Program development p C language and beginning with

More information

Elements of C in C++ data types if else statement for loops. random numbers rolling a die

Elements of C in C++ data types if else statement for loops. random numbers rolling a die Elements of C in C++ 1 Types and Control Statements data types if else statement for loops 2 Simulations random numbers rolling a die 3 Functions and Pointers defining a function call by value and call

More information

Tutorial-2a: First steps with C++ programming

Tutorial-2a: First steps with C++ programming Programming for Scientists Tutorial 2a 1 / 18 HTTP://WWW.HEP.LU.SE/COURSES/MNXB01 Introduction to Programming and Computing for Scientists Tutorial-2a: First steps with C++ programming Programming for

More information

Programming. Data Structure

Programming. Data Structure Programming & Data Structure For Computer Science & Information Technology By www.thegateacademy.com Syllabus Syllabus for Programming and Data Structures Programming in C, Arrays, Stacks, Queues, Linked

More information

Introduction to C. Systems Programming Concepts

Introduction to C. Systems Programming Concepts Introduction to C Systems Programming Concepts Introduction to C A simple C Program Variable Declarations printf ( ) Compiling and Running a C Program Sizeof Program #include What is True in C? if example

More information

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

COMP322 - Introduction to C++ Lecture 02 - Basics of C++ COMP322 - Introduction to C++ Lecture 02 - Basics of C++ School of Computer Science 16 January 2012 C++ basics - Arithmetic operators Where possible, C++ will automatically convert among the basic types.

More information

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O Overview of C Basic Data Types Constants Variables Identifiers Keywords Basic I/O NOTE: There are six classes of tokens: identifiers, keywords, constants, string literals, operators, and other separators.

More information

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below: QUIZ 1. Explain the meaning of the angle brackets in the declaration of v below: This is a template, used for generic programming! QUIZ 2. Why is the vector class called a container? 3. Explain how the

More information

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

Functions. Introduction :

Functions. Introduction : Functions Introduction : To develop a large program effectively, it is divided into smaller pieces or modules called as functions. A function is defined by one or more statements to perform a task. In

More information

Programming, numerics and optimization

Programming, numerics and optimization Programming, numerics and optimization Lecture A-2: Programming basics II Łukasz Jankowski ljank@ippt.pan.pl Institute of Fundamental Technological Research Room 4.32, Phone +22.8261281 ext. 428 March

More information

First of all, it is a variable, just like other variables you studied

First of all, it is a variable, just like other variables you studied Pointers: Basics What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the address (rather than the value)

More information

Direct Memory Access. Lecture 2 Pointer Revision Command Line Arguments. What happens when we use pointers. Same again with pictures

Direct Memory Access. Lecture 2 Pointer Revision Command Line Arguments. What happens when we use pointers. Same again with pictures Lecture 2 Pointer Revision Command Line Arguments Direct Memory Access C/C++ allows the programmer to obtain the value of the memory address where a variable lives. To do this we need to use a special

More information

Chapter 15 - C++ As A "Better C"

Chapter 15 - C++ As A Better C Chapter 15 - C++ As A "Better C" Outline 15.1 Introduction 15.2 C++ 15.3 A Simple Program: Adding Two Integers 15.4 C++ Standard Library 15.5 Header Files 15.6 Inline Functions 15.7 References and Reference

More information

ECE264 Spring 2014 Exam 2, March 11, 2014

ECE264 Spring 2014 Exam 2, March 11, 2014 ECE264 Spring 2014 Exam 2, March 11, 2014 In signing this statement, I hereby certify that the work on this exam is my own and that I have not copied the work of any other student while completing it.

More information

EL6483: Brief Overview of C Programming Language

EL6483: Brief Overview of C Programming Language EL6483: Brief Overview of C Programming Language EL6483 Spring 2016 EL6483 EL6483: Brief Overview of C Programming Language Spring 2016 1 / 30 Preprocessor macros, Syntax for comments Macro definitions

More information

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print CSC 4304 - Systems Programming Fall 2010 Lecture - II Basics of C Programming Summary of Last Class Basics of UNIX: logging in, changing password text editing with vi, emacs and pico file and directory

More information

Input and Expressions Chapter 3 Slides #4

Input and Expressions Chapter 3 Slides #4 Input and Expressions Chapter 3 Slides #4 Topics 1) How can we read data from the keyboard? 2) How can we calculate values? 3) How can we manage the type of a value? 4) How can we round or get random numbers?

More information

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2 Numerical Computing in C and C++ Jamie Griffin Semester A 2017 Lecture 2 Visual Studio in QM PC rooms Microsoft Visual Studio Community 2015. Bancroft Building 1.15a; Queen s W207, EB7; Engineering W128.D.

More information

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University Lesson #3 Variables, Operators, and Expressions Variables We already know the three main types of variables in C: int, char, and double. There is also the float type which is similar to double with only

More information

Basic Types, Variables, Literals, Constants

Basic Types, Variables, Literals, Constants Basic Types, Variables, Literals, Constants What is in a Word? A byte is the basic addressable unit of memory in RAM Typically it is 8 bits (octet) But some machines had 7, or 9, or... A word is the basic

More information

CSE 333 Lecture 9 - intro to C++

CSE 333 Lecture 9 - intro to C++ CSE 333 Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington Administrivia & Agenda Main topic: Intro to C++ But first: Some hints on HW2 Labs: The

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010 CSE 374 Programming Concepts & Tools Hal Perkins Spring 2010 Lecture 19 Introduction ti to C++ C++ C++ is an enormous language: g All of C Classes and objects (kind of like Java, some crucial differences)

More information

The C++ Language. Arizona State University 1

The C++ Language. Arizona State University 1 The C++ Language CSE100 Principles of Programming with C++ (based off Chapter 2 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

More information

Introduction to C Language

Introduction to C Language Introduction to C Language Instructor: Professor I. Charles Ume ME 6405 Introduction to Mechatronics Fall 2006 Instructor: Professor Charles Ume Introduction to C Language History of C Language In 1972,

More information

Output Primitives Lecture: 3. Lecture 3. Output Primitives. Assuming we have a raster display, a picture is completely specified by:

Output Primitives Lecture: 3. Lecture 3. Output Primitives. Assuming we have a raster display, a picture is completely specified by: Lecture 3 Output Primitives Assuming we have a raster display, a picture is completely specified by: - A set of intensities for the pixel positions in the display. - A set of complex objects, such as trees

More information

Lecture 8 Dynamic Memory Allocation

Lecture 8 Dynamic Memory Allocation Lecture 8 Dynamic Memory Allocation CS240 1 Memory Computer programs manipulate an abstraction of the computer s memory subsystem Memory: on the hardware side 3 @ http://computer.howstuffworks.com/computer-memory.htm/printable

More information

M1-R4: Programing and Problem Solving using C (JULY 2018)

M1-R4: Programing and Problem Solving using C (JULY 2018) M1-R4: Programing and Problem Solving using C (JULY 2018) Max Marks: 100 M1-R4-07-18 DURATION: 03 Hrs 1. Each question below gives a multiple choice of answers. Choose the most appropriate one and enter

More information

Week 3 Lecture 2. Types Constants and Variables

Week 3 Lecture 2. Types Constants and Variables Lecture 2 Types Constants and Variables Types Computers store bits: strings of 0s and 1s Types define how bits are interpreted They can be integers (whole numbers): 1, 2, 3 They can be characters 'a',

More information

Data encoding. Lauri Võsandi

Data encoding. Lauri Võsandi Data encoding Lauri Võsandi Binary data Binary can represent Letters of alphabet, plain-text files Integers, floating-point numbers (of finite precision) Pixels, images, video Audio samples Could be stored

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

More information

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference Pass by Value More Functions Different location in memory Changes to the parameters inside the function body have no effect outside of the function. 2 Passing Parameters by Reference Example: Exchange

More information

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes Distributed Real-Time Control Systems Lecture 17 C++ Programming Intro to C++ Objects and Classes 1 Bibliography Classical References Covers C++ 11 2 What is C++? A computer language with object oriented

More information

CSCI 171 Chapter Outlines

CSCI 171 Chapter Outlines Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures

More information

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information Laboratory 2: Programming Basics and Variables Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information 3. Comment: a. name your program with extension.c b. use o option to specify

More information

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington CSE 333 Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington Administrivia New exercise posted yesterday afternoon, due Monday morning - Read a directory

More information

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure C Overview Basic C Program Structure C OVERVIEW BASIC C PROGRAM STRUCTURE Goals The function main( )is found in every C program and is where every C program begins speed execution portability C uses braces

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example Appendix F Number Systems Binary Numbers Decimal notation represents numbers as powers of 10, for example 1729 1 103 7 102 2 101 9 100 decimal = + + + There is no particular reason for the choice of 10,

More information

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Principles of C and Memory Management

Principles of C and Memory Management COMP281 Lecture 8 Principles of C and Memory Management Dr Lei Shi Last Lecture Pointer Basics Previous Lectures Arrays, Arithmetic, Functions Last Lecture Pointer Basics Previous Lectures Arrays, Arithmetic,

More information