Corey Forman's 2012 Spring Opus
My life in a kernel when dealing with c/c++
Hello, wellcome to my opus. Here you will find a compilation of data that I have collected and compiled on different topics within the c/c++ world. I am an american who wishes he were British in terms of the acsent. I like anime like Naruto Shipuden amd Bleach. I am a huge League of Legends fan. This may be my last semester at CCC and i will be transfering to Liberty University to complete my degree in computer sciences. Well once again welcome to my opus and enjoy and remember pirating is bad so don't tell anyone you do it lol.
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:
Remember that 4 is just the minimum number of entries. Feel free to have more.
Today I was working on a program that could pop pictures up on all the screens in the room. The program didn't work but instead it pushed the processing use over 4,600% it was epic. I will not be using it again. We also learned about pointers and pointers that point to pointers.
Today we learned about arrays. when we run a program the data is stored in an array. the first part is the programs name. the parts that follow are the arguments that affect the command. Arrays can be accessed and specific information can be retrieved from them. These are really helpful because they allow the user to store information easily and retrieve specific data easily.
We talked a lot about project one which is a cipher decipher program. We talked about how everything to the computer is technical a number and with that being true numbers can be manipulated. This is a great concept because it allows for so really cool programming tricks. Considering that all letters are numbers then you can better use letters in you program. For example of a letter being a number …. z is the same as 122 and a is the same as 97.
Standard input and output is simple but complicated. The most obvious of what it can do is simple. Receive data and output data. Even that can be put onto a new level depending on your knowledge of Standard I/O. For now just the basics. The computer can receive input from some source(keyboard, other programs, and files). standard output is also just where a program sends its data. “fscanf” is used to grab infromation from the user input using the keyboard. “fprintf” is used to output data onto (well usually) the screen. STDERR is also like STDOUT but instead of outputting user generated data it tends to output when the program fails in some way. Segmentation faults and compilation problems tend to be standard errors.
#include <stdio.h> #include <stdlib.h> int main() { FILE *in,*out; int value=0; in=fopen("file.txt","r"); out=fopen("out.txt","w"); if(in==NULL) { printf("ERROR!\n"); exit(1); } fscanf(in,"%d",&value); while(value!=-1) { value *= 2; fprintf(out,"%d\n",value); fscanf(in,"%d",&value); } fclose(in); fclose(out); return(0); }
What this code does is creates a seemingly random number then has the user input their guess. the guess is compared to the actually number and then the program outputs the results. As seen in the program the terms “fscanf” and “fprintf” are used. Not only that but the terms in and out are used along with the proper “fscanf” with in and the other with “fprintf”
Header files are used in a program to give the user the ability to use certain commands in his program. There are two types. The first is “local header files” these files are programs you have created or data compilations you wish to use in your current program. The other type of header file is the “system header files.” These are instituted into the program using the command “#include <blah blah>” the blah blah is the header file you wish to use.
The C Standard Library is a compilation of both header files and all the possible commands one can use in their programs. It is the programming language of C.
Libraries are simply other compilations of commands which make up other languages for programming.
Essentially these are the different mathematical symbols and how they function within the program. This is easier to show then explain. This table is grabbed from http://www.tutorialspoint.com/ansi_c/c_operator_types.htm
Operator Description Example + Adds two operands A + B will give 30 - Subtracts second operand from the first A - B will give -10 * Multiply both operands A * B will give 200 / Divide numerator by denumerator B / A will give 2 % Modulus Operator and remainder of after an integer division B % A will give 0 ++ Increment operator, increases integer value by one A++ will give 11 -- Decrement operator, decreases integer value by one A-- will give 9 Operator Description Example == Checks if the value of two operands is equal or not, if yes then condition becomes true. (A == B) is not true. != Checks if the value of two operands is equal or not, if values are not equal then condition becomes true. (A != B) is true. > Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true. < Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true. >= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true. <= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true. && Called Logical AND operator. If both the operands are non zero then then condition becomes true. (A && B) is true. || Called Logical OR Operator. If any of the two operands is non zero then then condition becomes true. (A || B) is true. ! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(A && B) is false.
logic operators can be used to define exactly what you want you program to respond to and not to respond to. They can be used to make sure if statements only act when certain requirements are met. exp. if( (i =2)&&(x=2) ); the program will only run said if statement if both x and i are equal to 2.
and is && or is || not is ! xor is ^ or ^^ i am not sure
Variables in this case are just different data types. Each data type has a purpose. Char is made for characters. Int is used for small numbers. and long long int is used for very long numbers. The following are results from a program that takes the range and size of each datatype.
an unsigned char is 1 bytes The range of an unsigned char is 0 to 255 An unsigned char can store 256 unique values A signed char is 1 bytes The range of a signed char is -128 to 127 A signed char can store 256 unique values an unsigned short int is 2 bytes The range of an unsigned short int is 0 to 65535 An unsigned short int can store 65536 unique values A signed short int is 2 bytes The range of a signed short int is -32768 to 32767 A signed short int can store 65536 unique values an unsigned int is 4 bytes The range of an unsigned int is 0 to 4294967295 An unsigned int can store 4294967295 unique values A signed int is 4 bytes The range of a signed int is -2147483648 to 2147483647 A signed int can store 4294967296 unique values an unsigned long int is 8 bytes The range of an unsigned long int is 0 to 18446744073709551615 An unsigned long int can store 18446744073709551615 unique values A signed long int is 8 bytes The range of a signed long int is -9223372036854775807 to 9223372036854775806 A signed long int can store 18446744073709551615 unique values an unsigned long long int is 8 bytes The range of an unsigned long long int is 0 to 18446744073709551615 An unsigned long long int can store 18446744073709551615 unique values A signed long long int is 8 bytes The range of a signed long long int is -9223372036854775807 to 9223372036854775806 A signed long long int can store 18446744073709551615 unique values
A scope is something that is only functional within its area. Block scope are pieces of code that fall between these {}. They are main blocks of code and there can be many of these in a single function. Local Scope is when you have a code block but inside you have another and there is a variable in it that was created and is only usable within the separate code block inside the original.if { blah blah blah){ int i} if this is in a block of code then the int i is part of a local scope. File block is anything declared outside of normal blocks of code. These are accessible anywhere. The final Scope is Global. These are things declared outside of code and can be used in your code. Usualy called upon before you begin you code ( before you input int main() )
Type Casting is when you convert one type of variable to another within your program. Like when you divide two whole integers and want a float result you change it into a float.
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:
float avg(int m1, int m2, int m3, int m4) { float average=0; int total=0; total=m1+m2+m3+m4; average=(float)total/4; // this causes the division to recognize the float return(average); }
Here you can see i have an integer “total” and a float “average” in order to devide by 4 the total i first had to type cast he integer so the computer will recognize it not as a integer now but a float.
These are different types of loops. A loop is used when you want to repeat a process more then once based on a variable or a predetermined set of boundaries
while(*pick != -1) { *pick=rand()%99+1; printf("guess the computers pick: "); scanf("%d", input); if(*input == *pick) { printf("you are correct!\n"); } else if(*input > *pick) { printf("HAHA FAIL you are too high\n"); } else { printf("GOLDEN GUESS jk you were wrong that is too low\n"); } printf("the computers pick was %d\n",*pick); } printf("the computers pick was %d\n",*pick); printf("to go again enter in 0, to quit -1: "); scanf("%d", pick); } for(pos=0;pos<len;pos++) { fprintf(stdout,"%c",*(word+pos)-32); }
Do whiles are essentially just like the while loop but with a different way of writing it.
CAN I READ IT?
This entails me looking at one of our harder functions and explaining what each part is doing and how all the parts work together.
I will look at the program called
The information in the
#include <stdlib.h> int sum(int, int, int, int); //function prototype float avg(int, int, int, int); int high(int, int, int, int); //[these four blocks are function protoypes.... this means that they dont actually exist yet int low(int, int, int, int); // but are in place so that we can define them later. They will all receive] int main() { int a,b,c,d; a=b=c=d=0; printf("enter first value: "); fscanf(stdin, "%d", &a); printf("enter second value: "); // this zone is the initial area where we ask the user for his numbers fscanf(stdin, "%d", &b); // and scan there output into a variable. printf("enter third value: "); fscanf(stdin, "%d", &c); printf("enter fourth and final 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)); //these lines output different fprintf(stdout, "the avg of %d, %d, %d, and %d is : %0.5f\n",a,b,c,d,avg(a,b,c,d)); // results based on the numbers // one is sum, average, and lowest and greatest numbers. each calls a different function fprintf(stdout, "the greatest value is %d, and the lowest value entered is %d\n",high(a,b,c,d),low(a,b,c,d)); return(0); } // sum(a,b,c,d) is sending copies to v that function.not changing the value or cant change the value of a b c or d int sum(int n1, int n2, int n3, int n4) { int total=0; // this zone basically adds the numbers together and returns total so that when the total=n1+n2+n3+n4; // function main uses it it will see only the return of this function. return(total); } float avg(int m1, int m2, int m3, int m4) { float average=0; // here we take the same numbers , re add them , then convert them to a float. int total=0; // this is so that we can now divide by 4 and get decimals. Once again returns a single result. total=m1+m2+m3+m4; average=(float)total/4; // this causes the division to recognize the float return(average); } int high(int b1, int b2, int b3, int b4) { int greatest=0; if( (b1>b2) && (b1>b3) && (b1>b4)) { greatest=b1; } if( (b2>b1) && (b2>b3) && (b2>b4)) // this uses many ifs to check and find which variable is the largest by { greatest=b2; // comparing to the others. } if( (b3>b1) && (b3>b2) && (b3>b4)) { greatest=b3; } if( (b4>b1) && (b4>b3) && (b4>b2)) { greatest=b4; } } int low(int b1, int b2, int b3, int b4) { int lowest=0; if( (b1<b2) && (b1<b3) && (b1<b4)) { lowest=b1; } if( (b2<b1) && (b2<b3) && (b2<b4)) // same as greatest but tries to find the least greatest variable. { lowest=b2; } if( (b3<b2) && (b3<b1) && (b3<b4)) { lowest=b3; } if( (b4<b2) && (b4<b3) && (b4<b1)) { lowest=b4; } return(lowest); }
Reflect upon your results of the measurement to ascertain your achievement of the particular course objective.
What is the question you'd like to pose for experimentation? State it here.
Collect information and resources (such as URLs of web resources), and comment on knowledge obtained that you think will provide useful background information to aid in performing the experiment.
Based on what you've read with respect to your original posed question, what do you think will be the result of your experiment (ie an educated guess based on the facts known). This is done before actually performing the experiment.
State your rationale.
How are you going to test your hypothesis? What is the structure of your experiment?
Perform your experiment, and collect/document the results here.
Based on the data collected:
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.
What is the question you'd like to pose for experimentation? State it here.
Collect information and resources (such as URLs of web resources), and comment on knowledge obtained that you think will provide useful background information to aid in performing the experiment.
Based on what you've read with respect to your original posed question, what do you think will be the result of your experiment (ie an educated guess based on the facts known). This is done before actually performing the experiment.
State your rationale.
How are you going to test your hypothesis? What is the structure of your experiment?
Perform your experiment, and collect/document the results here.
Based on the data collected:
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.
What is the question you'd like to pose for experimentation? State it here.
Collect information and resources (such as URLs of web resources), and comment on knowledge obtained that you think will provide useful background information to aid in performing the experiment.
Based on what you've read with respect to your original posed question, what do you think will be the result of your experiment (ie an educated guess based on the facts known). This is done before actually performing the experiment.
State your rationale.
How are you going to test your hypothesis? What is the structure of your experiment?
Perform your experiment, and collect/document the results here.
Based on the data collected:
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.
Today we learned about typedef. I still don't know more then the basics of it but it allows you to create an alias for a function like “ typedef int i” allows the user to now say “i x” instead of “int x”. This is just the tip of what it can do i would guess. We also learned about union and struct containers along with a class but that is not a container. These containers are heterogeneous and don't have to contain the same type of data. they can hold ints and chars. Arrays are homogeneous meaning they can only hold one type of data. We also learned that adding -g to our gcc we can allow for debugging when we type gdb ./progname . this is useful because everyone likes to debug.
Today we manipulated our first C++ program into file separation and rejoined them up making our original program and script shorter. This idea allows us to sepperate the functions of a program into smaller pieces that may seem more manageable and make our final script look just that much nicer. this is good for working in groups so that each person can take a chunk and work on it then piece it all together again later.
Today we discussed the project2 and the importance of Paper. Why is paper important. (i use a whiteboard at home and a camera) Paper helps one organize their thoughts and visually see the code in their head before typing it all out. Paper also helps you run through problems to see an exact process you think it will follow. It doesn't help with syntax but it will help with your logic. Object oriented programming teaches us that paper is key and it never hurts to make some psuedocode and a uml document sometimes.
(yes this is out of order)
Today we discussed ways to fix project 1 by either flipping the letters back to the beginning of the alphabet or by changing all letters to their ASCII chart equivalent numbers allowing for a lot more flexibility. We also looked at a project that was multifunctioned or had more then one function in the script that contained it. The program took a number and ran it through a series of tests to check and see if it was even or odd. This was a a huge change because it gave us a hint into how to better organize our data within a script.
A pointer is basically something that contains a memory address of whatever it is pointing at. When you assign a pointer it will then point at that objects memory location. It is possible to use these pointers in another way like in arrays after you dereference it. The following code and result will show how this is done.
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:
#include <stdio.h> int main () { int v=17; int *p1=NULL; printf("v is %u\n", v); p1=&v; printf("*p1 is %u\n", *p1); *p1=53; printf("v is %u\n", v); printf("*p1 is %u\n", *p1); v=7; printf("v is %u\n", v); printf("*p1 is %u\n", *p1); return(0); }
Alternatively (or additionally), if you want to demonstrate something on the command-line, you can do so as follows:
lab46:~/src/cprog$ ./arrows v is 17 *p1 is 17 v is 53 *p1 is 53 v is 7 *p1 is 7
Selection structures allow for variation. When most beginning programs tend to be linear Selection Structures allow for branches within a program to form. The first type is an if statement which is usually just understood. If this then that else go there type program. say if (a < b) {
do this stuff
}
if it is not true it just goes around it. There can be nested if statements creating a messier but deeper program. The next type is called Switch and it can be used when one needs to have a series of options that can be selected. This is useful when we write code based on a variable. The following is not my code but code i found that shows what it is.
switch(var) { case constant1: statement; ... break; case constant2: statement; ... break; default: statement; ... break; }
This shows just a basic idea of how to use it but i am still not familiar with it.
This can be done in or outside of a program. We read and write in files whenever we go near a program. The availability to read, write or append is based on ones settings in chmod.
# Permission 7 full 6 read and write 5 read and execute 4 read only 3 write and execute 2 write only 1 execute only 0 none
(taken from Wikipedia under Chmod) the other way is to use fopen (“thisisfile.txt”, permission) here is a list of permissions taken from http://www.cplusplus.com/reference/clibrary/cstdio/fopen/
"r" Open a file for reading. The file must exist. "w" Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file. "a" Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist. "r+" Open a file for update both reading and writing. The file must exist. "w+" Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file. "a+" Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseek, rewind) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.
I really only understand typedef in its most basic form but can be very convenient.
typedef int i i x
the i now stands for int allowing you to waste less time typing it. Typedef can be used on much larger function like long long int and such creating a easier way to do tings. Enum is a lot more complicated and i am having a difficult time grasping it right now but aperently it can be used as a glorified number say (not my code) public static final in SEASON_WINTER = 0; could be a enum while enums can also be used in this way( i do not know how to do this this is from java but is pretty cool in how it is used. http://docs.oracle.com/javase/1.5.0/docs/guide/language/enums.html)
public enum Planet { MERCURY (3.303e+23, 2.4397e6), VENUS (4.869e+24, 6.0518e6), EARTH (5.976e+24, 6.37814e6), MARS (6.421e+23, 3.3972e6), JUPITER (1.9e+27, 7.1492e7), SATURN (5.688e+26, 6.0268e7), URANUS (8.686e+25, 2.5559e7), NEPTUNE (1.024e+26, 2.4746e7), PLUTO (1.27e+22, 1.137e6); private final double mass; // in kilograms private final double radius; // in meters Planet(double mass, double radius) { this.mass = mass; this.radius = radius; } public double mass() { return mass; } public double radius() { return radius; } // universal gravitational constant (m3 kg-1 s-2) public static final double G = 6.67300E-11; public double surfaceGravity() { return G * mass / (radius * radius); } public double surfaceWeight(double otherMass) { return otherMass * surfaceGravity(); } } public static void main(String[] args) { double earthWeight = Double.parseDouble(args[0]); double mass = earthWeight/EARTH.surfaceGravity(); for (Planet p : Planet.values()) System.out.printf("Your weight on %s is %f%n", p, p.surfaceWeight(mass)); } $ java Planet 175 Your weight on MERCURY is 66.107583 Your weight on VENUS is 158.374842 Your weight on EARTH is 175.000000 Your weight on MARS is 66.279007 Your weight on JUPITER is 442.847567 Your weight on SATURN is 186.552719 Your weight on URANUS is 158.397260 Your weight on NEPTUNE is 199.207413 Your weight on PLUTO is 11.703031
epic (this is java use but shows what it is capable in some languages.) This is a version in C from http://msdn.microsoft.com/en-us/library/whbyts4t(v=vs.80).aspx
enum DAY /* Defines an enumeration type */ { saturday, /* Names day and declares a */ sunday = 0, /* variable named workday with */ monday, /* that type */ tuesday, wednesday, /* wednesday is associated with 3 */ thursday, friday } workday;
this is good in its own way but is just placing numbers into variable in a simpler fashion then doing it the long way by separating them all and adding = blah to it. Union is the final type. Union is a container that can hold multiple variables of different types but cant use both variables at the same time because one will get all screwy. They allow for really easy data manipulation and with typedef make for short work when it comes to typing things out.
#include <stdio.h> #include <stdlib.h> int main() { int i; union var { int x; float f; }; typedef union var Uif; Uif value; value.x=0; for(i=0;i<24;i++) { value.x=value.x+rand()%51+1; } printf("total is: %d\n", value.x); value.f=0.0; for(i=0;i<73;i++) { value.f=value.f+rand()%27+0.1; } printf("total is: %f\n",value.f); return(0);
lab46:~/src/cprog$ ./union total is: 734 total is: 937.299438 lab46:~/src/cprog$
Identification of chosen keyword (unless you update the section heading above).
STANDARD FORM The standard form of an array that we were taught in class is as follows. arr1 = (int *)malloc( sizeof(int) * (sizea-1)); What this does is it takes the array “arr1” and gives it the size of one int or relatively the ability to hold one int, but we then malloc that array to increase its size by the number we desire allowing it to hold a set number of anything.
POINTER ARITHMETIC c = arr1[i-1]; or arr[ i ] == * ( arr + i )
both add using pointers. Subtraction will have the same results but in the opposite direction. This allows you to change the location of the array you wish to access.
SINGLE DIMENSIONAL This array can only hold one thing in each slot of the array. int[] myArray = new int[] {1, 3, 5, 7, 9}; each piece separated by a comma is in its own block within the array. This makes more sense next.
MULTIDIMENSIONAL ARRAYS This array can hold multiple things in each block of the array and i don't mean digits that are multiple numbers long. int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; now each thing in {} is in a different block so array2D[0] would contain 1 and 2 and so forth for each block of the array. This is useful because say you input one thing and expect a set of something else. Each set can be stored into one block of an array and then your distinction would point to which block to get the data from.
DECLARATION
typedef struct {
char name[64]; char course[128]; int age; int year; } student;
This usually comes after the #include of a program. A Struct basically allows the user to set variables usable in multiple locations through out a script.
ACCESSING ELEMENTS
st_rec.name
using this we can use the variable in struct. This in general is just a variable name for the element in the struct. This is longer but if you have multiple functions inside one script you will not have to redefine each variable within each code block.
POINTERS TO
st_ptr -> name
The → indicates that you are selecting from a structure pointer.
#include <iostream> 3 cout // standered out in c 4 cin // standard in in c 5 cerr // standard error in c 6 7 printf("Hello, Workd!\n"); 8 cout << "hello, world!" << endl; 9 10 a=4,b=2; 11 printf("a is %d, b is %d\n", a,b); 12 cout << "a is" << a << "b is" << b << endl; 13 cout << "a is" << hex << a<< endl; 14 bin 15 oct 16 dec 17 18 19 20 cin >> a; 21 scanf("%d", &a); 23 24 25 cerr << "ERROR! RABID CHICKENS!" << endl; 26 27 28 // if i want to use these i need to declair a namespace. i do this by .... 29 30 using namespace std; 31 before my main will allow them to be used in main 32
the above shows I/O stream functions and their C equivalent. The operators are « and ». There are other operations or manipulators that can be placed in the I/O stream
endl "end line": inserts a newline into the stream and calls flush. ends "end string": inserts a null character into the stream and calls flush. flush forces an output stream to write any buffered characters dec changes the output format of number to be in decimal format oct changes the output format of number to be in octal format hex changes the output format of number to be in hexadecimal format ws causes an inputstream to 'eat' whitespace showpoint tells the stream to show the decimal point and some zeros with whole numbers
This is referring to the above key word ^^^^^^^^^^. At the end of the first cli block it says to use “namespace std”. “std” stands for standard. This is reffering to standard (input output, library[most likely]). How to use this. Here is an example from http://www.cplusplus.com/doc/tutorial/namespaces/
// using namespace example #include <iostream> using namespace std; namespace first { int x = 5; } namespace second { double x = 3.1416; } int main () { { using namespace first; cout << x << endl; } { using namespace second; cout << x << endl; } return 0; }
What this demonstrates is that one can creat their own namespaces like std and use them throughout the program. Downside is that you have to keep saying what namespace you are using but this can be a great aid with some programs.
State the course objective
In your own words, define what that objective entails.
State the method you will use for measuring successful academic/intellectual achievement of this objective.
Follow your method and obtain a measurement. Document the results here.
Reflect upon your results of the measurement to ascertain your achievement of the particular course objective.
Can I use OCT, HEX, and DEC work in an printf if they work in an I/O stream?
man 3 printf
Yes i can convert things into HEX,DEC,and OCT format. This is because C++ is a more simplified version of C so therefore C must have the functionality to do what C++ can but in a possibly harder format.
How are you going to test your hypothesis? I am going to test my hypothesis through attempting to give a variable “a” a value and convert that value in a printf statement into a different base.
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 int a = 4923523; 7 printf("This is a in HEX : %x\n", a); 8 9 printf("This is a in OCT: %o\n", a); 10 11 printf("This is a as a plain DEC: %u\n",a); 12 return(0); 13 }
Here is the results.
lab46:~/src/cprog$ ./expiremnt4 This is a in HEX : 4b2083 This is a in OCT: 22620203 This is a as a plain DEC: 4923523 lab46:~/src/cprog$ vi expiremnt4.c lab46:~/src/cprog$
Based on the data collected:
It is possible to convert the results to different formats within the actual printf statement as one would do in a cout stream in c++.
Based on experiment 4. Would %x stay the same or change if i enter in a already HEX number?
experiment 4.
I believe that it will remain the same.
The developers must have thought this idea through if they allowed for the conversion to take place at all. %u doesn't try and convert when a decimal number is already existent.
I am going to change a in the above program and see if it can run the same three printf statements and comvert from hex and see if %x can recognize a hex digit and keep it the same.
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 int a = 0x4b2083; 7 printf("This is a in HEX : %x\n", a); 8 9 printf("This is a in OCT: %o\n", a); 10 11 printf("This is a as a plain DEC: %u\n",a); 12 return(0); 13 }
This is my attempt at trying to input a hex digit. I tried it the first time and it did not like the numbers but i looked up some possible solutions and learned that the computer won't recognize a hex digit without 0x infront of it else it will just complain about the letters. Here are the results.
lab46:~/src/cprog$ ./expiremnt4 This is a in HEX : 4b2083 This is a in OCT: 22620203 This is a as a plain DEC: 4923523
as you can see this matches the above projects results.
Based on the data collected:
The way C was designed keeps it from excepting just letters as part of a number but with the right deffinitions on the number it can be seen as what the letters represent as with the 0x allowing letters up to f to be in a number as it is part of the hex digit system. The computer can also recognize and convert a hex digit to a dec or oct equivalent.
Perform the following steps:
Whose existing experiment are you going to retest? Provide the URL, note the author, and restate their question.
Evaluate their resources and commentary. Answer the following questions:
State their experiment's hypothesis. Answer the following questions:
Follow the steps given to recreate the original experiment. Answer the following questions:
Publish the data you have gained from your performing of the experiment here.
Answer the following:
Answer the following:
Today we learned about logic gates and classes. We used classes to allow us to create separate files each containing a part of the program. This allows for organization and simple finding because instead of having to search a single program you can just look for the file of which one would hope was named after what it does. Ex. got a function that checks two variables for if they are both true. you would name this separate file “and.cc” not “phill” as “and.cc” is actually what it checks for.
Today i learned that their is actually a changeable file for the vi system. This was really cool for me because one of my biggest problems with vi is that it was to dull and sometimes hard to read sometimes after staring for a while. This also was cool because it demonstrates that once again everything and every program is a file.
Templates - a tool for generic programming in c++ They allow for a generic representation of a process that gets specific when you use it. Its kind of like a stencil for a program. A stencil can fit different types of coloring utensils so in that aspect a template can support different types of variables from chars to ints.
this includes April 30 - May 9, 2012 (the opus does not allow me to edit the fourth journal day so i am putting it here)
procrastination kills. Let this be a warning unto myself if i pass this class i will be happy. The issue is that i keep putting off this class work in order to allow myself the ability to finish other work and running out of time with this class. New priority this stuff is my life nuff said i need to focus on my programming because this will be my future. i also have really bad eye irritation and need some gunnar glasses so this doesn't hurt so much.
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:
Remember that 4 is just the minimum number of entries. Feel free to have more.
Functions are something a programmer will use everyday. We use them and create them whenever we do something with a program. Each program we make is essentially a function, whether we pass it parameters or not its a function and can be adjusted to work inside any other function. example in C
#include <stdio.h> 2 #include <stdlib.h> 3 int subtraction(int *a1, int *a2,int); //I then call this function called "subtraction" later on in my devision part of the program and this is used as a function. 266 int subtraction(int *arr1, int *arr2,int sizea) 267 { 268 int l,i,a,b,c,p; 269 l=i=a=b=c=p=0; 270 while(!(*arr1 <= 0)) 271 { 272 i=sizea-1; 273 for(l=sizea-1;l>=0;l--) 274 { 275 276 a = arr1[i]; 277 b = arr2[l]; 278 if( a >= b ) 279 { 280 a = a - b; 281 } 282 else if( (a < b)&&(arr1[i-1]>0)) 283 { 284 c = arr1[i-1]; 285 c = c - 1; 286 arr1[i-1] = c; 287 a = a + 10; 288 a = a - b; } 290 arr1[i] = a; 291 i=i-1; 292 } 293 p=p+1; 294 } 295 return(p); 296 }
what this did was take information given and send it to a function that would act within the program and would return a result. It does not always have to return anything but this one did. The one thing you may notice is that i sent something into the subtraction program. I am sending paramaters for the program to act on. Two ways they can be sent is by value and by reference. Value is when we send the function a straight up variable, this is when we sent it “int sizea” that was by value. The next kind i used was by reference “int *arr1” this was sending it the array called “arr1” and sending all the pieces of it not just one part. The star declares that it is by reference. The next kind is when we pass something by address which is when we send the computer a hexadecimal digit representing a particular zone in memory.
The return type can be seen within the program. Line 295 above shows that I am returning the variable “p” which is an int. You can return anything back to the original but you can only return one thing to it so be specific on what you want to send out. There is no limit to what you can send out besides the fact that it must be one thing(can be int, char, long int, void, float ex…).
Recursion— bassically something that will run itself over and over again… here is an example of code from http://www.cprogramming.com/tutorial/lesson16.html
#include <iostream> using namespace std; void recurse ( int count ) // Each call gets its own count { cout<< count <<"\n"; // It is not necessary to increment count since each function's // variables are separate (so each count will be initialized one greater) recurse ( count + 1 ); } int main() { recurse ( 1 ); //First function call, so it starts at one }
Compiler is a program that will take your script and convert it into runnable code. gcc or g++ ( one i used recently “gcc -g -o bignum bignum.c” )
Preprocessor allows for the use of header files and checks for errors during before compilation to make sure the program will run. -Wall can be added to the line above to allow the preprocessor to check for warning and not just flags.
Flags are the easiest things the the preprocessor can spot because they are errors like missing “;” or trying to use a char as an int or something that the user may mistake and can be solved easily.
The Assembler is the part of the computer that takes a program written in source code and compiled then translated int to assembly language and then to matching code so that it can be used.
The linker is the final part of the compiler where it takes the source codes from multiple pieces and combines them into one executable file. Usually we have only used it to take one source code and create one program but it can do this to many pieces of source code and combine them into one executable.
Multi-file programs - usually its best to create a separate directory for a multi filed program as it is easier to work with.
lab46:~/src/cprog/c++/sampleprogs/abtractbaseclass$ ls abstractbaseclass.cc and.h friends gate.h main.cc nand.cc nand.o or.h origabstractclass.cc xor.cc xor.o and.cc and.o gate.cc gate.o main.o nand.h or.cc or.o program xor.h lab46:~/src/cprog/c++/sampleprogs/abtractbaseclass$
to compile this we first take the original say “gate.cc” and extract the definitions form it and make them into a header file “gate.h” after that we can convert the .cc file into a .o by using “g++ -c gate.cc” then to combine all the .o files we use “g++ -o program and.o gate.o main.o nand.o or.o xor.o ”
Classes allow for data to be sent in and sent out it makes it easy to define variables in multiple parts of functions like the program in the keyword above uses classes to allow for easy creation of variable so that the same data doesn't have to repeated a hundred times.
objects in a class are the pieces that are being used in the other functions in relation to that class.
Constructor creates the object for the moment that it is called and after its use is deconstructed by the deconstructor when no longer needed.
Access control can be summed up by what the child program cant do and can do. the child program is the program that uses objects created by the class. Remember the child can access its parents public and protected objects but it cannot touch its parents privates.
Public - is data that any part of the program can access and manipulate.
protected - is data that only designated parts can access but usually allows for all child programs.
private - is only data accessible within the class itself and cannot be accessed by any other part unless you got a friend.
friend - creating a friend is like giving a person the permission to stick his hand into your parents private when related to programming. A friend can bypass the security of a class and access the private parts of the class so the child program can use that data.
“This” pointer type - from lack of a better way to put it “ The keyword this identifies a special type of pointer. Suppose that you create an object named x of class A, and class A has a nonstatic member function f(). If you call the function x.f(), the keyword this in the body of f() stores the address of x. You cannot declare the this pointer or make assignments to it.” by http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr035.htm
#include <iostream> using namespace std; struct X { private: int a; public: void Set_a(int a) { // The 'this' pointer is used to retrieve 'xobj.a' // hidden by the automatic variable 'a' this->a = a; } void Print_a() { cout << "a = " << a << endl; } }; int main() { X xobj; int a = 5; xobj.Set_a(a); xobj.Print_a(); }
from same place
Inheritance - is when you create a child program it receives the data from the class and uses it as its own.
class NAND:public GATE{ 7 public: 8 NAND(); 9 void process(); 10 };
multiple inhertance is like inheritance of a single class but from multiple parents to one child …. yes there can be more then 2 parents. http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm (Table below)
class A { /* ... */ }; class B { /* ... */ }; class C { /* ... */ }; class X : public A, private B, public C { /* ... */ };
Polymorphism - takes the idea of a type pointer in one class and turns it back on the parent class as they share the same type pointer as the parent. script from http://www.cplusplus.com/doc/tutorial/polymorphism/ because ours was really long and i missed that day so i have photos and not program.
// pointers to base class #include <iostream> using namespace std; class CPolygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b; } }; class CRectangle: public CPolygon { public: int area () { return (width * height); } }; class CTriangle: public CPolygon { public: int area () { return (width * height / 2); } }; int main () { CRectangle rect; CTriangle trgl; CPolygon * ppoly1 = ▭ CPolygon * ppoly2 = &trgl; ppoly1->set_values (4,5); ppoly2->set_values (4,5); cout << rect.area() << endl; cout << trgl.area() << endl; return 0; }
An abstract base class is like “class person” with the objects that define it more as age and height. its non-specific or vague concept
when you overload a function you have two functions with the same name but different parameters and by calling that program it will dicide which one to choose based on the parameters that you feed it.
when you overload an operator you
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$
A template is like a stencil or outline of a program. difference is a template is not specific until it is given data then it will choose which program to run that matches the template.
template <class T> 2 T larger(T x, T y) 3 { 4 if (x >= y) 5 return (x); 6 else 7 return(y); 8 } 9 10 int main() 11 { 12 int a=12, b=17; 13 float c=3.14,d=1.59; 14 char e=65, f='A'; 15 16 cout<<larger(a,b)<<endl; 17 cout<<larger(c,d)<<endl; 18 cout<<larger(e,f)<<endl; 19 return(0); 20 } 21 22
STL - contains many of the basic algorithms and outlines for the programs used in C++
type casting operators -
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$
an exception is a way of dealing with odd circumstances in our programs by surrounding a block of text in the block called “try”
int main() { try { throw 10 } catch(int k) { printf("an exception has happened %d\n",e); } ...
the idea of using this is to have the throw be within your program inside of try so you want to put your whole program in try. Then the variables that are getting lost you check with catch( variable type) and then output what is getting lost or if it isnt.
1 2 3 4 5 6 7 8 9 10 11 try { try { // code here } catch (int n) { throw; } } catch (...) { cout << "Exception occurred"; }
State the course objective
In your own words, define what that objective entails.
State the method you will use for measuring successful academic/intellectual achievement of this objective.
Follow your method and obtain a measurement. Document the results here.
Reflect upon your results of the measurement to ascertain your achievement of the particular course objective.
in an while statement can i subtract 48 from /n in the controller of the loop?
none previous knowledge
Yes, I should be able to treat the variables like they were somewhere else and not special.
I am going to use a while loop and i wanted to subtract 48 from the numbers because they were still in ASCII so i did now it wont see the \n that finishes it i will change the end loop variable to '\n'-48 and hope it works
while(g!='\n') 52 { 53 g = fgetc(stdin); 54 g = g-48; 55 arr2[f] = g; 56 f = f + 1; 57 } while(g!='\n'-48) 52 { 53 g = fgetc(stdin); 54 g = g-48; 55 arr2[f] = g; 56 f = f + 1; 57 }
this worked
Based on the data collected:
This works and is a great way to help with math functions in a program.
What is the question you'd like to pose for experimentation? State it here.
Collect information and resources (such as URLs of web resources), and comment on knowledge obtained that you think will provide useful background information to aid in performing the experiment.
Based on what you've read with respect to your original posed question, what do you think will be the result of your experiment (ie an educated guess based on the facts known). This is done before actually performing the experiment.
State your rationale.
How are you going to test your hypothesis? What is the structure of your experiment?
Perform your experiment, and collect/document the results here.
Based on the data collected:
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.
Perform the following steps:
Whose existing experiment are you going to retest? Provide the URL, note the author, and restate their question.
Evaluate their resources and commentary. Answer the following questions:
State their experiment's hypothesis. Answer the following questions:
Follow the steps given to recreate the original experiment. Answer the following questions:
Publish the data you have gained from your performing of the experiment here.
Answer the following:
Answer the following: