User Tools

Site Tools


opus:spring2012:cforman:part3

Part 3

Entries

Entry 9: April 5, 2012

Today we learned about logic gates and classes. We used classes to allow us to create separate files each containing a part of the program. This allows for organization and simple finding because instead of having to search a single program you can just look for the file of which one would hope was named after what it does. Ex. got a function that checks two variables for if they are both true. you would name this separate file “and.cc” not “phill” as “and.cc” is actually what it checks for.

Entry 10: April 10, 2012

Today i learned that their is actually a changeable file for the vi system. This was really cool for me because one of my biggest problems with vi is that it was to dull and sometimes hard to read sometimes after staring for a while. This also was cool because it demonstrates that once again everything and every program is a file.

Entry 11: April 24, 2012

Templates - a tool for generic programming in c++ They allow for a generic representation of a process that gets specific when you use it. Its kind of like a stencil for a program. A stencil can fit different types of coloring utensils so in that aspect a template can support different types of variables from chars to ints.

this includes April 30 - May 9, 2012 (the opus does not allow me to edit the fourth journal day so i am putting it here)

procrastination kills. Let this be a warning unto myself if i pass this class i will be happy. The issue is that i keep putting off this class work in order to allow myself the ability to finish other work and running out of time with this class. New priority this stuff is my life nuff said i need to focus on my programming because this will be my future. i also have really bad eye irritation and need some gunnar glasses so this doesn't hurt so much.

Entry 12: April Day, 2012

This is a sample format for a dated entry. Please substitute the actual date for “Month Day, Year”, and duplicate the level 4 heading to make additional entries.

As an aid, feel free to use the following questions to help you generate content for your entries:

  • What action or concept of significance, as related to the course, did you experience on this date?
  • Why was this significant?
  • What concepts are you dealing with that may not make perfect sense?
  • What challenges are you facing with respect to the course?

Remember that 4 is just the minimum number of entries. Feel free to have more.

cprog Keywords

Functions, Parameters(pass by: value, Address, Reference), Return Types, Recursion

Functions are something a programmer will use everyday. We use them and create them whenever we do something with a program. Each program we make is essentially a function, whether we pass it parameters or not its a function and can be adjusted to work inside any other function. example in C

 #include <stdio.h>
  2 #include <stdlib.h>
  3 int subtraction(int *a1, int *a2,int);
//I then call this function called "subtraction" later on in my devision part of the program and this is used as a function. 

266 int subtraction(int *arr1, int *arr2,int sizea)
267 {
268     int l,i,a,b,c,p;
269     l=i=a=b=c=p=0;
270     while(!(*arr1 <= 0))
271     {
272         i=sizea-1;
273         for(l=sizea-1;l>=0;l--)
274         {
275
276             a = arr1[i];
277             b = arr2[l];
278             if( a >= b )
279             {
280                 a = a - b;
281             }
282             else if( (a < b)&&(arr1[i-1]>0))
283             {
284                 c = arr1[i-1];
285                 c = c - 1;
286                 arr1[i-1] = c;
287                 a = a + 10;
288                 a = a - b;
      }
290             arr1[i] = a;
291             i=i-1;
292         }
293         p=p+1;
294     }
295     return(p);
296 }

what this did was take information given and send it to a function that would act within the program and would return a result. It does not always have to return anything but this one did. The one thing you may notice is that i sent something into the subtraction program. I am sending paramaters for the program to act on. Two ways they can be sent is by value and by reference. Value is when we send the function a straight up variable, this is when we sent it “int sizea” that was by value. The next kind i used was by reference “int *arr1” this was sending it the array called “arr1” and sending all the pieces of it not just one part. The star declares that it is by reference. The next kind is when we pass something by address which is when we send the computer a hexadecimal digit representing a particular zone in memory.

The return type can be seen within the program. Line 295 above shows that I am returning the variable “p” which is an int. You can return anything back to the original but you can only return one thing to it so be specific on what you want to send out. There is no limit to what you can send out besides the fact that it must be one thing(can be int, char, long int, void, float ex…).

Recursion— bassically something that will run itself over and over again… here is an example of code from http://www.cprogramming.com/tutorial/lesson16.html

#include <iostream>

using namespace std;

void recurse ( int count ) // Each call gets its own count
{
  cout<< count <<"\n";
  // It is not necessary to increment count since each function's
  //  variables are separate (so each count will be initialized one greater)
  recurse ( count + 1 );
}

int main()
{
  recurse ( 1 ); //First function call, so it starts at one        
}
• Compiler, Preprocessor, Flags, Assembler, Linker, Multi-file programs (how to structure, how to compile)
Definition

Compiler is a program that will take your script and convert it into runnable code. gcc or g++ ( one i used recently “gcc -g -o bignum bignum.c” )

Preprocessor allows for the use of header files and checks for errors during before compilation to make sure the program will run. -Wall can be added to the line above to allow the preprocessor to check for warning and not just flags.

Flags are the easiest things the the preprocessor can spot because they are errors like missing “;” or trying to use a char as an int or something that the user may mistake and can be solved easily.

The Assembler is the part of the computer that takes a program written in source code and compiled then translated int to assembly language and then to matching code so that it can be used.

The linker is the final part of the compiler where it takes the source codes from multiple pieces and combines them into one executable file. Usually we have only used it to take one source code and create one program but it can do this to many pieces of source code and combine them into one executable.

Multi-file programs - usually its best to create a separate directory for a multi filed program as it is easier to work with.

lab46:~/src/cprog/c++/sampleprogs/abtractbaseclass$ ls
abstractbaseclass.cc  and.h  friends  gate.h  main.cc  nand.cc  nand.o  or.h  origabstractclass.cc  xor.cc  xor.o
and.cc                and.o  gate.cc  gate.o  main.o   nand.h   or.cc   or.o  program               xor.h
lab46:~/src/cprog/c++/sampleprogs/abtractbaseclass$

to compile this we first take the original say “gate.cc” and extract the definitions form it and make them into a header file “gate.h” after that we can convert the .cc file into a .o by using “g++ -c gate.cc” then to combine all the .o files we use “g++ -o program and.o gate.o main.o nand.o or.o xor.o ”

• Classes (Objects, Constructor, Destructor, Access Control, Public, Protected, Private, Friend, “this” pointer) [C++]
Definition

Classes allow for data to be sent in and sent out it makes it easy to define variables in multiple parts of functions like the program in the keyword above uses classes to allow for easy creation of variable so that the same data doesn't have to repeated a hundred times.

objects in a class are the pieces that are being used in the other functions in relation to that class.

Constructor creates the object for the moment that it is called and after its use is deconstructed by the deconstructor when no longer needed.

Access control can be summed up by what the child program cant do and can do. the child program is the program that uses objects created by the class. Remember the child can access its parents public and protected objects but it cannot touch its parents privates.

Public - is data that any part of the program can access and manipulate.

protected - is data that only designated parts can access but usually allows for all child programs.

private - is only data accessible within the class itself and cannot be accessed by any other part unless you got a friend.

friend - creating a friend is like giving a person the permission to stick his hand into your parents private when related to programming. A friend can bypass the security of a class and access the private parts of the class so the child program can use that data.

“This” pointer type - from lack of a better way to put it “ The keyword this identifies a special type of pointer. Suppose that you create an object named x of class A, and class A has a nonstatic member function f(). If you call the function x.f(), the keyword this in the body of f() stores the address of x. You cannot declare the this pointer or make assignments to it.” by http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr035.htm

#include <iostream>
using namespace std;

struct X {
private:
  int a;
public:
  void Set_a(int a) {

    // The 'this' pointer is used to retrieve 'xobj.a'
    // hidden by the automatic variable 'a'
    this->a = a;
  }
   void Print_a() { cout << "a = " << a << endl; }
};

int main() {
  X xobj;
  int a = 5;
  xobj.Set_a(a);
  xobj.Print_a();
}

from same place

• Inheritance (single, multiple Inheritance), Polymorphism/Virtual Functions, Abstract Base Class [C++]
Definition

Inheritance - is when you create a child program it receives the data from the class and uses it as its own.

 class NAND:public GATE{
  7     public:
  8         NAND();
  9         void process();
 10 };

multiple inhertance is like inheritance of a single class but from multiple parents to one child …. yes there can be more then 2 parents. http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm (Table below)

class A { /* ... */ };
class B { /* ... */ };
class C { /* ... */ };
class X : public A, private B, public C { /* ... */ };

Polymorphism - takes the idea of a type pointer in one class and turns it back on the parent class as they share the same type pointer as the parent. script from http://www.cplusplus.com/doc/tutorial/polymorphism/ because ours was really long and i missed that day so i have photos and not program.

// pointers to base class
#include <iostream>
using namespace std;

class CPolygon {
  protected:
    int width, height;
  public:
    void set_values (int a, int b)
      { width=a; height=b; }
  };

class CRectangle: public CPolygon {
  public:
    int area ()
      { return (width * height); }
  };

class CTriangle: public CPolygon {
  public:
    int area ()
      { return (width * height / 2); }
  };

int main () {
  CRectangle rect;
  CTriangle trgl;
  CPolygon * ppoly1 = &rect;
  CPolygon * ppoly2 = &trgl;
  ppoly1->set_values (4,5);
  ppoly2->set_values (4,5);
  cout << rect.area() << endl;
  cout << trgl.area() << endl;
  return 0;
}

An abstract base class is like “class person” with the objects that define it more as age and height. its non-specific or vague concept

Overloading (Functions, Operators) [C++]
Definition

when you overload a function you have two functions with the same name but different parameters and by calling that program it will dicide which one to choose based on the parameters that you feed it.

when you overload an operator you

Demonstration

Demonstration of the chosen keyword.

If you wish to aid your definition with a code sample, you can do so by using a wiki code block, an example follows:

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    return(0);
}

Alternatively (or additionally), if you want to demonstrate something on the command-line, you can do so as follows:

lab46:~$ cd src
lab46:~/src$ gcc -o hello hello.c
lab46:~/src$ ./hello
Hello, World!
lab46:~/src$ 
Templates, STL (Standard Template Library) [C++]

A template is like a stencil or outline of a program. difference is a template is not specific until it is given data then it will choose which program to run that matches the template.

 template <class T>
  2 T larger(T x, T y)
  3 {
  4     if (x >= y)
  5         return (x);
  6     else
  7         return(y);
  8 }
  9
 10  int main()
 11 {
 12     int a=12, b=17;
 13     float c=3.14,d=1.59;
 14     char e=65, f='A';
 15
 16     cout<<larger(a,b)<<endl;
 17     cout<<larger(c,d)<<endl;
 18     cout<<larger(e,f)<<endl;
 19 return(0);
 20 }
 21
 22

STL - contains many of the basic algorithms and outlines for the programs used in C++

• Type Casting Operators, Const-Volatility Specifiers (const, volatile) [C++]
Definition

type casting operators -

Demonstration

Demonstration of the chosen keyword.

If you wish to aid your definition with a code sample, you can do so by using a wiki code block, an example follows:

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    return(0);
}

Alternatively (or additionally), if you want to demonstrate something on the command-line, you can do so as follows:

lab46:~$ cd src
lab46:~/src$ gcc -o hello hello.c
lab46:~/src$ ./hello
Hello, World!
lab46:~/src$ 
• Exception Handing (throw, try, catch) [C++]
Definition

an exception is a way of dealing with odd circumstances in our programs by surrounding a block of text in the block called “try”

int main()
{
   try
   {
      throw 10
   }
   catch(int k)
   {
     printf("an exception has happened %d\n",e);
   }
   ...

the idea of using this is to have the throw be within your program inside of try so you want to put your whole program in try. Then the variables that are getting lost you check with catch( variable type) and then output what is getting lost or if it isnt.

1
2
3
4
5
6
7
8
9
10
11

	

try {
  try {
      // code here
  }
  catch (int n) {
      throw;
  }
}
catch (...) {
  cout << "Exception occurred";
}

cprog Objective

cprog Objective

State the course objective

Definition

In your own words, define what that objective entails.

Method

State the method you will use for measuring successful academic/intellectual achievement of this objective.

Measurement

Follow your method and obtain a measurement. Document the results here.

Analysis

Reflect upon your results of the measurement to ascertain your achievement of the particular course objective.

  • How did you do?
  • Is there room for improvement?
  • Could the measurement process be enhanced to be more effective?
  • Do you think this enhancement would be efficient to employ?
  • Could the course objective be altered to be more applicable? How would you alter it?

Experiments

Experiment 7

Question

in an while statement can i subtract 48 from /n in the controller of the loop?

Resources

none previous knowledge

Hypothesis

Yes, I should be able to treat the variables like they were somewhere else and not special.

Experiment

I am going to use a while loop and i wanted to subtract 48 from the numbers because they were still in ASCII so i did now it wont see the \n that finishes it i will change the end loop variable to '\n'-48 and hope it works

Data

while(g!='\n')
 52         {
 53             g = fgetc(stdin);
 54             g = g-48;
 55             arr2[f] = g;
 56             f = f + 1;
 57         }

while(g!='\n'-48)
 52         {
 53             g = fgetc(stdin);
 54             g = g-48;
 55             arr2[f] = g;
 56             f = f + 1;
 57         }

this worked

Analysis

Based on the data collected:

  • Was your hypothesis correct?
    • yes
  • Was your hypothesis not applicable?
    • yes
  • Is there more going on than you originally thought? (shortcomings in hypothesis)
    • not that i can tell but not perfect with loops
  • What shortcomings might there be in your experiment?
    • my lack of knowledge of loops

Conclusions

This works and is a great way to help with math functions in a program.

Experiment 8

Question

What is the question you'd like to pose for experimentation? State it here.

Resources

Collect information and resources (such as URLs of web resources), and comment on knowledge obtained that you think will provide useful background information to aid in performing the experiment.

Hypothesis

Based on what you've read with respect to your original posed question, what do you think will be the result of your experiment (ie an educated guess based on the facts known). This is done before actually performing the experiment.

State your rationale.

Experiment

How are you going to test your hypothesis? What is the structure of your experiment?

Data

Perform your experiment, and collect/document the results here.

Analysis

Based on the data collected:

  • Was your hypothesis correct?
  • Was your hypothesis not applicable?
  • Is there more going on than you originally thought? (shortcomings in hypothesis)
  • What shortcomings might there be in your experiment?
  • What shortcomings might there be in your data?

Conclusions

What can you ascertain based on the experiment performed and data collected? Document your findings here; make a statement as to any discoveries you've made.

Retest 3

Perform the following steps:

State Experiment

Whose existing experiment are you going to retest? Provide the URL, note the author, and restate their question.

Resources

Evaluate their resources and commentary. Answer the following questions:

  • Do you feel the given resources are adequate in providing sufficient background information?
  • Are there additional resources you've found that you can add to the resources list?
  • Does the original experimenter appear to have obtained a necessary fundamental understanding of the concepts leading up to their stated experiment?
  • If you find a deviation in opinion, state why you think this might exist.

Hypothesis

State their experiment's hypothesis. Answer the following questions:

  • Do you feel their hypothesis is adequate in capturing the essence of what they're trying to discover?
  • What improvements could you make to their hypothesis, if any?

Experiment

Follow the steps given to recreate the original experiment. Answer the following questions:

  • Are the instructions correct in successfully achieving the results?
  • Is there room for improvement in the experiment instructions/description? What suggestions would you make?
  • Would you make any alterations to the structure of the experiment to yield better results? What, and why?

Data

Publish the data you have gained from your performing of the experiment here.

Analysis

Answer the following:

  • Does the data seem in-line with the published data from the original author?
  • Can you explain any deviations?
  • How about any sources of error?
  • Is the stated hypothesis adequate?

Conclusions

Answer the following:

  • What conclusions can you make based on performing the experiment?
  • Do you feel the experiment was adequate in obtaining a further understanding of a concept?
  • Does the original author appear to have gotten some value out of performing the experiment?
  • Any suggestions or observations that could improve this particular process (in general, or specifically you, or specifically for the original author).
opus/spring2012/cforman/part3.txt · Last modified: 2012/05/09 23:26 by cforman