Table of Contents

C/C++ Programming Journal

September, 4th 2013

*What I learned in class this week.

*How to use lab46 and the Linux terminal.

*int main()

printf(“hello, world\n”);

  return a;

* Useful Commands.

*Version Control - Mercurial is used to to keep track of changes made to your repository.

September, 11th 2013

This week in class.

Data Types.
  Signed/Unsigned char - 1 byte.
  Signed/Unsigned short int - 2 bytes.
  Signed/Unsigned int - 4 bytes.
  Signed/Unsigned long int - 8 bytes.
  Floating Point Data Types.
  float.
   double.
   long double.
printf data type formats.
  Signed char - %hhd.
  Unsigned char - %hhu.
  Signed short int - %hd.
  Unsigned short int - %hu.
  Signed int - %d.
  Unsigned int - %u.
  Signed long int - %ld.
  Unsigned long int - %lu.
Some example of these being used in code:
  printf("a signed char is %hhu bytes\n", sizeof(sc)); - prints the size of a char in bytes.
  printf("a signed long int is %ld bytes\n", sizeof(sli)); - prints the size of a long int in bytes. 

Septmeber, 17th 2013

*This week in class

* If Statements- VERY IMPORTANT, NOT IF LOOP.

*Loops

September 24, 2013

*Arrays

     triangle[0].x = wide/2;
     triangle[0].y = 0;
     triangle[1].x = wide/4;
     triangle[1].y = high/2;
     triangle[2].x = 3*wide/4;
     triangle[2].y = high/2;
     gdImageFilledPolygon(img,triangle,3,color[BLUE]);

* gd.h

October, 2nd 2013

*Functions

October, 10th 2013

More function fun and preprocessor directives.

October, 17th 2013

Passing by value compared to passing by address/reference

LAB

Over the semester we have had lab with Joe. We did a lot of the same things in lab as in our problem solving class last semester. Most of the labs dealt with logic and we created a logic library that contains or(), and(), not(), xor(),nor(),and nand() function. We then built off of those halfsum(), halfcarry(), and some other functions below is as far as got with the library.

 
#include<stdio.h>
#include<time.h>
 
char PREFFERED = 'i';
char OTHER = 'o';
 
char nott(char a)
{
	char b = '\0';
	if(a == PREFFERED)
		return OTHER;
	else 
		return PREFFERED;	
}
 
char andd(char a, char b)
{
	if(a == OTHER)
	{
		if(b == OTHER)
		{
			return OTHER;
		}
		else if(b==PREFFERED)
		{
			return OTHER;
		}
		else
		{
			return 0;
		}
	}
	else if(a==PREFFERED)
	{
		if(b==OTHER)
		{
			return OTHER;
		}
		else if(b==PREFFERED)
		{
			return PREFFERED;
		}
		else
		{
			return 0;
		}
	}
	else
	{
		return 0;
	}
}
 
char or(char a, char b)
{
	if(a==OTHER)
	{
		if(b==OTHER)
		{
			return OTHER;
		}
		else if(b==PREFFERED)
		{
			return PREFFERED;
		}
		else
		{
			return 0;
		}
	}
	else if(a==PREFFERED)
	{
		if(b==OTHER)
		{
			return PREFFERED;
		}
		else if(b==PREFFERED)
		{
			return PREFFERED;
		}
		else
		{
			return 0;
		}
	}
	else
	{
		return 0;
	}
}
 
char nand(char a, char b)
{
	char c = andd(a,b);
	return nott(c);
}
 
char nor(char a, char b)
{
	char c = or(a,b);
	return nor(c);
}
 
char xor(char a, char b)
{
	char c = nott(b);
	char d = nott(a);
	char e = andd(a,c);
	char f = andd(d,b);
	return xor(e,f);
}
 
char halfsum(char a,char b)
{
	return xor(a,b);
}
 
char halfcarry(char a, char b)
{
	return andd(a,b);
}
 
char FullSum(char a,char b,char c)
{
	char g = xor(a,b);
	char h = nott(c);
	char j = andd(g,h);
	char k = xor(a,b);
	char m = nott(k);
	char n = andd(m,c);
	return or(j,n);
}
 
char FullCarry(char a, char b, char c)
{
	d = andd(a,b);
	e = andd(c,a);
	f = or(d,e);
	g = andd(b,c);
	return or(f,g);
}
 
class RSNandLatch
{
	char R,S,Q,QNot;
	public:
		RSNandLatch(char Q, char QNot);
		char apply(char R, char S,char Q, char QNot);
		char Q();
};
RSNandLatch::RSNandLatch(char Q,char QNot)
{
	Q=OTHER;
	QNot=PREFFERED;
 
}
char RSNandLatch:: Q(void)
{
	return Q;
}
char RSNandLatch::apply(char r, char s,)
{
	if(r!=R && s!=S)
	{
		srand(time(NULL));	
		int rannum = rand();
		if(rannum>=RAND_MAX/2)
		{
			QNot=nand(Q,s);
			Q=nand(QNot,R);
			QNot=nand(R,QNot);
			Q=nand(s,Q);
		}
		else
		{
			Q=nand(r,QNot);
			QNot=nand(S,Q);
			Q=nand(S,QNot);
			QNot=nand(r,Q);	
		}
 
	}	
	else if(r!=R && s==S)
	{
		Q=nand(r,QNot);
		QNot=nand(S,Q);
	}
	else if(s!=S && R==r)
	{
		QNot=nand(Q,s);
		Q=nand(QNot,R);
	}
}

November, 2nd 2013

*Structures and Classes

November, 9th 2013

*Solving Problems in code

Three steps
1. Look at the code and see if you can figure out any error.
2. insert strategic printf statements. This lets you see how your data is changing through out the running of the code.
3. Use the debugger

* The gdb debugger (GNU debugger)

 This is a very useful and powerful tool when it comes to writing code and finding problems in you program. 
 It lets you create break points where the progarma will only run until a certain line. 
 Also you can step into lines of code or next over line. 
 Some of the useful tools i have learned in the gdb debugger.
  * Use -g to compile using the debugger (i.e. gcc -o program program.c -g)
  * To run the debugger just type gdb before the ./ at the prompt. (i.e. gdb ./program)
  * now the prompt should look like this "(gdb)".
  * list - will list your code
  * break line - this will add a break point at an indicated line.
  * run - this runs you code until a break point.
  * print - this will show the memory address of a variable.
  * next - allows you to run one line of code at a time.
  * display - shows the variables at every point. This is very helpful to watch how the variable change as they go through the program.
  * print *() - show the actual data stored in the memory address of a variable.
  * continue - runs the program to the next breakpoint.

November, 16th 2013

*Makefile's

A simple program that lets you compile all your objects as well as other simple tastes written in a scripting type language.
* Variable for C++ is CXX. OBJ tells what objects to compile. BIN names the executable.
* Example code
   all: mydata
   mydata:$(OBJ)
          $(CXX) $(CFLAGS) -o $(BIN) $(OBJ) $(LIBS)
   clean:
         rm -f $(OBJ) $(BIN) core
         
   help:
        @echo "Type some help comments"
   default: $(BIN)
   
* clean removes old objects, help will show comments usually pertaining to the functionality of the makefile.
* running the make is easy at the command 
* just type makefile and after that you can run the executable. In this case with the command ./mydata

November, 30th 2013

*Inheritance

*Polymorphism

*Templates