User Tools

Site Tools


opus:fall2011:lburzyns:part3

Part 3

Entries

November 3rd, 2011

This week, I did my first project for this course entitled “Exploring Arithmetic Operators”. In this project, I dealt with defining all of the arithmetic operators used in C programming, as well as using all of them in a program I wrote from scratch. In this program, my objective was to write a code that allowed the user to type in any numbers they wanted to add, subtract, multiply, and divide. The program prompted the user to input numbers seperately, and then (based on where the user was in the program), the numbers were either added, subtracted, multiplied, or divided. The program warned the user beforehand what operator was being used. It turned out this project was a success after running a test set of numbers. This was significant because it was my first project submitted in this course, and I found it to be intimidating at first, but then realized it was much easier than I originially thought it would be. This was a good jumping off point, project-wise, for me, because it was relatively simple, yet it dealt with several different aspects of C programming that are essential to being successful in this course.

November 13th, 2011

Today in another computer class I am currently taking this semester (CSCS 1200), we were assigned reading about the world wide web. I found this assignment to be interesting and informative, since this is something that I personally use countless times a day. The world wide web is something that, I believe, people now take for granted and do not fully appreciate the background of the world wide web. For example, CGI (Common Gateway Interface) is a set of standards for how servers can handle a variety of HTTP requests. ASP (Active Server Pages) is an alternative to CGI that runs on server and deals with data submitted by the user. This is the kind of things I found interesting in my reading for the week, since this is something people use every single day, yet most probably have no idea how they access the internet. All people really know is how to click on an icon and type things into Google. These types of topics are things I enjoy learning about in my computer classes (kind of like looking behind the scenes).

November 16th, 2011

Today I completed a project about pointers and arrays. I decided to choose this as a topic for my project so that I could focus more on pointers and how they are used within a program. This topic has given me some problems in the past, and has brought on some confusion, so I took this opportunity to spend some more time on pointers and get some more practice with them. In my reserach for pointers, I came across a website that explained how to use function pointers, which I found to be useful and informative. I learned that function pointers could assist the programmer in using pointers as different behaviors at different times. I also learned how to delcare a pointer using the '*' operator, and I learned about pointer arithmetic (++, –, -=, += ). These topics, along with others I researched to complete my project, were useful and informative, and overall I feel like I have a better grasp on pointers.

November 19th, 2011

This week for my CSCS 1200 class, we were instructed to review the past two months worth of work we have been doing to prepare for a test this coming week. While doing this, I came across a section in my book about object oriented programming, which we also learn about in this class. Examples of object oriented programming include visual basic, java, and C++. In my textbook, there is a helpful diagram to assit the reader in understanding object oriented programming, which basically consists of showing existing objects combining with new objects to form an object oriented program. We also focused on operating systems, which is a set of computer programs that runs the computer hardware and acts as an interface with both application programs and users. Some examples of an operating system is microsoft windows and apple OS X. I found it interesting how these two classes this semester has overlapped in some way, in CSCS 1200 I am able to “go behind the scenes” of the computer, and learn what makes it tick. In this class, we go into deeper topics and look at the details by learning how to write a program. I found it useful to take both classes at the same time, since at one point or another, each class as helped me with the other.

cprog Topics

Namespaces (C++)

Namespaces let the user group variables like classes and functions under a specific name. Under a namespace is all of the declarations needed to complete the program. One identifier listed in a namespace may be used in another namespace, but will not necessarily have the same meaning under the two namespaces. In C++, a namespace is used in a namespace block, and within this block, identifiers can only be used as they are declared. If an identifer is used outside of the namespace block, then the identifier needs to show that it is being used in the namespace by typing “using namespace x;” in front of the identifier.

#include <iostream>
 
using namespace std;
 
namespace x
{
    int a = 4;
}

I/O Streams in C++

Input streams are used to save input from the user, output streams are used to save output for a particular object, such as a file or a printer. This means that the stream saves the information by the user to put into action at a later time when, for example, a printer is done warming up and is ready to print. Some devices are capable of being both input and output sources. Cin: standard input stream; when the user goes to type up a code in C++, “cin” is the object used to input data. Cout: standard output stream; when the user is typing up a program, “cout” is the object used to store the output data Cerr: standard output stream for errors

#include <iostream>
 
  int main() {
      int x;
 
      cout << "enter a number: ";
      cin >> x;
      return(0);
  }

Type Casting Operators C++

Casting means the user changes the definition of a variable by changing its type to a different one. In order to type-cast an object to another you use the traditional type casting operator. This is used in C++ so that the user can make a variable of one type act like another type just for one single operation.

int i;
double d;
 
i = (int) d;

Standard Template Library C++

The standard template library provides C++ users with a specific set of tools that can be used for most types of applications. STL is the C++ standard library that contains four components called algorithms, containers, functors, and iterators. STL is specified by a set of headers, while ISO C++ does not specify headers.

#include <iostream.h>
#include <list.h>     // The STL list class
 
int main () {
     int x;
 
return 0;
}

"This" pointer C++

The “this” pointer is a pointer that can be used only with the nonstatic member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a “this” pointer. “This” pointer stores the address of the class instance, is not counted for calculating the size of the object, and cannot be modified.

#include <iostream>
using namespace std;
class MyClass {        
int data;    
public:       
     MyClass() {data=100;};       
     void Print1();        
     void Print2(); 
 
  void MyClass::Print2() {    
     cout << "My address = " << this << endl;    
     cout << this->data << endl;}

Templates

Function templates are special functions that can operate with generic types, which means this allows the user to create a function template whose use can be changed to more than one type or class without repeating the entire code for each type. In C++ this can be done by using 'template parameters' which is a special kind of parameter that can be used to pass a type as argument.

template <class identifier> function_declaration;
template <typename identifier> function_declaration;

Const-Volatility Specifiers C++

Const defines a type that is constant (as the name suggests), and volatile defines a type that is volatile. Volatile allows access to memory mapped devices, allows the use of variables between 'setjmp' and 'longjmp', and allows the use of 'sig_atomic_t' variables. To use a const, the user declares a constant as if it was a variable but adds ‘const’ before it. The user has to declare it immediately in the constructor because the programmer cannot set the value later (as that would be changing it).

const int Constant1=96; 
volatile REGISTER * io_ = 0x1234;

Function Overloading C++

The user can overload a function by declaring more than one function with the name in the same scope. The declarations of the function must be different from each other by types or the number of arguments in the argument list. When you call an overloaded function, the correct function is chosen by comparing the argument list of the function call with the parameter list of each of the overloaded functions with the same name. The following example is the correct way to not overload a function:

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;
}

Operator Overloading C++

Operator overloading is where different operators have different meanings depending on their arguments. Operator overloading is generally defined by the language or the programmer. To overload an operator means to provide it with a new meaning for user-defined types. Overloadable operators include addition, subtraction, multiplication, and division.

a + b * c
add (a, multiply (b,c))

Abstract Base Class C++

An abstract class is a class that is meant to be specifically used as a base class. An abstract class contains at least one 'pure virtual function'. The user can declare a pure virtual function by using a pure specifier (= 0) in the declaration of a member function in the class declaration.

class AB {
public:
  virtual void f() = 0;
};

Polymorphism/Virtual Functions C++

Polymorphism in C++ means that some code, operations or objects behave differently in different situations. In C++, a type of polymorphism is overloading. The term 'polymorphism' when used in C++, it refers to using virtual methods. In C++ virtual function is a member function of a class, is declared with virtual keyword, and usually has a different functionality in the derived class.

public interface IQuestion
    {
        string Text
        {
            get;
            set;
        }
        ArrayList Choices
        {
            get;
            set;
        }
        void DisplayAccumulatedResult();
    }
namespace std
{
  public class Animal
  {
    public virtual void Eat()
    {
      Console.WriteLine("I eat like a generic Animal.");
    }
  }

Inheritance C++

Inheritance is a way of reusing existing classes without changing them, therefore making relationships between them. Inheritance is almost like embedding an object into a class. Inheritance lets the user include the names and definitions of another class's members as part of a new class. Single inheritance is where a class can only derive from one base class, and multiple inheritance is where the user can derive a class from any number of base classes.

#include <iostream>
using namespace std;
 
class A {
   int data;
public:
   void f(int arg) { data = arg; }
   int g() { return data; }
};
 
class B : public A { };
 
int main() {
   B obj;
   obj.f(20);
   cout << obj.g() << endl;
}

cprog Objective

Objective: Know how to use arrays and pointers

Know how to use arrays and pointers. I focused a lot on arrays and pointers in this past month. An array is useful for one name for a group of variables of the same type, that can be accessed numerically. A pointer “points” to a location in memory. They can make a program more efficient, and can help handle large amounts of data.

Method

Write an example of an array and a pointer in a program(s) to display how both are properly used.

Measurement

This is an example of an array:

#include <stdio.h>
#define  ARR_SIZE  5
 
int main(void)
{
  static float const prices[ARR_SIZE] = { 1.99, 1.49, 13.99, 50.00, 109.99 };
  auto float total;
  int i;
 
  for (i = 0; i < ARR_SIZE; i++)
  {
    printf("price = $%.2f\n", prices[i]);
  }
 
  printf("\n");
 
  for (i = 0; i < ARR_SIZE; i++)
  {
    total = prices[i] * 1.08;
    printf("total = $%.2f\n", total);
  }
 
  return(0);
}

This is an example of a pointer:

#include <stdio.h>
void DoubleIt(int *num)
{
*num*=2;
}
int main()
{
   int number=2;
      DoubleIt(&number);
        printf("%d\n",number);
return 0;
}

Both of these examples are taken from my project on arrays and pointers, found here:

http://lab46.corning-cc.edu/user/lburzyns/portfolio/project2

Analysis

Since I happened to focus on arrays and pointers for the project I completed this week, I decided it would be fitting to choose arrays and pointers as this month's Opus objective. I feel like I have done copious amounts of research regarding these two topics in the past week, and I feel like I have demonstrated them properly in a program. I chose to do two seperate programs to display how to use arrays and pointers properly because it was clearer to me and better for me to understand these two topics if I seperated them and focused on each one individually. I found arrays very simple and easy to use, and actually enjoyed learning about them. Pointers were a little bit more tricky to understand, and have been giving me some trouble for some time now. After focusing on these two topics this week as much as I have, I feel like I have a much better grasp on pointers and I think I can better understand the logic behind them.

Experiments

Experiment 1

Question

What would happen if I were to write a basic for loop program and at the end of the code, have the return be 0?

Resources

http://einstein.drexel.edu/courses/Comp_Phys/General/C_basics/#loops This website was helpful when it came to a basic introduction to loops. It is easy to understand because it takes the user through a line of code step by step. This was great for me because sometimes seeing a whole program and then seeing the explanation either before or after it is difficult for me to follow. This way, I can see what purpose each line of code served when it comes to the program as a whole.

http://computer.howstuffworks.com/c8.htm This website was useful in this experiement because it takes the user through all of the different types of loops C programming has to offer: else, else if, do-while, for loop, etc.

http://www.exforsys.com/tutorials/c-language/decision-making-looping-in-c.html I found this website particularly interesting and helpful because after a program was listed with an explanation, there is also a diagram drawn that helps the user physically see what the loops are doing within a program, rather than try to use your imagination while reading an explanation.

Hypothesis

I expect there to be an error when I try to compile the program listing a return value at 0 at the end of the code. What should be there instead is “getchar();”, not “return 0;”.

Experiment

I am going to write a very small, basic program that allows me to simply modify the code to my experiement, compile & run, and then report the results (along with the code written) here.

Data

Here is the code written:

#include <stdio.h>
 
int main()
{
    int x;
    for ( x = 0; x < 10; x++ ) {
        printf( "%d\n", x );
    }
    return 0;
}

Here is the result when compiled:

lab46:~/src/cprog$ ./loops
0
1
2
3
4
5
6
7
8
9
lab46:~/src/cprog$

Analysis

My hypthesis was incorrect. All adding “return 0” did was exit the program right after it was finished running, rather than wait for the user to exit the program manually. I should have realized that before making my hypothesis, it seems so obvious now that I have written the program and executed it. Had I written a program that allowed for user input, then maybe the return 0 function would have caused an error when I tried to run the program, but that was not the case with this experiment. It turned out that having either “return 0” or “getchar()” at the end of the program did not make a very big difference at all.

Conclusions

I have discovered how to successfully write a simple loop statement in this program, and I did not make any mistakes. The program executed just as I would have liked. After compiling my program, I realized that having a return 0 statement at the end did not make a very big difference to the program, rather it just exited the program for the user instead of waiting for user input.

Experiment 2

Question

How would I use functions to add, subtract, multiply, or divide numbers in a program?

Resources

http://www.cprogramming.com/function.html This website lists a lot of commonly used functions in C and in C++, the user is able to click on each function individually, and an explanation of the function is given, along with an example.

http://www.mycplus.com/tutorials/c-programming-tutorials/functions/ This website takes the user through a step by step introduction to functions by showing the structure, such as the header and the body.

http://computer.howstuffworks.com/c13.htm This website is great for beginners because it makes it very easy for the user to understand how to use functions and why they are used, along with several short examples given.

Hypothesis

After researching how to use arithmetic as a function and not use the arithmetic operators, I believe I can successfully write a program that will add two numbers together (based on user input) by using the “sum” function.

Experiment

I am going to write a small program in putty using the 'sum' function to add together two numbers the user chooses to input, and the output will yield the correct arithmetic.

Data

Here is the code written:

#include <stdio.h>
 
int sum ( int x, int y );
 
int main()
{
  int x;
  int y;
 
  printf( "Please input two numbers to be added: " );
  scanf( "%d", &x );
  scanf( "%d", &y );
  printf( "The sum of your two numbers is %d\n", sum( x, y ) );
  getchar();
}
 
int sum (int x, int y)
{
  return x + y;
}

And here is the output of the compiled code:

lab46:~/src/cprog$ ./functions
Please input two numbers to be added: 5 6
The sum of your two numbers is 11

Analysis

My hypothesis was correct, I was able to successfully compile a code using a function for adding together two numbers based on user input, and the output of the numbers were correctly calculated. I learned that there are many functions used in C and in C++ that have a specific job to perform when called on in the program, and I also learned that a function can be called from another function (in this case, the sum function was called from the main function).

Conclusions

I have discovered that using functions can sometimes be more practical, and can be a bit of a time saver when it comes to certain jobs that need to be performed. In the past, I have done a project on arithmetic operators, and writing the program to add, subtract, multiply, and divide numbers the user inputs can be much longer than just writing a function to perform the same job. I am sure this is not always the case, but in this particular example, it proved to be true. Also in my research, I learned that a function can perform its job independently from the program and does not need interference.

Retest

If you're doing an experiment instead of a retest, delete this section.

If you've opted to test the experiment of someone else, delete the experiment section and steps above; perform the following steps:

State Experiment

Whose existing experiment are you going to retest? Prove 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/fall2011/lburzyns/part3.txt · Last modified: 2011/11/19 19:15 by lburzyns