User Tools

Site Tools


opus:fall2011:lburzyns:part1

Part 1

Entries

August 29, 2011

Today, I accessed my Lab 46 class email, as well as joined the CPROG mailing list. This was significant because most of the communication for this class will be done through this email, whether it be between students or important information from the teacher to the students. Joining the CPROG mailing list was significant to me because it was the first time I was able to full experience the lab 46 email functions. It was the first email I received on this account, and it was the first time i sent an email from that account as well. I took this opportunity to play around with the different options given, such as learning how to 'delete', 'save', and 'send'. At first, I did not realize the '^' symbol really meant to press 'ctrl' (and then the command key). I struggled with this for a while until finally, after some experimenting, was able to successfully send and delete emails. When I first tried setting up my lab 46 email, I felt overwhelmed and immediately assumed since I was not understanding everything all at once that I was behind in the class. After re-reading some information and experimenting, I realized that I was not behind and that I may have been just stressed and a bit confused by new things. I am looking forward to using the putty program more and getting more experienced.

August 30, 2011

Today I accessed the class IRC, which is more or less a chat room for the students in the class. This was significant because I was able to see what my fellow classmates were struggling with by reading what questions they had, and I also learned some things by reading the answers given by others, probably more experienced, than myself. This aid is going to be useful to me when I have questions for others regarding assignments or a topic given to us to read about. I almost see this chat room as a classroom setting: if we were to have this online class in person, this chat room would be what the every day setting would be like, with the questions and answers given, as well as everybody's personalities shining through and getting to know some people. I was having trouble at first trying to figure out how to close the screen and exit the chat room, but after doing some research and reading, I was able to figure out what I was doing wrong.

September 19, 2011

Today I read about how to write basic lines of code using C. I learned that it is important to define variables at the beginning of the code, so when a variable is used, it has a meaning that the compiler can refer back to in order to make sense of the code. I also learned how to create a comment in a line of code, which is basically for use of the programmer. A comment is more like a note that the programmer can refer back to at a later date as a reminder for something. This is an important tool to learn how to use because the compiler does not read comments in a line of code and try to print them, it is basically disregarded. I learned that escape sequences in a line of code tell the complier to do a specific task, such as create a new line of code at the end of a command. All of these commands are important to learn because they are basics when it comes to writing code, and will be used frequently.

September 25, 2011

Today I wrote my first code in putty, just the basic “hello, world” code from our textbook. I was very excited and proud of myself when I successfully completed this task. I needed assistance in getting to the screen which allows us to write code in putty. Once I got step by step instructions on how to do this, and once I successfully executed this task, I felt motivated and excited to do more. When I logged into putty, at the command prompt I typed “cd ~/src/cprog”. From there, I created a new source file to add to my repository by typing “svn add hello.c”. Once this was created, I went into the nano editor by typing “nano hello.c”. From there, I was able to write the 'hello, world' code given in the textbook. Once finished, I saved the code. Then when I went to compile the program, a the command prompt I typed “gcc -o hello hello.c”, then to run the compiled program i typed “./hello”. When all of this was done, I was given the output of “hello, world” successfully, without any errors.

Topics

Variables (types, ranges, sizes)

A variable is a word given with a specific value, listed before the code, that the compiler can reference when running lines of code. char: character, 1 byte short int: short integer, 2 bytes int: integer, 4 bytes long int: long integer, 4 bytes bool: boolean value, 1 byte float: floating point number, 4 bytes double: double precision floating number, 8 bytes long double: long double precision floating number, 8 bytes

int a;
int b;
int c;

Pointers (address of, assignment, dereferencing)

A pointer is a data type used in C that holds the address when a value is stored in the memory. So basically, instead of a pointer containing any actual data or a value, it “points” to the memory location in which the value, or data, can be found.

#include <stdio.h>
void main()
{
    int nNumber;
    int *pPointer;
 
    nNumber = 15;
    pPointer = &nNumber;
}

“String”, Format/Formatted Text String

A character string is a command sentence, given in double quotes, that lets the program know what characters, or words, the user wants to print when the program is run.

The format parameter is followed by a string of characters that contains the text to be written. The number of arguments following the format parameter should contain at least as many as the number of format tags

  int printf ( const char * format, ... );

Arithmetic (equations, operators)

Arithmetic Operators are used to perform some mathematical operations like addition, subtraction, multiplication, division, etc. Normally, these operations are performed on an expression. + : additive operator - : subtraction operator * : multiplication operator / : division operator % : remainder operator

#include <stdio.h>
 
     main()
   {
     int fahr, celsius;
     int lower, upper, step;
 
     lower = 0;  
     upper = 300;   
     step = 20;      
 
     fahr = lower;
     while (fahr <= upper) {
         celsius = 5 * (fahr-32) / 9;
         printf("%d\t%d\n", fahr, celsius);
         fahr = fahr + step;
     }
   }

logic and operators (and, or, not, xor)

Logic operators compare boolean expressions and give a boolean result. The AND (&&) operator performs logical conjuction between 2 boolean expressions. If both expressions are evaluated to be true, then the AND operator will give the result 'true'. The OR ( || )operator performs logical disjunction between 2 boolean expressions. If one or the other expressions evaluate to true, then the OR operator will result in 'true', if neither of the expressions evaluate to true, then the result given is 'false'. The XOR operator performs logical exclusion on two expressions. If only one expression evaluates to true, then the XOR operator will result in 'true'. If both of the expressions evaluate to either true or false, then the XOR operator will result in 'false', just as long as both expressions evaluate the same. The NOT (!) operator performs logical negation on a boolean expression.

#include <iostream> 
 
using namespace std;
 
int main()       
{
  !(5 == 5)    // evaluates to false because the expression at its right (5 == 5) is true. 
  !(6 <= 4)    // evaluates to true because (6 <= 4) would be false. 
  !true        // evaluates to false
  !false       // evaluates to true. 
}

Type Casting

Typecasting is making a variable of one type, (for example, an int), perform like another variable, (for example, a char), for one single operation.

#include <iostream> 
 
using namespace std;
 
int main()       
{
  cout<< (char)65 <<"\n"; 
  // The (char) is a typecast, telling the computer to interpret the 65 as a
  //  character, not as a number with a value.  It is going to give the character output of 
  //  the equivalent of the number 65.
  cin.get();
}

Scope (Block, Local, Global, File)

The scope of an identifier is the biggest part of the program text where the identifier can possibly be used to reference it's object. A local scope can be used in a block and in blocks enclosed within that block, but the name must be declared before the lines of code before it is used, otherwise the program will not run correctly. A name has file scope if the identifier's declaration appears outside of any block. Global scope is the namespace scope of a program that occurs on the outside, in which objects, functions, etc. can be defined. A name has global scope if the identifier's declaration appears outside of all blocks.

int x = 4;             /* variable x defined with file scope */
long myfunc(int x, long y); /* variable x has function       */

Arrays (standard notation, pointer arithmetic)

An array is a data structure, which is a specific way of storing information in a computer, which entails a selection of variables, each of which is identified by one integer. Pointer arithmetic can be used to access an array or act like an array.

#include <stdio.h>
 
   int main()
   {
     short age[4]; 
     age[0]=23;
     age[1]=34;
     age[2]=65;
     age[3]=74;
    return 0;
    }

Multi-dimensional arrays

Multi dimensional arrays are simply several arrays put into one array, most commonly multi dimensional arrays are 2 arrays put into one. Multi dimensional arrays are not limited to two arrays into one, but the more arrays used means more memory is used as well. When creating a multi dimensional array, we must use two nested brackets. This gives us a counter variable for every column and every row in the arrays.

#include <stdio.h>
 
int main()
{
    int[][] myArray = { {0,1,2,3}, {4,5,6,7} };  );
}

typedef, enum, union

Unions are a structure that is used to define a data type with more than one field, and each field takes a separate storate location. A union defines a single location that can be given many different field names. Typedef is a statement that allows the user in C++ to define their own variable types, instead of being restricted to the basic variables that have been taught. Enum stands for enumerated data type that is designed for variables that contain only a limited set of values. These values are referenced by the compiler as name, and assigns each name an integer value

union value {
long int i_value; // Long integer version of value
float f_value; // Floating version of value
}
 
typedef int day_of_the_week; // Define the type for days of the week
const int SUNDAY = 0;
const int MONDAY = 1;
const int TUESDAY = 2;
const int WEDNESDAY = 3;
const int THURSDAY = 4;
const int FRIDAY = 5;
const int SATURDAY = 6;

Command-line arguments

A command line argument can be defined as any parameter that is typed after the name of a program

In the example: dir *.txt

'dir' would be the name of the program to run, and '.txt' would be the command line argument

Header Files (Local and System)

A header file is a file containing C declarations to be shared between several source files. In order to reference a header file in your code, you must “include” it by writing '#include' at the front of each source file. System header files declare the interfaces to parts of the operating system. The user includes them in the program to supply definitions and declarations needed to make system calls and libraries. In C, it is common to see header files names that end with .h .

#include <filename>
 
#include <stdio.h>

Objectives

Objective 1: Knowing the difference between structures and classes

The main difference between structures and classes is that structures have public access and classes have private access. This essentially means that the user who wrote the code chooses to have certain variables private, usually a value is given, and when it is referenced within a line of code, the variable itself will not appear, but the value will appear. If the user defines a structure and then delcares an object of that structure using the word “class”, the object is still public.

Method

Write an example of a structure and a class

Measurement

#include <stdio.h>
 
struct point {
     int x;
     int y;
}
int main()
{
     x = 1;
     y = 2;
 
     return 0;
}
class Box
{
  double Length, Width, Height;
  char Color[12];
  float Boxsize;
};

Analysis

I attempted to write very simple, short lines of code for examples of each: a structure and a class. I simply tried to show examples of how each variable should be declared in a program. When it comes to the example I listed for a class, ideally the program would give results for the length, width, height, color, and size of a box when each were defined within the lines of code. In my structure example, I tried to introduce a structure declaration (which is a list of declarations enclosed in braces). I believe the measurement process could definitely be enhanced to prove a more effective example, and maybe after having more experience with these variables, I eventually will be able to do so without the risk of error. I attempted to simply describe and show very brief declarations of the use of a structure and a class.

Experiments

Experiment 1

Question 1

What would happen if I were to write a simple code in C, and leave out the header file? What is the importance of the header file?

Resources

http://gcc.gnu.org/onlinedocs/cpp/Header-Files.html This website has a brief description of what a header file is, as well as how to include it into a code. In C, it is typical to have a header file that ends with “.h”. The declarations listed in the header file can be used in more than one source file, and it is easier to just include the header file at the beginning of the code instead of copying the header file into each source code that needs it.

http://www.suite101.com/content/c-header-files-a2936 This website describes how to create a separate source code file to contain a function that provides additional processing capabilities. In these situations, it is required to provide a user defined header file so that the compiler knows how to call this function.

Hypothesis

I believe if I remove the header file in a simple code (in this case, I will use the “hello, world” code given in the textbook), I will get an error message when I try to run the program.

Experiment

I am going to go to putty, create the source file hello.c, write the code (which is given to me in the textbook), and leave out the header file. I will save this code, compile, and run the program to see what happens.

Data

This is the code I entered into putty:

main ()
{
     printf("hello, world\n");
}

After I saved, compiled, and tried to run this program without the header file, this is the result I got:

lab46:~/src/cprog$ gcc -o hello hello.c
hello.c: In function 'main':
hello.c:5: warning: incompatible implicit declaration of built-in function 'printf'
lab46:~/src/cprog$

Analysis

My hypothesis was correct in guessing that I would receive an error message when I tried to run a program without the header file. This means that the program did not know what I wanted from it when I asked it to print the words “hello, world” without the declaration, or definition, given for “printf”, which would have been accessed from the header file, which I left out of the code. Without previously definied variables, which are stored in the header file, the code cannot execute any commands given by the user, since it does not understand what I am asking of it. It is like trying to learn Spanish without a Spanish to English dictionary: the computer had nothing to reference when the command was given.

Conclusions

I have learned that header files are essential when creating a code. If the user expects the code to run properly, it is necessary to add a header file to the code so the compiler understands the commands given by myself and what I want from it. If the compiler does not know what “printf” means, then it will not print the words for me (in this case, 'hello, world'). If printf is not previously declared or defined, the program is doomed to fail.

Experiment 2

Question

What will happen if I write a code in C and I do not put the “\n” after the printf argument? What is it's purpose, why is it so important?

Resources

http://www.cplusplus.com/reference/clibrary/cstdio/printf/ This website shows an example of a short code written, and then the same code compiled and what it should look like when properly done. The code has several uses of “\n” and the result. http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/V40F_HTML/AQTLTBTE/DOCU_003.HTM This website explains the necessity behind the newline character, and gives a brief definition.

Hypothesis

I believe I will get an error when I try to run a code without the newline character present, because when the code is run, the compiler will not be able to determine where the command ends and what the user wants.

Experiment

I am going to write the “hello, world” code into putty, and I will leave out the newline command, then run the program.

Data

When I removed the newline command from the program and then tried to run the program, I did not receive an error message, but this is what resulted:

lab46:~/src/cprog$ ./hello
 
hello, worldlab46:~/src/cprog$

Analysis

My hypothesis was not exactly correct, I had guessed that I would receive an error message when I tried to run the program without the newline character. Instead, the program ran the output of “hello, world” but did not leave any space between the output and the command prompt, as shown above. In a way, I was correct when I said the compiler would be confused by my code, but it still ran the program, it was just a jumbled mess instead of an error message. Looking back, it makes sense why the program would have tried to run instead of producing an error message, which is what I originally thought. Now that I have seen what happens when you do not add the newline character, I understand how the compiler would still run the code, it just (simply put) was not neat and tidy had I added the newline character. This being the first code I wrote in C, I was unsure of what the results would yield, even when one of the most basic commands was used improperly.

Conclusions

I can conclude from my findings that even though the newline character is important for clarity and neatness, it is not 100% essential when running a program. It definitely makes the code easier to read, but leaving the character out of the code does not prevent the compiler from running it successfully, it would just take exra time to read it, given that the lines of code would be all unorganized.

Experiment 3

Question

In the “Hello, world” program, what would happen if I added the character “\c” in the printf argument?

Resources

Up to this point in the textbook, we do not know what the character “\c” stands for, or what it does. That being said, I did some research on the internet about the printf arguement. http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.cmds/doc/aixcmds4/printf.htm This website explains and gives examples of the printf command. Essentially, the printf command simply writes an output given by the user in a line of code. http://linux.about.com/library/cmd/blcmdl1_printf.htm This website clearly lists and explains several characters and commands commonly used with the printf argument

Hypothesis

I believe I will receive an error message when I try to run the program with a “\c” randomly placed within the lines of code. I think this because I have tried researching on Google what the “\c” character means and what it does, but I was unable to find anything about it.

Experiment

I am going to test this hypothesis by editing my “hello, world” program in putty, insert a “\c” after the esacpe command, and running the program.

Data

lab46:~/src/cprog$ gcc -o hello hello.c
hello.c:5:9: warning: unknown escape sequence '\c'
lab46:~/src/cprog$ ./hello
hello, world
c lab46:~/src/cprog$

Analysis

I was wrong in assuming I would receive an error message, instead I noticed that the program ran smoothly and the output was the same “hello, world”. The difference I noticed was that the line after the output from the code, I saw that the command prompt was preceeded with a “c”.

Conclusions

I can conclude that the “\c” is not a variable in need of a definition within the lines of code, but it is a command for the following line of code. I am unsure what the character actually means when it comes to running a program with this character within it.

opus/fall2011/lburzyns/part1.txt · Last modified: 2011/09/29 06:08 by lburzyns