======C/C++ Programming Journal====== ====8/26,2014==== ==First day of class!!== We discussed what is going to happen this semester and what to expect out of the class. We also set up our lab46 accounts and connected to the mailing list. Notes///// Bash-Shell environment Class Repo - src/cprog $mv *.c ~/src “Moves all programs ending in .c to the repository” $mkdir ~/src/cprog “makes directory cprog” $mv *.c ~/src/cprog “moves all .c programs in src to cprog” hg status hg add hg commit -m “adding code”\ hg push hg update hg log cd src/cprog/ ====8/28, 2014==== We created our Hello World program today and went over how to compile and run the program. ---------------------- #include int main() { fprintf(stdout,"Hello, World!\n"); return(0); } --------------------- ==Compile- gcc -o filename filename.c== ==File run ./filename== ====week of 9/2, 2014==== We went over data types (char-basically for holding character strings,int-basically for holding numbers) today. #include int main() { int a=5; int b=0x3d,c=073; int *d; // integer pointer char e,*f; // e is data, f character pointer. e='X'; // e is a single character d=&c; f=&e; printf("address of a is: %p\n",&a); printf("address of b is: %p\n",&b); printf("address of c is: %p\n",&c); printf("address of d is: %p\n",&d); printf("address of e is: %p\n",&e); printf("address of f is: %p\n",&f); printf("a contains: %d\n",a); printf("b contains: %d\n",b); printf("c contains: %d\n",c); printf("d contains: %p\n",d); printf("e contains: '%c'(%hhd)\n",e,e); printf("f contains: %p\n",f); printf("d dereferenced is: %d\n", *d); printf("f dereferenced is: '%c'(%hhd)\n",*f,*f); return(0); } My output address of a is: 0x7fffbf28456c\\ address of b is: 0x7fffbf284568\\ address of c is: 0x7fffbf284564\\ address of d is: 0x7fffbf284558\\ address of e is: 0x7fffbf284557\\ address of f is: 0x7fffbf284548\\ a contains: 5\\ b contains: 61\\ c contains: 59\\ d contains: 0x7fffbf284564\\ e contains: 'X'(88)\\ f contains: 0x7fffbf284557\\ d dereferenced is: 59\\ f dereferenced is: 'X'(88)\\ Afterwards we made minor changes to the program. ====week of 9/9, 2014==== This week we started going over how to use the scanf function. With this function we can ask someone to enter in some form of information and set what they entered to an int. Ex: int f; then ask for the scanf: scanf("%d",&f); Now the f int will have the property of what ever you entered into the scanf. the code we wrote this week. #include main() { int a; int b; int c; int d; float avg; printf("Enter score 1: "); scanf("%d",&a); printf("Enter score 2: "); scanf("%d",&b); printf("Enter score 3: "); scanf("%d",&c); printf("Enter score 4: "); scanf("%d",&d); avg=(float)(a+b+c+d)/4; printf("The average of %6.4d,%d,%d,%,d is %.2f\n",a,b,c,d,avg); return(0); } We then wrote a code that calls lets us change the base number we are calling and enter what we want to count to. The code then counts up to the base number and if it reaches 1 above the base number then it brings the first integer to 0 and adds 1 to the second integer\\ ex: base 3 counting to 4\\ 01\\ 02\\ it then makes the first integer 0 then adds 1 to second integer\\ 10\\ 11\\ #include #define MAX 4 // When ever the computer sees MAX it counts it as the number called int main() { int base,i,input,number[MAX]; for(i=0;i(base-1)) { number[MAX-1]=0; number[MAX-2]++; } if(number[MAX-2]>(base-1)) { number[MAX-2]=0; number[MAX-3]++; } if(number[MAX-3]>(base-1)) { number[MAX-3]=0; number[MAX-4]++; } } return(0); } ====week of 9/16, 2014==== Over the weekend we worked on adding more decimal places to our base number program. if(number[MAX-1]>(base-1)) { number[MAX-1]=0; number[MAX-2]++; } Once I figured out how the program worked I understood that you just had to change the value of MAX at the top and then you add another if statement like this one but change the values you are subtracting by to change the decimal place. Afterwards we also changed the code slightly so that we could enter a value in for MAX at a prompt instead of in the program. This way we could change the decimal places with 1 if statement instead of multiple ones. #include #define MAX 6 int main() { int base, i, input, j, number[MAX]; for(i = 0; i < MAX; i++) { number[i] = 0; } printf("What base? (2-10): "); scanf("%d", &base); printf("How much to count? "); scanf("%d", &input); for(i = 0; i < input; i++) { for (j = 0; j < MAX; j++) printf("%d ", number[j]); printf("%d\n"); number[MAX-1] = number[MAX-1] + 1; for (j = 1; j < MAX; j++) { if (number[MAX-j] > (base-1)) { number[MAX-j] = 0; number[MAX-(j+1)] = number[MAX-(j+1)] + 1; } } } return(0); } I also finished working on the square of a two digit number ending with 5 project we were given this week and uploaded it. ====week of 9/23, 2014==== This week we started going over data types and the byte sizes. We wrote a program for this Data types: -char -long int -long long int -short int #include int main() { printf("A char is %ld bytes.\n", sizeof(char)); // sizeof outputs the size of the data type printf("A short int is %ld bytes.\n", sizeof(short int)); // printf("A int is %ld bytes.\n", sizeof(int)); // int and long int are similar sizes. printf("A long int is %ld bytes.\n", sizeof(long int)); // %ld works with both long and short ints. printf("A long long in is %ld bytes.\n", sizeof(long long int)); return(0); } LOGIC in C & - bitwise AND | - bitwise OR ^ - bitwise XOR ====week of 9/30, 2014==== This week we were given the pipemath project, I was given the number 2 the math operator powerof and base of 5. I used a code similar to the basenum program we wrote earlier in the class. int base=5,i,input,value[NUM]; for(i=0;i(base-1)) { value[NUM-1]=0; value[NUM-2]++; } We also started working on our funwithfunctions program this week we started working with functions. The math term for a function if something like f(x)=y. #include #include int sum(int * ,int ); // <-function protoype // parameters go inbetween () information to help the program run. float average(int,int);// float-return type // int main() { int highest(int *,int); int lowest(int *,int); int num=4; int scores[num]; int i,total=0; int high,low; float avg; for(i=0;i ====week of 10/7, 2014==== This week we took a tangent instead of finishing our function program we decided to work with some of c programmings graphics tools (SDL) #include int main() { int i,j,quit=0; SDL_Surface *screen; SDL_Surface *box; SDL_Surface *box2; SDL_Surface *background; SDL_Event event; SDL_Rect offset; SDL_Rect offset2; SDL_Init(SDL_INIT_EVERYTHING); // creates a surface for the image screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE); background=IMG_Load("background.bmp"); //renders the image box=IMG_Load("box.bmp");//loads the image saved in this file and attaches it to box box2=IMG_Load("box2.bmp"); offset2.x=0; // sets offset2 starting position to (0,0) offset2.y=0; while(quit==0) { SDL_BlitSurface(background, NULL, screen, NULL); if(SDL_PollEvent(&event)) { if(event.type==SDL_KEYDOWN) // . is a structure?? { switch(event.key.keysym.sym) { case SDLK_ESCAPE: // escape key is being set to quit the program quit=1; break; case SDLK_RETURN: offset.x=320; offset.y=240; offset.w=box->w; offset.h=box->h; break; case SDLK_UP:// up key can be replaced by lower case w capital W means you have to hold shift down every time you hit w. offset.y=offset.y-10; break; // with programming screens 0,0 is the top right corner of the screen not center like math class. case SDLK_DOWN: offset.y=offset.y+10; break; case SDLK_RIGHT: offset.x=offset.x+10; break; case SDLK_LEFT: offset.x=offset.x-10; break; } } else if(event.type == SDL_QUIT) quit = 1; } if(offset2.x <= 0) i=10; else if(offset2.x >=640) i=-10; if(offset2.y <= 0) j=10; else if(offset2.y >=480) j=-10; offset2.x +=i; //offset2 is a box that moves on its own bouncing around on the walls. offset2.y +=j; SDL_BlitSurface(box2,NULL,screen,&offset2); SDL_BlitSurface(box,NULL,screen,&offset); SDL_Flip(screen); SDL_Delay(10); } return(0); } This was a fun program I learned a lot about how images work and signed up for a class next semester that goes into this further and expands your knowledge of c/c++ programming. After looking at this program you could make the basics of a pong game with this. Its an interesting concept and look into the background of these kinds of programs. ====week of 10/14, 2014==== BREAK WEEK!! Worked on project when I had time. ====week of 10/21, 2014==== This week we talked about malloc and structs. Took this description of malloc from class notes since it made a lot of sense to me. scores=(int*)malloc(sizeof(int)*num); /*sizeof returns thesize of an int and multiply's it by the number of ints aka num,(num=4 in this case), then malloc allocates the specified amount of memory. In this case, it will take 4 bytes times 4 bytes giving 16 bytes, which is just enough to store 4 integers.*/ *(scores+0) /*dereferences what's at the scores address and if you add you can dereference the things at the address shifted up from the starting adress.*/ We talked about structs and how they work with databases and things. Bellow we got this program from Matt through grabit command to show us a struct program he had on file from a while ago. #include #include int main() { int i; char entry[80], entries = 4, junk; struct person { char name[80]; int age; float height; }; do { printf("How many people in your database? "); scanf("%d", &entries); } while (entries <= 0); struct person people[entries]; for(i=0; i Node is another term meaning structure, they can be used together: structure node. Structure pointers look like this →, instead of a *. ====week of 10/28, 2014==== Actions that can be done to a file: Open,Read,Write,Append,Close,Create,Remove/Delete,Seek A program we created going over the basics of file usage #include #include int main() { char c; int i; int count=0; FILE*fptr; fptr=fopen("datafile.txt","r"); // allows us to interact with this file if(fptr==NULL) { fprintf(stdout,"Error opening file!\n"); exit(1); } while((c=fgetc(fptr))!=EOF) // allows us to call information from the file and prints it out for us { fprintf(stdout,"%c",c); } fclose(fptr); return(0); } fprintf(stdout""); creates three file pointers -file Stdin, stdout,stdir ====week of 11/04, 2014==== Started C++ programming this week (Object Oriented Programming) It is 90% similar to regular C most regular C programs will run in C++ also. Three major points of **Object Oriented Programming:** \\ *Polymorphism *Inheritance *Encapsulation The biggest difference between C programming and C++: \\ C++ is able to use functions with its structures which are called classes instead. Our introduction to classes (private and public) #include class rectangle { public: // member functions int area(); int perimeter(); rectangle(); // constructor rectangle(int,int); // ~rectangle(); is a destructor private: // member variables int length; int width; }; // defining our classes area,perimeter and our constructors int rectangle::area() //area is a member of the rectangle class due to :: { return((length*width)); } int rectangle::perimeter() { return((2*width)+(2*length)); } rectangle::rectangle() { length=0; width=0; } rectangle::rectangle(int length,int width) { this->length=length; //this is used to define itself this->width=width; } int main() { int area,perimeter; area=0; perimeter=0; rectangle rect1; rectangle *rect2; rect2=new rectangle(4,10); // rect2 is a new rectangle enter length of 4 width of 10 //rect1.length=6; does not work since length is a private class. area=rect1.area(); // rect1 is not a pointer . is used for structure perimeter=rect1.perimeter(); printf("The rect1's area is: %d\n", area); printf("The rect1's perimeter is: %d\n", perimeter); area=rect2->area(); perimeter=rect2->perimeter(); printf("The rect2's area is: %d\n", area); printf("The rect2's perimeter is: %d\n", perimeter); return(0); } Since area and perimeter were private variables we couldn't make changes to them in rect1 so we made some changes next class to allow us to make changes to it. Three different types of classes Public-usable by all parts of the program protected-Usable by all parts of the program until said otherwise private- only usable in this program for other private parts #include #include class rectangle { public: // member functions int area(); int perimeter(); rectangle(); // constructor rectangle(int,int); // ~rectangle(); is a destructor int getlength(); // accessor methods void setlength(int); int getwidth(); void setwidth(int); // end accessor methods private: // member variables int length; int width; }; // defining our classes area,perimeter and our constructors int rectangle::area() //area is a member of the rectangle class due to :: { return((length*width)); } int rectangle::perimeter() { return((2*width)+(2*length)); } rectangle::rectangle() { length=0; width=0; } rectangle::rectangle(int length,int width) { this->length=length; //this is used to define itself this->width=width; } int rectangle::getlength() { return(length); } void rectangle::setlength(int length) { this->length=length; } int rectangle::getwidth() { return(width); } void rectangle::setwidth(int width) { this->width=width; } int main() { int area,perimeter; area=0; perimeter=0; rectangle rect1; rectangle *rect2; rect2=new rectangle(4,10); // rect2 is a new rectangle enter length of 4 width of 10 rect1.setlength(6); rect1.setwidth(7); area=rect1.area(); // rect1 is not a pointer . is used for structure perimeter=rect1.perimeter(); printf("The rect1's area is: %d\n", area); printf("The rect1's perimeter is: %d\n", perimeter); area=rect2->area(); perimeter=rect2->perimeter(); printf("The rect2's area is: %d\n", area); printf("The rect2's perimeter is: %d\n", perimeter); return(0); } ====week of 11/11, 2014==== We discussed inheritance this week children and parent classes. We created multiple codes today that we archived together in a library Programs we created. Node.h #ifndef _NODE_H // checks for the existence of a symbol if it does exist skip s$ #define _NODE_H // defines _NODE_H #include // same thing as class Node { public: Node(); Node(int); void setvalue(int); int getvalue(); private: int value; }; #endif // closes the ifndef Create.cc #include "node.h" Node::Node() { value=0; } Node::Node(int value) { this->value=value; } Value.cc include "node.h" void Node::setvalue(int value) // lets us access the private value from node.h { this->value=value; } int Node::getvalue() { return(this->value);// this-> isnt required here but makes the program $ } Singlylinkednode.h #ifndef _SINGLY_H #define _SINGLY_H #include "node.h" class Singlylinkednode:public Node // Singlylinkednode is a inherant to the par$ { public: Singlylinkednode(); Singlylinkednode(int); Singlylinkednode*getNext(); //getnext returns a Singlylinkednod$ void setNext(Singlylinkednode *); private: Singlylinkednode*next; // references Singlylinkednode to itself }; #endif Next we is the Create.cc for Singlylinkednode #include "Singlylinkednode.h" Singlylinkednode::Singlylinkednode() { this->setvalue(0); this->next=NULL; // can't set pointers equal to 0 you set them equal to$ } Singlylinkednode::Singlylinkednode(int value) { this->setvalue(value); // since this is a child class it can't directly$ this->next=NULL; } next.cc #include "Singlylinkednode.h" Singlylinkednode*Singlylinkednode::getNext() { return(this->next); } void Singlylinkednode::setNext(Singlylinkednode*next) { this->next=next; } Afterwards we created object files from these with g++ -c create.cc// g++ -c value.cc g++ -c next.cc Next we created the archive with ar rcs libnode.a create.o value.o// ar rcs libsinglylinkedlist.a create.o next.o// ====week of 11/18, 2014==== We created the program to allow us to run last weeks programs we created. We also discussed nodes and how they can be interacted with and how they store data. #include #include "SinglyLinkedNode.h" int main() { int i; SinglyLinkedNode *start=NULL; SinglyLinkedNode *tmp=NULL; SinglyLinkedNode *tmp2=NULL; //setting up List of Singly Linked Nodes start=new SinglyLinkedNode(7); start->setNext(new SinglyLinkedNode(39)); tmp=start->getNext(); //inserting a new node tmp2=new SinglyLinkedNode(73); tmp2->setNext(start->getNext()); start->setNext(tmp2); //setting tmp to point to starting node tmp=start; //print out our list with a loop while (tmp!=NULL) { printf("%d->", tmp->getValue()); tmp=tmp->getNext(); } printf("NULL\n"); return (0); } Programming with nodes is dynamic you can make add or remove nodes as need be if you find you have to many you can remove some and use only the ones that are required. You can also break a link and add a new node in between two. After everything is written and done with we compile with g++ -o nodefun nodefun.cc -I inc -L SinglyLinkedNode -l SinglyLinkedNode -L node -l node Afterwards we learned about GD and wrote a few programs with shapes and images. we created a star in class. ====week of 11/25, 2014==== Thanks giving break ====week of 11/30, 2014==== Learned a little more about GD this week then got our EOCE and started working on that ====DECEMBER==== Worked on EOCE for the remainder of the semester