User Tools

Site Tools


opus:fall2011:lburzyns:part2

Part 2

Entries

October 13, 2011

Today I read a little bit more about binary arithmetic operators. The signs are + (addition), - (subraction), * (multiplication), / (division), and the modulus operator %. The modulus operator prodices the remainder number (if applicable to the math problem) when two numbers are divided together. This operator can only be used on integers and not on floats or doubles. At first, this was difficult for me to understand when I was reading the book on this operaton. I did some online research and then I was able to find some more information on this topic, and I was able to better understand that “modulus” is practically another word for “remainder”. When reading about arithmetic operators, I tried thinking about writing a program almost as if I were on the inside of a calculator: when we put numbers into a calculator and get the result, it happens so easily. It was interesting to think about what goes on “behind the scenes” (so to speak) of everyday machines.

#include <stdio.h>
 
void main()
{
  int total = 45;                    
  int divider = 7;
  int a = 0;    
  int b = 0; 
 
  a = total/divider;  
 
  printf(" total is %d  and  divider is %d", total, divider);
  printf("\n a is  %d.", a);
 
  /*  left over */
  b = total%divider;          
 
  printf("\nThere are %d  left over.\n", b);
}

October 19, 2011

Today I worked on an assignment for another computer class of mine (CSCS 1200) based on programming. This teacher had us download Microsoft Small Basic, which is a very user friendly, first-timer program used to write code. I found this to be extremely helpful (and, obviously, a lot easier to use) because when I began typing a command, such as “TextWindow” and “WriteLine”, Small Basic tried guessing what I was asking of it, and in the toolbar section popped up a helpful aide listing suggestions of what I might be trying to do, along with a description of what the command means. Also, instead of having to save the file in a repository and then write a specific line of code in putty to run the program, there is a simple button that says “run” to click on, and Small Basic runs the program for you instantly, in a seperate window, so it makes things look a lot less jumbled and confusing. I found that Small Basic was just easier on the eyes with a white background in the window and normal black text, or in some cases, different colored text, in which the colors symbolized a certain command. Overall, I found Small Basic a lot easier to work with rather than work with putty, but that was to be expected, seeing as Small Basic is for beginners. I just overall found the similarities and the differences interesting.

October 23, 2011

Today I did an experiment (listed as Experiment 1 below) about arithmetic expressions. I was curious to see if my program would output negative numbers when I subtracted 20 from 10. I did some research and discovered that as long as I used the modulus operator properly in my program, I would receive the desired output, which was a -10. As I was writing my program and attempting to run some tests on it, I discovered how to write a program where the user can input the numbers to get a result, rather than dedicate values to variables. I found this method to be useful, and more fun, because I was able to try different numbers as the input (such as ridiculously high or low numbers) just to play around with the program and make it more interesting. I found this to be a lot more interactive and creative instead of just assigning values to variables and running the program. I also think learning this will be more useful down the road.

October 27, 2011

Today I did an experiment about height. I wrote a program based on user input, which had various outputs depending on what number was entered into the program. If the number was less than 5 (as well as in decimal form, not using ' or “) the output should read “you are short”. If the input was between 5 and 6, the output should read “you are average”, and if the input was greater than 6, the output should read “you are tall”. I enjoyed writing this program because I like writing things that have user interaction, rather than write a code that has assigned values to variables and just compiling and running the program without any interaction by the user. I find this to be more challenging and creative, and I enjoy thinking up situations where user input could be used and different outputs could be given. In this particular program, I focused on using “if” and “else” statements, and I did some research online as well as reading what the book has to say about them in order to properly use them within my program.

cprog Topics

Standard I/O

Standard input is basically whatever the user types into the code from the keyboard. Meaning, whatever text is entered, the program reads and interpretes it. Standard output is typically the screen in the session, which means whatever the program prints from the standard input, it appears in the standard window.

#include <stdio.h>
#include <ctype.h>
 
int main()
{
    int c;
 
    while ((c = getchar()) ! = EOF)
         putchar(tolower(c));
    return(0);
}

Parameters

A parameter is generally used for a variable named in the list in a function definition. An ordered list of parameters is usually included in the definition of a function.

#include <stdio.h>
double sales_tax(double price)
{
    return 0.05 * price;
}

Return Types

A return statement causes the execution to leave the function and start again at the point in the code right after where the function was called. A return type is usually set at zero in C programming and set at the end of the written program. The zero return value from main generally means that a normal program exit is represented. Non zero values indicate unusual or wrong termination conditions.

#include <stdio.h>
power(base, n)
int base, n;
{
     int i, p'
 
     p = 1;
     for (i = 1; i <= n; ++i)
          p = p * base;
    return p;
}

Recursion Function

Recursion is a programming technique that allows the user to express operations in terms of themselves. It is similar to a loop because it repeats the same code, but it requires passing in the looping variable. This function can simplify some tasks.

void recurse()
{
  recurse(); //Function calls itself
}
 
int main()
{
  recurse(); //Sets off the recursion
}

C Library

The C library consists of a set of sections of the ANSI C standard which describes a collection of library routines used to fulfill common operations, such as input/output and string handling. This is essential in C because anything and everything that follows the listed C library in the program references the library in order to understand what the user wants the program to output. stdio.h is the C library to perform I/O operations.

#include <stdio.h> /* this is the C library listed for this program */
 
int main()
{
    printf("The C Library is essential to any code\n");
    return(0);
}

Compiler/Linker

A compiler, or a linker, is the program that generates the executable file by linking (which essentially means several object files are merged to create one executable file). A compiler is important because it translates human readable source code into machine code. It is the “middleman” between humans and machines. In the following code example, the compiler searches for a definition of the function “printf” and finds it in the header file, which is ”#include<stdio.h>“, and then creates a file successfully.

#include<stdio.h> 
int main() 
{ 
printf("Lauren"); 
return 0; 
}

Preprocessor

A preprocessor is a program that processes its input data to produce an output that will eventually be used as the input data to another program. When it comes to the C preprocessor, the '#' sign is usually followed by the preprocessor. The following example shows the most common use of a preprocessor, which is to include another file in a program. The preprocessor replaces the line ”#include <stdio.h>“ with the header file of that name, which declares the printf() function (as well as declaring other things).

#include <stdio.h>
 
int main (void)
{
    printf("Hello, world!\n");
    return 0;
}

MakeFiles

A makefile is a way of associating short names with a series of commands to execute when called upon to do so by the user. The makefile has instructions for a specific project. The default name of the makefile is literally 'makefile', but the name can be specified with a command-line option.

project.exe : main.obj io.obj
	tlink c0s main.obj io.obj, project.exe,, cs /Lf:\bc\lib 
main.obj : main.c
	bcc –ms –c main.c

Source Code

Source code is a language written by the user in a program, which is then translated to binary code that the computer can directly read and execute to perform whatever task the user requests of the program.

#include <stdio.h>
 
int main()
{
        int counter = 0;  
        int numTimes = 0; 
 
	cout <<  "How many times do you want to see Lauren?: ";
	cin >> numTimes;  
 
	cout << "\n";
 
	while (counter < numTimes) 
	{
		cout << "Lauren!\n";
		counter++; 
	}
 
	cout << "\n";
 
    return(0);
}

Object Code

Object code is the binary representation of the assembly output of the compiler. It is the way the computer represents programs internally after receiving input from the user in source code.

color c = color(0);
float x = 0;
float y = 100;
float speed = 1;
 
void setup() {
  size(200,200);
}
 
void draw() {
  background(255);
  move();
  display();
}
 
void move() {
  x = x + speed;
  if (x > width) {
    x = 0;
  }
}

Libraries

A library is a collection of resources used to develop software. These may include pre-written code and subroutines, classes, values or type specifications. A library is essential to be listed at the beginning of a code in order for the computer to understand what the user is asking to be compiled. It is also possible for the user to create their own library using their own names or shortcuts, as long as each name has a definition. The following default example states “stdio.h” as the library used in the specific code.

#include <stdio.h>
 
int main()
{
    printf "hello world\n";
    return(0);
}

Compiling Multi File Programs

In order to compile a program that has been divided into multiple source files, you would simply list all of the files the user wished to compile after typing the command “gcc -o”. If the user wished to name a file to create after compiling all of the files listed, they would simply type the name of the file the user wished to create after “gcc -o” and before all of the file names being compiled.

gcc -o executable sourcefile_1.c sourcefile_2.c sourcefile_3.c
 
gcc -o mydb main.c keyboard_io.c db_access.c sorting.c

cprog Objective

Objective

Understand the difference between procedural and object-oriented languages. The idea behind procedural programming is to disect a programming task into variables and data structures. Object-oriented programming disects a programming task into classes.

Method

Doing some online research, as well as textbook research, to determine the similarities and differences between procedural programming and object-oriented programming.

Measurement

A significant differnece between procedural programming and object-oriented programming is that procedural programming uses procedures to operate on data structures, object-oriented programming combines the two together so an object, which is an example of a class, operates on its own data structure. Object-oriented programming focuses on representing real world object problems and their behavior. Procedural programming deals with representing solutions to problems using procedures, which are collections of code that run in a specific order. One thing to remember is that object-oriented programming and procedural are two ways of representing problems to be solved, and it does not matter which language is used. Basically, object-oriented programming languages can be used for procedural programming while procedural languages can sometimes be used for object-oriented programming.

Analysis

I think with a little research on the internet, as well as in the textbook, I was able to differentiate between object oriented programming and procedural programming successfully. It was a bit difficult to wrap my mind around the two topics at first, especially when the topics don't deal with something that is experienced by the user when physically writing a program. For instance, it is easier to understand what an array or string is when writing a program because I can see the topic being put to use. The measurement process could be more effective if the user were to cite examples. To say “demonstrate” the difference between object-oriented programming and procedural programming is a little vague: it is easier to understand what is being asked if it were to say “give examples” or “explain the difference” instead of the word “demonstrate”. I felt that word left the objective open to interpretation, which I took to mean “explain”. On the other hand, leaving the objective open ended could be useful to see different types of responses from different people.

Experiments

Experiment 1

Question

What would happen if I were to add 2 numbers together that would receive a negative number output?

Resources

http://www.trunix.org/programlama/c/kandr2/ This website explains basic arithmetic operations, and mentions using the modulus operator when expecting a negative answer as a result

http://publications.gbdirect.co.uk/c_book/chapter2/expressions_and_arithmetic.html This website explains the use of double and float variables when it comes to arithmetic expressions

Hypothesis

Based on my research, if I use the proper modulus operator in my program, I will be able to get a negative result if I subtract 20 from 10.

Experiment

I am going to write a program in putty declaring x and y as integers with the values of 10 and 20, and then write a small program subtracting the two from each other to hopefully yield a result of -10.

Data

Code written:

#include <stdio.h>
 
int main()
 
{
 
        int firstNum, secondNum, delta = 0;
        printf("Enter first number (a): ");
        scanf("%d", &firstNum);
        printf("Enter second number (b): ");
        scanf("%d", &secondNum);
        delta = firstNum - secondNum;
        printf("a - b = %d\n", delta);
        return 0;
}

The following code shows the entry of numbers I put in when prompted, and the output:

lab46:~/src/cprog$ ./numbers
Enter first number (a): 10
Enter second number (b): 20
a - b = -10

Analysis

My hypothesis was correct in guessing that I would receive a negative output when I properly inserted an arithmetic expression in my program. I found it a lot easier to assign my integers with the value of 0, and then input numbers myself when prompted to do so by the program instead of assigning values to each integer and then writing an arithmetic expression from there. Also, it was more fun to play around with the program and enter different numbers rather than just writing a program where the integer values were restricted to what was assigned by the user.

Conclusions

I discovered that when the modulus operator is used properly within a program, the output will yield the result expected. This also goes for any other arithmetic expression: whether it be addition, subtraction, multiplication, or division, when used properly within a program, and having the proper integers assigned in the first place, the expected results will be obtained. Also, when I went into this experiment, I did not anticipate writing a program where the user inputs the values of the integers at the prompt. Instead, I had originally planned on assigning values to the integers and then writing an expression to yield a result. As I was writing the code, I decided to challenge myself a little more and go a step further.

Experiment 2

Question

If I write a program that allows the user to input their height, will I get the correct response when I enter in various heights?

Resources

http://cboard.cprogramming.com/c-programming/92579-usefulness-else-if-statement.html I looked at this message board for some tips on the “else if” statements, how to use them, and how they can be useful in programs. I referenced this person's program on age and changed mine to height and added more options to the outputs. http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_025.htm This website is useful when it comes to different uses for “else if” statements, and explains how to use them properly by citing several different examples.

Hypothesis

I believe (as long as I write the code correctly) that I will receive the dedicated output when I enter in various heights into my program when prompted to do so. By doing some reserach on “else if” statements, as well as reading what is in our textbook, I believe I will be able to compile together a simple code that allows the user to enter their height (as long as the input is “5.3” and not “5'3””) and a specific statement about their height will be the output.

Experiment

I am going to write a simple program in putty that allows the user to enter their height in decimal form to receive a specific output based on their height.

Data

This is the program I wrote:

#include <stdio.h>
 
int main ()
{
        int height;
 
        printf("Please enter your height");
        scanf("%d", &height );
        if ( height < 6 ) {
                printf ("You are average height\n");
        else if ( height > 5 )
                printf ("You are average height\n");
        else if ( height < 5 )
                printf ("You are short\n");
        else if ( height > 6 )
                printf ("You are tall\n");
        }
return 0;
}

This is my input and result:

lab46:~/src/cprog$ ./height
Please enter your height5.3
You are average height
lab46:~/src/cprog$

Analysis

My hypothesis was correct in assuming that when I entered my height of 5'3“ (in decimal form, 5.3) I would receive the output “You are average height”. If I were to input anything lower than 5, the output should be “You are short” and if I were to input anything greater than 6, the output should be “you are tall”.

Conclusions

I conclude that as long as the “if” statements are used properly within a program, the output based on the input should be correct. I also have a great deal of fun compiling programs based on user input, rather than just assigning concrete values to variables and running a program without any interaction.

opus/fall2011/lburzyns/part2.txt · Last modified: 2011/10/31 04:05 by lburzyns