User Tools

Site Tools


opus:fall2013:bg016372:start

Brandon Gates' fall2013 Opus

C/C++

Introduction

Hello, I am back at CCC after about a 8 year break. I am a math/science major, but I have always been interested in computer science so I took problem solving last semester and C this semester as electives. I do not really see myself working as a programmer in the future, but I feel that it is good to have some programming skills for any career in the sciences.

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.

  • gcc = GNU Compiler Collection..
  • ls = lists whats in a directory.
  • cd = changes directory.
  • ./a.out = runs the program after it is compiled.
  • ssh - allows remote connection to lab46 or other systems from an Unix terminal.
  • chat - once logged into lab46 “screen -r” will open the class chat.

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

  • hg - allows you to make changes to you repository.
    • hg commands:
    • hg push - pushes changes to your repo.
    • hg status - shows which files are being tracked.
    • hg commit - commits changes to your repo, and allows you to make comments to go with your changes.
    • hg pull - this will pull changes from your repo.
    • hg update - this is used after you do “hg pull” to update the file with changes.

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

  • First take a look at the statement “int main(int argc, char** argc)
    • argc - argument count
    • argv - argument vercos
  • Some sample code playing with these arguments
  • printf(“\targv[%d]:%s\n”,i,argv[i]);
  • Since argv is actually an array when imbedded in a for loop this program will show the number of aguments and what array position they are in.

* If Statements- VERY IMPORTANT, NOT IF LOOP.

  • must have one “if”, can have only one “else” and zero or more “else if” statement.

*Loops

  • Common Loops - “Do While”, “While”, and the “For” loop.
  • For loop example: for(i=0,1<argc,i++)
  • This means the loop starts at zero then loops until 1 < argc and in steps of i = i+1.
  • Some other common ways to increment:
  • i–: i=i-1
  • i+=1: i=i+1
  • i*=1: i=i*1

September 24, 2013

*Arrays

  • homogeneous composite variable modifier type int. - Scalor.
  • All data must be the same data type. Arrays have to be all one kind of data. Structs will be used when different data types are needed.
  • int b[3]
    • initializes an array with data type int and 4 units long.
    • b[0] - *(b+0)
    • b[1] - *(b+1)
    • b[2] - *(b+2)
    • b[3] - *(b+3)
  • Creating a triangle using an array and the gd.h library.
  • sample code
     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]);
  • when using the gd.h make sure to link when compiling using -lgd.

* gd.h

  • Library used to create images - not native on all unix systems.
  • Makes creating images easier using a x, y coordinate system.

October, 2nd 2013

*Functions

  • Functions are sub programs that help organize code.
  • Most programs will be comprised of many functions rather than one long main.
  • Having the program split up into smaller pieces makes it easier to deal with and see what is happening.
  • Functions are very helpful, they can be reused to to reduce redundant code. We have been using functions with out even knowing it.
  • Things such as printf() and scanf() are functions that we have been using on a regular basis.
  • Functions can return values, but they don't have to. You will declare a function accordingly.
  • If the function does not return any value you will declare the function as void, otherwise you will declare the function its return type.
    • Example of no return value “void functionname(datatype argument/arguments)”
    • Example of a return type int “int functionname(datatype argument/arguments)”
  • The arguments are the variables that you will be passing into the function and they also need there data type declared in the function declaration.
  • The “return” keyword is used to tell the function which value to return to whatever called the function. This could be another function or main, which is actually a function.

October, 10th 2013

More function fun and preprocessor directives.

  • Preprocessor directive
  • most common is the #include. This lets an user include header files.
  • This basically takes whatever is in the file you are including and puts it right in you program.
  • Using angled brackets such as <stdio.h> has the complier look for the file in the usual include pathway. Using double quotes such as “stdio.h” includes the home directory in the search.
  • #define is a preprocessor directive that allows declaration of global variables.
  • The next kind of preprocessor directive we will run into are if-else directives, these are used for conditional compiling. some examples are #if, #ifdef, #else #endif.
  • Pointers
  • A pointer is a number that shows the memory address of a variable.
  • An asterisk (*) is used when declaring a pointer and is placed before the variables name.
  • Its important to remember that when using a pointer you are only looking at were the variable is stored, not the actual value of that variable.
  • The (&) is used when assigning values to a pointer. It is the 'address of' operator.

October, 17th 2013

Passing by value compared to passing by address/reference

  • Passing by value
  • Perseveres the value of the variable/variables.
  • Example:“getval(a,c)” This would in when the function is called. “int getval(int top, int bot)”, function declaration.
  • The values of a/top and c/bot would be preserved.
  • Passing by address/reference
  • Example “getval(&a,c)” “int getval(int *top, int bot)”
  • Here the memory address of a is being copied into top.
  • If you modify 'top' then 'a' will change.

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

  • You can declare a class as a struct or as a class both struct and class are keywords in C++.
  • Clases are made up of data members(i.e. variables) and functions which are also called methods.
  • Data Members
    • A class can have static and non static data members. If the variable is static then every object shares a single copy.
    • Nonstatic data members have a copy for every object in the class.
    • Data members are declared just like variables in a function, usually in the private access level.
  • Member Functions
    • Member functions can be public, private, or protected.
      • Public functions are available to everything.
      • Private functions are only available to the class.
      • Protected functions are available to derived classes as well as the class.
    • The scope operator (::) is used to to define a member function outside the class definition.
      • example- “void classname::function name(var1,var2)”
    • Constructors
      • Must have the same name as the class and cannot have a return type.
      • Are used to initialize variables and assign memory in order to avoid returning unexpected values.
      • Automatically called every time a new object of its class is created.
    • Deconstructors
      • Performs the opposite of the constructor.
      • Also has the same name as the class but with a ”~“ before the name and cannot have a return type.
      • Its is automatically called whenever an object is destroyed.

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

  • Inheritance between classes allows for a class to have sub classes. The sub class inherits the members of the main class called the base class.
  • Declaring sub classes: class mainclassname: public subclassname.
  • Subclasses can inherit from not only the public of the main class but also from the protected of the main class. Yet subclasses do not have access to the private of the main class.
  • Classes are able to inherit from more than one class.

*Polymorphism

  • Polymorphism allows for the creation of virtual functions.
  • Must distinguish between the declared type of the object, or pointer and the actual type at runtime.
  • Declared Type called the static type.
  • Actual Type called the dynamic type.

*Templates

  • Templates make variables generic.
  • Declaring a templete: “template <parametr-list> declaration
  • Like a function parameter a template parameter has an optional name and default argument.
opus/fall2013/bg016372/start.txt · Last modified: 2014/01/19 02:56 by 127.0.0.1