User Tools

Site Tools


opus:spring2012:sswimle1:part3

Part 3

Entries

Entry 9: April 25, 2012

Wanted to revisit structs and unions from a class we had this semester.

When creating a struct you are making a record for a set of labelled objects into a single structured object.

Example:

struct sales {
    int month sales;
    char *item_name;
    char *item_type;
    float totals;
    };

This is an example of a struct declaration, which is a list of fields that can contain any types (int, char, etc…) and allocates a total amount of storage based on the sum requirements for all of the list types included, along with any internal padding.

A union is nearly the same as a struct except for the way a union allocates memory, each data type in the union starts from the same location in memory instead of having its own location like in a struct.

The concept of a union is to save space the multiple data types inside the union are being condensed into the same memory location.

Example:

union <name>
{
    <datatype>  <1st variable name>;
    <datatype>  <2nd variable name>;
    .
    .
    .
    <datatype>  <nth variable name>;
} <union variable name>;

Entry 10: May 1, 2012

Since we talked about inheritance at length this semester I will refresh on that with this journal entry.

Inheritance in programing has to do with the way classes relate to each other, for example there are two main types of classes in respect to programming inheritance parent classes and child/children classes.

Parent classes are the preceding classes that came before their children a lot like it would seem. Parent classes are also sometimes called superclasses, ancestor classes or base classes.

Child classes or subclasses are derived from their parent class and live in a hierarchy based system.

The application of using inheritance in programming is to relate two or more classes together.

Entry 11: May 8th, 2012

Figured I would write this opus entry on Header Files since that seems to be one of the thing I do grasp.

There are a few different instances of header files

There is the standard Library of header files which are already made files saved that included commonly used declarations that a program might need, for instance stdio.h which includes common input and output functions used in c programming and stdlib.h which includes standard collection of functions collected into a library file.

There are also header files that you can make on your own for instance if you wrote a function saved it and then included it in another function as a .h file that program will now use all the logic from the other instance your wrote with just the file included at the start of the new file as a .h file.

Entry 12: May 8th, 2012

Just going to scribble some things down here for the last entry as I go through some of the programs we accomplished in class this semester.

int main ( ) anything inside the parenthesis are parameters for main and the first parameter of main must be an integer. when on a line of code if you type a \ followed by enter the syntax is that the line will continue onto the next line below it but the program will read it as a single line of code even though for your viewing pleasure its broken up into more than one line of text. int sum(int, int, int, int); is a function prototype.

and so on…

cprog Keywords

cprog Keyword 17

I/O Streams (cin, cout, cerr, stream operators) [C++]

Definition

I/O Streams in C++ is an standard included library, the library consists of (basic class templates, class template instantiations, standard objects, types, and manipulators).

cin - is an object of the class of istream that notates the standard input stream, corresponding to cstdio stream. cin like most systems defaults its standard input from the keyboard.

cout - is an object of the class of ostream that notates the standard output stream, corresponding to cstdio stream.

cerr - is the standard output stream for errors.

cprog Keyword 18

Namespaces [C++]

Definition

Namespaces give groups like classes and functions a more global scope, for instance if you had two int's in your program but you wanted each to have its own global scope you could name one namespace first and the other namespace second like in the example shown below.

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:

// using
#include <iostream>
using namespace std;
 
namespace first
{
  int x = 5;
  int y = 10;
}
 
namespace second
{
  double x = 3.1416;
  double y = 2.7183;
}
 
int main () {
  using first::x;
  using second::y;
  cout << x << endl;
  cout << y << endl;
  cout << first::y << endl;
  cout << second::x << endl;
  return 0;
}
cprog Keyword 19

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

Definition

casting operators remove c syntax inherent for the purpose of making the types more compatible with c++, there are several casting operators.

dynamic_cast - conversion of polymorphic types.

static_cast - conversion of non polymorphic types.

const_cast - removes the const, volatile, and _unaligned attributes

reinterpret_cast - simple reinterpretation of bits.

safe_cast - produce verifiable MSIL.

cprog Keyword 20

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

Definition

A class allows you to put functions inside of a data structure for the purpose of organization and ease of use.

A constructor is a function that can be included within a class that can be automatically called whenever a new object is created within that class for the reason of avoiding returning unexpected values.

Destructors are called automatically when an object is destroyed with the purpose of releasing previously allocated memory.

Access Control breaks down into the Public, Protected, and Private data type sub class definitions.

Public variables and data types are accessible to the program outside of the class

Protected are accessible but may not be changed

Private variables and data types are only available inside that specific class they are defined in and may not be acted upon by the outside program.

this pointer can only be used as a nonstatic member function of a class, struct, or union.

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:

class example{
    public:
        int a;
        int b;
        int c;
        int d;
    private:
        int e;
        int f;
    protected:
        int g;
};
 
cprog Keyword 21

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

Definition

Inheritance in object oriented programming is used to create a relationship between two or more classes.

when using single class inheritance it means pretty much what it sounds like a sub class can only inherit from one super class or parent class.

and obviously multiple inheritance means that you can inherit information in your subclasses from more than one parent class.

Polymorphism with pointers using the ability that a pointer to a derived class is then compatible with a pointer to it's base class also.

Virtual members or functions are members of a class that can be changed in a derived class.

An abstract class is designed to be a base class that contains a pure virtual function.

Demonstration
class A 
{ public:
   void DoSomethingALike() const {}
};
 
class B : public A 
{ public:
   void DoSomethingBLike() const {}
};
 
void UseAnA(A const& some_A)
{
   some_A.DoSomethingALike();
}
 
void SomeFunc()
{
   B b;
   UseAnA(b); // b can be substituted for an A.
}
cprog Keyword 22

Overloading (Functions, Operators) [C++]

Definition

Overloading means that you are assigning more than one name for the same function or operator in the same scope.

Demonstration

Example I found showing Function Overloading.

#include <iostream>
using namespace std;
 
void print(int i) {
  cout << " Here is int " << i << endl;
}
void print(double  f) {
  cout << " Here is float " << f << endl;
}
 
void print(char* c) {
  cout << " Here is char* " << c << endl;
}
 
int main() {
  print(10);
  print(10.10);
  print("ten");
}

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

 Here is int 10
 Here is float 10.1
 Here is char* ten
cprog Keyword 23

Exception Handing (throw, try, catch) [C++]

Definition

An exception in programming is when a program has a situation that has an unexpected circumstance that happens in that section of the programs code. To handle these exceptions their are programming keywords in place like (throw, try, and catch).

catch is used as a preemptive guard against exceptions.

throw is used as a handler after and exception has occurred and tells the program to get rid of or throw the bad segment or unexpected circumstance for the section of code.

try is used as a handler for after an exception is realized as well and instead just telling the program to toss the bad line or code out it gives an instance of instead to try.

cprog Keyword 24

Templates, STL (Standard Template Library) [C++]

Definition

The Standard Template Library is made of predefined class for C++ and provides components called algorithms, containers, functional, and iterators.

cprog Objective

I already filled this out twice

Experiments

Experiment 7

Question

What will happen if i replace a for loop with a while loop in my program. as shown here

#include<stdio.h>
#include<stdlib.h>
 
int main(int argc, char **argv) //parameters are in the ( ), first parameter of main must be an integer
{
        unsigned char i;
 
        if(argc<2)
        {
                printf("%8s must be run with 1 or\
 more arguments, you only provided %hhu\n",*(argv+0),(argc-1));
                exit(1); // \ followed by enter allows you to continue code on new line
        }
        printf("You ran this program with %hhu arguments, they are:\n",(argc-1));
        for(i=1;i<argc;i++)
        {
                printf("argv[%hhu]: %s\n",i,*(argv+i));
        }
        return(0);
}

I am going to replace the for loop in this program with a while loop and see what happens.

Resources

lab46

Hypothesis

I do not have a clue what it will do. My best guess is break something.

State your rationale.

Seems like something that would break something.

Experiment

lab46

Data

lab46:~/src/cprog$ nano sample1.c
lab46:~/src/cprog$ gcc -o sample1 sample1.c
sample1.c: In function 'main':
sample1.c:15: error: expected ')' before ';' token

Analysis

Based on the data collected:

  • Was your hypothesis correct?

Yep it broke

  • Was your hypothesis not applicable?

it was

  • Is there more going on than you originally thought? (shortcomings in hypothesis)

no I think it just produces a syntax error

  • What shortcomings might there be in your experiment?

none

  • What shortcomings might there be in your data?

none

Conclusions

I broke the program you can not just simply make a for loop a while loop

Experiment 8

Question

I am going to try and add a simple multiplication function to the file sample2.c where we had 4 int declared and the program calculating a sum and average.

Resources

lab46

#include<stdio.h>
#include<stdlib.h>
 
int sum(int,int,int,int); //function prototype
int average(int,int,int,int);
int main()
{
        int a,b,c,d;
        int avg;
        a=b=c=d=0;
        avg=0;
        printf("Enter first value: ");
        fscanf(stdin, "%d",&a);
 
        printf("Enter second value: ");
        fscanf(stdin, "%d",&b);
 
        printf("Enter third value: ");
        fscanf(stdin, "%d",&c);
 
        printf("Enter fourth value: ");
        fscanf(stdin, "%d",&d);
 
        fprintf(stdout,"the sum of %d, %d, %d, and %d is:%d\n",a,b,c,d,sum(a,b,c,d));
        fprintf(stdout,"the average of %d, %d, %d, and %d is:%d\n",a,b,c,d,average(a,b,c,d));
        return(0);
}
 
 
int sum(int n1, int n2, int n3, int n4)
{
        int total=0;
        total=n1+n2+n3+n4;
        return(total);
}
int average(int n1, int n2, int n3, int n4)
{
        int average=0;
        average=(n1+n2+n3+n4)/4;
        return(average);
}

Hypothesis

I am going to try and follow the code above and add a similar instance for multiplication to see if i can get it to work I assume this will be easier than project 2's concept.

State your rationale. project 2 was dealing with specific allocated 4 digit numbers and something like that i don't know this just looks like it is going to make more sense to me and maybe work.

Experiment

lab46

Data

lab46:~/src/cprog$ nano sample2.c
lab46:~/src/cprog$ gcc -o sample2 sample2.c
lab46:~/src/cprog$ ./sample2
Enter first value: 2
Enter second value: 2
Enter third value: 2
Enter fourth value: 2
the sum of 2, 2, 2, and 2 is:8
the average of 2, 2, 2, and 2 is:2
the product of 2, 2, 2, and 2 is:16
lab46:~/src/cprog$

Analysis

Based on the data collected:

  • Was your hypothesis correct?

Yeah it worked amazingly.

  • Was your hypothesis not applicable?

for once yes.

  • Is there more going on than you originally thought? (shortcomings in hypothesis)

no

  • What shortcomings might there be in your experiment?

no

  • What shortcomings might there be in your data?

no

Conclusions

It worked I am pretty much pro.

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/sswimle1/part3.txt · Last modified: 2012/05/09 22:42 by sswimle1