User Tools

Site Tools


opus:spring2012:jcavalu3:part3

Part 3

Entries

Entry 9: April 26, 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:

  • On this day, I learned how to create nested functions to achieve a set goal.
  • It allowed me to complete a project, as well as showed me what is possible with functions.
  • I had trouble figuring out how to work pointer functions, due to the use of pointers inside of them and passing them to each other.
  • I understand it, now. The use of nested functions should be easier for me, now.

Entry 10: April 26, 2012

  • On this date, I found out that I can use fscanf instead of fgetc to accept character input that I need to change into integer.
  • This allows me to use something I am more familiar with to assign input to values.
  • What doesn't quite make sense to me is the buffer, and how if the buffer has a random value, like enter from the last time it was pressed, it gets printed.
  • The challenge I am facing with the course at the moment is catching up on all of my work!

Entry 11: April 5, 2012

  • On this date, we learned about polymorphism.
  • Polymorphism allows a program to redefine functions and use them for a different purpose.
  • The idea makes sense, what doesn't make as much sense is how to implement it in programming.
  • I am having trouble with C++ and most of what that includes.

Entry 12: April 22, 2012

  • On this date, we learned about Templates.
  • Templates are like blueprints of code. It contains something that can be made but isn't actually made in the template.
  • I'm not sure how I would use this, it seems that it would be important, but how to write it, I have no idea.
  • I think I will try to implement it a few times.

cprog Keywords

Compiler, Preprocessor, Flags, Assembler, Linker, Multi-file programs (how to structure, how to compile)
Namespaces [C++]
Type Casting Operators, Const-Volatility Specifiers (const, volatile) [C++]
Classes (Objects, Constructor, Destructor, Access Control, Public, Protected, Private, Friend, “this” pointer) [C++]
Inheritance (single, multiple Inheritance), Polymorphism/Virtual Functions, Abstract Base Class [C++]
Exception Handing (throw, try, catch) [C++]
Templates, STL (Standard Template Library) [C++]
Structures (Declaration, Accessing Elements, Pointers to)

cprog Keyword 17

Structures (Declaration, Accessing Elements, Pointers to)

Definition

Structures contain public variables held under one name. Each of these variables can be different data types. These variables are known as members.

Demonstration
/*
 *
 * Sample code block
 *
 */
 
#include<stdio.h>
 
int main()
{
        struct person{
                char *name;
                unsigned char age;
                short int weight;
                float gpa;
        };
 
        typedef struct person P;
        struct P p1, p2, p3, p4, p5;
 
        pl.age = 29;
        return(0);
}
cprog Keyword 18

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

Definition

A template is like a blueprint,

Demonstration
/*
 * 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$ 
cprog Keyword 19

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

Definition

A class is similar to a struct. The main difference is that a class can hold both data and functions. Objects are created by using classes. Access control includes the use of the private, public, and protected keywords. They actually determine what can access the variables in the class. Public allows anyone to access those variables, private allows only the members of the same class to access those variables, and protected allows only members of the same class and their friends.

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 <cstdio>
#include "num.h"
 
        class num {
                public:
                        num();
//                      ~num();
                        float average(int, int, int, int);
//                      int sum(int, int, int, int);
                        void set(int,int,int,int);
                        int get(int);
                private:
                        int n1,n2,n3,n4;
                  };
 
        num :: num()
        {
                n1=0;
                n2=0;
                n3=0;
                n4=0;
        }
 
        float num :: average(int a, int b, int c, int d)
        {
                return((float)(a+b+c+d)/4);
        }
 
 
        void num :: set(int a, int b, int c, int d)
        {
                n1=a;
                n2=b;
                n3=c;
                n4=d;
        }
 
        int num :: get(int value)
        {
                int result=0;
                switch(value)
                {
                        case 1:
                                result=n1;
                                break;
                        case 2:
                                result=n2;
                                break;
                        case 3:
                                result=n3;
                                break;
                        case 4:
                                result=n4;
                                break;
                        default:
                                result=-9999;
                                break;
                }
                return(result);
        }
 
 
int main()
{
        num myNum;
        myNum.set(7,0,0,0);
        return(0);
}
cprog Keyword 20

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

Definition

Exceptions, from what I have seen, appear to be a type of debugging that can occur during runtime. The program will run through a try block, and if the exception does not exist, then the program will continue running.

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$ 
cprog Keyword 21

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

Definition

Inheritance allows a program to reuse existing classes in different cases, without actually changing the values in the classes.

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:

The AND file:

#include "gate.h"
#include "and.h"
 
AND::AND()
{
        setA(false);
        setB(false);
        setX(false);
}
 
void AND::process()
{
        if((A==true) and (B==true))
                setX(true);
        else
                setX(false);
}

The NOT file:

#include "gate.h"
#include "not.h"
 
NOT::NOT()
{
        setA(false);
        setX(false);
}
 
void NOT::process()
{
        if(A == false)
                setX(true);
        else
                setX(false);
}

The OR file:

#include "gate.h"
#include "or.h"
 
OR::OR()
{
        setA(false);
        setB(false);
        setX(false);
}
 
void OR::process()
{
        if((A == false) and (B == false))
                setX(false);
        else
                setX(true);
}

The main file:

#include"gate.h"
#include"and.h"
#include"or.h"
#include"not.h"
#include<stdio.h>
 
int main()
{
        AND *myAND = new AND();
        myAND -> setA(true);
        myAND -> setB(false);
        myAND -> process();
        printf("Result of AND is: %d\n", myAND->getX());
 
        OR *yoOR = new OR();
        yoOR -> setA( true );
        yoOR -> setB( true );
        yoOR -> process();
        printf("Result of OR is: %d\n", yoOR -> getX());
 
        NOT *whyNOT = new NOT();
        whyNOT -> setA( false );
        whyNOT -> process();
        printf("Not A is: %d\n", whyNOT -> getX());
        return(0);
}

With the following header files, the C++ files can use the same class file:

The OR header file:

#ifndef _OR_H
#define _OR_H
 
class OR:public gate {
        public:
                OR();
                void process();
};
 
#endif

The AND header file:

#ifndef _AND_H
#define _AND_H
 
class AND:public gate {
        public:
                AND();
                void process();
};
 
#endif

The NOT header file:

#ifndef _NOT_H
#define _NOT_H
 
class NOT:public gate {
        public:
                NOT();
                void process();
};
 
#endif

The most important files of the code, the ones that hold the classes to allow inheritance, the gate.cc file and the gate.h file:

#include "gate.h"
 
gate :: gate()
{
        A=B=X=false;
}
 
//returns X
bool gate :: getX()
{
        return(X);
}
 
void gate :: setA(bool A)
{
        this->A=A; // 'this.' allows you to reference yourself.'this.A' is
                  // the A in the protected part of the class.
}
 
void gate :: setB(bool B)
{
        this->B=B;
}
 
void gate :: setX(bool X)
{
        this->X=X;
}
#ifndef _GATE_H
#define _GATE_H
 
class gate {
        public:
                gate();
                bool getX();// LOOK UP BOOL DATA TYPE!!!!
                void setA(bool);
                void setB(bool);
        protected:
                void setX(bool);
                bool A;
                bool B;
        private:
                bool X;
};
 
#endif
cprog Keyword 22

Compiler, Preprocessor, Flags, Assembler, Linker, Multi-file programs (how to structure, how to compile)

Definition

The compiler turns source code into executable code. The preprocessor takes data that is input into a compiler and returns output that can be used in another program. Multi-file programs take separate files and, when compiled together, take data from each file and run a program.

Demonstration
 
cprog Keyword 23

Namespaces [C++]

Definition

Namespaces allow programs to group classes and objects together and be used under different names.

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$ 
cprog Keyword 24

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

Definition

Type casting allows a program to assign a variable of one data type equal to a variable of another data type, and then forces that value to be the new data type. Type casting operators consist of dynamic_cast, static_cast, reinterpret_cast, const_cast, and typeid. Const-Volatility Specifiers set types as whichever you choose. Const sets the type to const, and volatile set the type to volatile.

Demonstration

Demonstration:

 

cprog Objective

cprog Objective

Distinguish and explain difference between homogeneous and heterogeneous composite data types:

Definition

Homogeneous composite data types consist of created variables with multiple spaces of memory that can either be empty or filled, that are of the same data type. Heterogeneous composite data types consist of very similar variables, except they hold multiple data types.

Method

Some of the various data types that are capable of holding more than one value or space of memory are:

  • struct
  • arrays
  • classes
  • unions

What determines whether or not they are heterogeneous or homogeneous is whether or not they have more than one data type stored in them.

Measurement

Most arrays are of one data type, which is defined when the array is initialized:

   char *array[10];

Unions and Structs are very similar, and they both can have more than one data type. They are seen more often as the group of code that would have more than one data type stored in them.

This is a Union:

        union var {
                int x;
                float f;
        };

What is special about a union is that it holds one block of memory for multiple values. Each time a variable is assigned a value in the code, the old value in the memory is replaced with the new.

Struct:

        struct person{
                char *name;
                unsigned char age;
                short int weight;
                float gpa;
        };

A struct can have more than one block of memory, one for each of the values in the struct.

Analysis

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

  • How did you do?

– From what I was able to type up, it seems that my understanding of it now is pretty good.

  • Is there room for improvement?

– There is room for improvement, I still have a little trouble with creating these and implementing them, but with a little practice, I should be fine.

  • Could the measurement process be enhanced to be more effective?

– It could be, I could have implemented code and shown the result.

  • Do you think this enhancement would be efficient to employ?

– It would be efficient to employ, it would help me understand these concepts more.

  • Could the course objective be altered to be more applicable? How would you alter it?

– It probably could be, in the way that I mentioned.

Experiments

Experiment 7

Question

Will passing by reference work just as well as passing by value? Specifically, would they give me the same result?

Resources

Passing by reference is a method that, when looking at pointers, points one argument at the address of another. Passing by value allows the compiler to copy one or more values in one or more addresses into a function, and allows the function to use these values. The original variables that are used are not changed.

Hypothesis

I believe that they won't give me the same result. What will most likely happen is using pass-by-reference will allow me to manipulate the value given and the value given will be saved, but the value will not be saved as I would have liked with pass-by-value.

Experiment

In my experiment, I will create a running program that will use both methods for the same purpose. The program will have one function for performing passing-by-value; the main function will have the coding for passing-by-reference. I will perform the same change on the variables, and that will determine whether or not the same change takes place for both cases.

Data

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

Code for the experiment:

#include <stdio.h>
#include <stdlib.h>
 
// Function to demonstrate passing-by-value
 
void passByValue( int val1, int val2 )
{
        int pbv1, pbv2;
        pbv1 = pbv1 + 5;
        pbv2 = pbv2 + 5;
}
 
int main()
{
        int pbv1 = 5, pbv2 = 10, pbr1 = 5, pbr2 = 10;
        printf("Values before adding 5:\n");
        printf("a: %d\nb: %d\nc: %d\nd: %d\n", pbv1, pbv2, pbr1, pbr2);
 
        passByValue( pbv1, pbv2 );
        pbr1 = pbr1 + 5;
        pbr2 = pbr2 + 5;
 
        printf("Values after adding 5:\n");
        printf("a: %d\nb: %d\nc: %d\nd: %d\n", pbv1, pbv2, pbr1, pbr2);
        return 0;
}

Results of the experiment:

lab46:~/src/cprog/Opus/Opus3$ ./experiment1 
Values before adding 5:
a: 5
b: 10
c: 5
d: 10
Values after adding 5:
a: 5
b: 10
c: 10
d: 15
lab46:~/src/cprog/Opus/Opus3$ 

Analysis

Based on the data collected:

  • Was your hypothesis correct?

– My hypothesis was correct, they did not have the same change in values.

  • Was your hypothesis not applicable?

– My hypothesis was applicable, the variables that were passed by reference did change, but the variables that were passed by value did not remain changed, they went back to their original values.

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

– Not really, all that I thought would happen, did happen. This just helped me to confirm my understanding of passing variables in different ways.

  • What shortcomings might there be in your experiment?

– I did not print what the value was in the function, so the value may have never changed. I am certain that it did, however.

  • What shortcomings might there be in your data?

– There weren't any, really. Everything seemed to work and was accounted for.

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.

– I can now say that I fully understand the difference between passing-by-value and passing-by-reference. I was confused on what the differences were, and if they were both applicable in any case, but it seems that they aren't and I need to decide when either is more appropriate.

Experiment 8

Question

When using logic operators, can one use more than two (in an if statement)?

Resources

Logic operators consist of and, or, not, is equal to, etc. These logic operators are usually used to check one variable or compare two variables which determine whether or not a piece of code will run or not. These operators are represented, in code, by these characters:

  • and: &&
  • or: ||
  • not or not equal to: !=
  • is equal to: ==

Hypothesis

I don't think that more than two logic operators can be used without any clarification of their separation (parenthesis separating them). I believe that this would cause confusion in the code, and the compiler would not read the code as it was intended to be read, therefore leading to a logical error, but not a syntax error.

Experiment

I am going to create a block of code that will test this. The if block will only run if x and y are greater than 0 or less than 0.

Data

Code for the experiment (the one that separates):

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
        int x = 0, y = 0;
 
        printf("The following program will test to see if the result of two input numbers, when multiplied or divided, \
\nwill return a positive or negative number.\nPlease input the first value: ");
        scanf("%d", &x);
        printf("\nPlease input the second value: ");
        scanf("%d", &y);
 
        if( x == 0 || y == 0 )
        {
                printf("The value is zero.\n");
        }
        else if( ( x > 0 && y > 0 ) || ( x < 0 && y < 0 ) )
        {
                printf("The value will be positive\n");
        }
        else
        {
                printf("The value will be negative\n");
        }
        return 0;
}

The result for this code (with parenthesis as separation) worked and printed this:

lab46:~/src/cprog/Opus/Opus3$ ./experiment2
The following program will test to see if the result of two input numbers, when multiplied or divided, 
will return a positive or negative number.
Please input the first value: -1

Please input the second value: -2
The value will be positive
lab46:~/src/cprog/Opus/Opus3$ ./experiment2
The following program will test to see if the result of two input numbers, when multiplied or divided, 
will return a positive or negative number.
Please input the first value: 1

Please input the second value: 2
The value will be positive
lab46:~/src/cprog/Opus/Opus3$ ./experiment2
The following program will test to see if the result of two input numbers, when multiplied or divided, 
will return a positive or negative number.
Please input the first value: -1

Please input the second value: 2
The value will be negative
lab46:~/src/cprog/Opus/Opus3$ ./experiment2
The following program will test to see if the result of two input numbers, when multiplied or divided, 
will return a positive or negative number.
Please input the first value: 1

Please input the second value: -2
The value will be negative
lab46:~/src/cprog/Opus/Opus3$ 

Code without separation:

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
        int x = 0, y = 0;
 
        printf("The following program will test to see if the result of two input numbers, when multiplied or divided, \
\nwill return a positive or negative number.\nPlease input the first value: ");
        scanf("%d", &x);
        printf("\nPlease input the second value: ");
        scanf("%d", &y);
 
        if( x == 0 || y == 0 )
        {
                printf("The value is zero.\n");
        }
        else if( x > 0 && y > 0 || x < 0 && y < 0 )
        {
                printf("The value will be positive\n");
        }
        else
        {
                printf("The value will be negative\n");
        }
        return 0;
}

The result without separation:

lab46:~/src/cprog/Opus/Opus3$ ./experiment2
The following program will test to see if the result of two input numbers, when multiplied or divided, 
will return a positive or negative number.
Please input the first value: 1

Please input the second value: 1
The value will be positive
lab46:~/src/cprog/Opus/Opus3$ ./experiment2
The following program will test to see if the result of two input numbers, when multiplied or divided, 
will return a positive or negative number.
Please input the first value: -1 

Please input the second value: -1
The value will be positive
lab46:~/src/cprog/Opus/Opus3$ ./experiment2
The following program will test to see if the result of two input numbers, when multiplied or divided, 
will return a positive or negative number.
Please input the first value: 0

Analysis

Based on the data collected:

  • Was your hypothesis correct?

– My hypothesis was incorrect. I should have known, seeing as how C programming is free-form.

  • Was your hypothesis not applicable?

– It wasn't really applicable, I figured it out, and decided to double check my incorrectness-ocity-ism!

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

– Not really with this, it is a simple concept, I just messed up!

  • What shortcomings might there be in your experiment?

– Well, at first, I had an error. I fixed it in the code and it ran properly.

  • What shortcomings might there be in your data?

– There don't appear to be any shortcomings in my data.

Conclusions

Well, my understanding of how free-form coding works seems to have eluded me for a time. I have regained it, and now I shall use that power to further my exploration into the world of programming! YEAH!

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.

– I decided to retest Josh Davis' experiment about multiplying an int value and a float value together. What would be the result?

http://lab46.corning-cc.edu/opus/spring2012/jdavis34/start#experiment_3

Resources

Evaluate their resources and commentary. Answer the following questions:

  • Do you feel the given resources are adequate in providing sufficient background information?

– He said he was able to create and run the program to provide a result for this experiment on his own from what he had learned in class. I believe it. He seemed to know what he was doing.

  • Are there additional resources you've found that you can add to the resources list?

– I would use the C for Engineers class as a resource, because I vaguely remember learning about this, the answer just escapes me.

  • Does the original experimenter appear to have obtained a necessary fundamental understanding of the concepts leading up to their stated experiment?

– I believe he does understand it.

  • If you find a deviation in opinion, state why you think this might exist.

– No deviations in opinion, sir!

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?

– I believe that he has a thorough hypothesis, but I don't think it will go as he plans it to.

  • What improvements could you make to their hypothesis, if any?

– I don't think that there are any improvements that you could make on his hypothesis. It seems very in depth.

Experiment

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

  • Are the instructions correct in successfully achieving the results?

– The instructions appear to be correct.

  • Is there room for improvement in the experiment instructions/description? What suggestions would you make?

– I would suggest that he should have given some sort of result, and spaced it all out, made it a little easier to read. Just small things.

  • Would you make any alterations to the structure of the experiment to yield better results? What, and why?

– I would not!

Data

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

DA CODE:

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
        float x, z1;
        int y, z2;
 
        printf("Please input a number with a decimal value: ");
        scanf("%f", &x);
 
        printf("\nPlease input a whole number: ");
        scanf("%d", &y);
 
        z1 = x * y;
        z2 = x * y;
 
        printf("\nThe result stored in a float variable: %f\n", z1);
        printf("The result stored in an int variable: %d\n", z2);
 
        return 0;
}

DA RESULT:

lab46:~/src/cprog/Opus/Opus3$ ./retest2
Please input a number with a decimal value: 1.2

Please input a whole number: 2

The result stored in a float variable: 2.400000
The result stored in an int variable: 2
lab46:~/src/cprog/Opus/Opus3$ 

Analysis

Answer the following:

  • Does the data seem in-line with the published data from the original author?

– It is hard to determine, because he never actually showed any results. He used a function he created to multiply them together, which is cool, but I didn't do that.

  • Can you explain any deviations?

– Mine was a little more organized and seemed to work better, since he said his did not compile.

  • How about any sources of error?

– There are no errors in mine.

  • Is the stated hypothesis adequate?

– The stated hypothesis is pretty good, had I not known ahead of time, I would have thought the same thing would happen.

Conclusions

Answer the following:

  • What conclusions can you make based on performing the experiment?

– I can conclude that it is not necessary to add a specifier of how many decimals is needed when printing out a float value or assigning a float value.

  • Do you feel the experiment was adequate in obtaining a further understanding of a concept?

– I do believe that this experiment was a good way of further understanding floats, since we didn't work with them much during the course.

  • Does the original author appear to have gotten some value out of performing the experiment?

– It appears that he knew, then, that his understanding of variables and data types was not complete and not as in depth as he thought.

  • Any suggestions or observations that could improve this particular process (in general, or specifically you, or specifically for the original author).

– I have no suggestions.

opus/spring2012/jcavalu3/part3.txt · Last modified: 2012/05/09 21:39 by jcavalu3