======Part 1====== =====Entries===== ====Entry 1: February 7, 2012==== * On this date, I found out the proper syntax and usage of arrays in the C Programming language. * Arrays are an essential part of programming, because they allow the programmer to input multiple similar variables into specific allocated memory, and from there the programmer can use that array to do many different things in the program. * At the moment, the concept of arrays I am set with. I can't think of any problems that have come up. * My understanding of arrays comes from C for Engineering, which is a little different because we used the C++ version of arrays, which gave me a little confusion at first, but now all is going well. ====Entry 2: February 10, 2012==== * On this date I finally grasped the concept of the data types and what I needed to program into the code to either accept them from the user or display them to the user. * With this knowledge, I can easily program code that allows me to accept values and print values that are necessary for the program. * The concept of printing characters in an array still confuse me, like the use of %s and %c. * With the programming we did February 16, 2012, I began to understand what these mean and what I can do to print single characters instead of strings. ====Entry 3: February 9, 2012==== * On this date, the concept of using debugging programs was introduced. * Using this method, I was able to find out where a logical error in a program is, and in some cases it can tell me how to fix it. * This is very helpful, because logical errors occur often in programs, which doesn't stop the program from compiling. Using this strategy, I can find those logical errors quickly and correct them. * I have used this a few times and it has helped quite a bit. It helped with some of the programs I had to write, and I was able to fix them easily. ====Entry 4: February 16, 2012 ==== * On this date, I learned how to take arguments input by the user, assign them to an array, and use them for multiple purposes. * Using this method, I am able to print certain code, take the arguments, separate the characters, and use each as their ASCII numbers, and many other things. * I used this in the first project, as well as some in class examples. * My knowledge of using this method is still lacking a bit, but I am beginning to understand it more. =====Keywords===== {{page>cprogpart1&nofooter}} =====Experiments===== ====Experiment 1==== ===Question=== What would be printed if I replace the %u with the $d for an unsigned integer? ===Resources=== According to classes being taken, %d represents a signed integer, while %u represents an unsigned integer. The range for a signed integer data type is -2,147,483,647 to 2,147,483,646, while the range for an unsigned integer data type is 0 to 4,294,967,295. ===Hypothesis=== I believe that the printed value will be the same ONLY if it is within the range of the signed integer data type. Ex. If the user inputs a value between 0 and 2,147,483,647, then it should print out fine, but if the value is greater than that range, then the value will "roll over" to the next integer in the signed integer data type, which would make the value a negative number. Due to the data type only covering positive numbers (unsigned integer), it can go to larger numbers in the 4 bytes it is given for memory, but if it is printed as a signed integer, then it will only be able to print out half of the positive integers that it could as an unsigned integer, so it will go back over into the negative numbers until it satisfies the amount of digits it has. ===Experiment=== To run this experiment, I am going to use the Lab46 terminal and create a program that will accept a value ranging from 0 to 4,294,967,295, which will be accepted as a unsigned integer. From there, the program will print the value back out as a signed integer using the %d command. ===Data=== This is the code that I used: #include int main() { unsigned int integer; printf("Please input any integer between 0 and 4,294,967,295: "); scanf("%u\n", &integer); printf("The number you put into the program is: %d\n\n", integer); return(0); } This is the result: lab46:~/src/cprog$ ./experiment1 Please input any integer between 0 and 4,294,967,295: 3015489756 1 The number you put into the program is: -1279477540 lab46:~/src/cprog$ ===Analysis=== Based on the data collected: * My hypothesis was correct. * My hypothesis was applicable because the number did roll over because it was too large to be included as a positive integer, so it just ran back as a negative number. * Yes, much more is going on than I originally thought. I can mix it up. * I tried to run the program and it did not work. It was able to run, and I fixed it. * The data was what I was expecting. ===Conclusions=== When using an unsigned data type, it is possible to print that number as a signed data type, the only problem is that if it is larger than the range given, then it will just "roll over" until the entire number has been stored. ====Experiment 2==== ===Question=== What would result from manipulating the "firstarray.c" program and replacing the "*4" with a "*3" and nothing else? ===Resources=== According to what I have learned so far, the purpose of the "*4" along with the function malloc is to allocate memory that the program can then store whatever it needs to in. This is one way to create an array. The "*4" in this program specifically multiplies the size of the char data type, which is one bit, and multiplies that by 4, leaving 4 bits of space that the program can store data in. ===Hypothesis=== My best guess would be that the array will only have three char-sized "blocks" of memory that are available for storage. This will cause the fourth assignment in the array (*(array + 3)) to not be assigned to any block of memory in the array. This shouldn't cause any problems with the compiler, the program should run fine, still. ===Experiment=== To perform this experiment, I have used a program from class and manipulated it, replacing the 4 with a 3 when sizing the array. The program then prints out what is left, and I believe that the program will print out a bunch of randomness when it gets to the fourth block of memory that was supposed to be created. ===Data=== This is the program, minus the documentation: #include #include int main() { char *value, i; //Declaring two integers: the pointer "value" //and i as a char value = (char*)malloc(sizeof(char)*3); *(value+0) = 0; *(value+1) = 1; fprintf(stdout, "Please enter a value (-128-+127):"); fscanf(stdin, "%hhd", &i); *(value+2) = i; *(value+3) = (2*i)+3; for(i=3; i>=0; i--) { printf("%hhd\n", *(value+i)); } return(0); } These are the results of the program: lab46:~/src/cprog/Opus/Opus1$ ./experiment2 Please enter a value (-128-+127):15 33 15 1 0 lab46:~/src/cprog/Opus/Opus1$ ./experiment2 Please enter a value (-128-+127):458 -105 -54 1 0 lab46:~/src/cprog/Opus/Opus1$ ./experiment2 Please enter a value (-128-+127):12 27 12 1 0 lab46:~/src/cprog/Opus/Opus1$ ===Analysis=== Based on the data collected: * My hypothesis was incorrect. * My hypothesis was applicable. When the memory was allocated, a little bit of "buffer" memory was saved, as well, which allowed the last value to be stored when it was compiled. * I didn't think that there would be any more memory left to store that value in, but it seems that there is more to the allocation of memory than I thought. * The chance of storing a variable in an array when the program doesn't specifically make the memory for it is one that I did not think would happen. * The lack of memory allocation didn't stop the number from kicking down the array's door and walking right in! ===Conclusions=== Through this experiment, I realized that there is more to arrays and memory allocation than I thought. There is a chance that variables may be stored in buffer memory when making an array, and that is what happened in this experiment. I also learned that I need to read up on my arrays. ====Experiment 3==== ===Question=== Is it possible to multiply things in an array and store the result in the array? ===Resources=== From what I have learned about arrays, arrays are capable of a lot of different functions, like storing related variables, and either reading them out or using them for another purpose. So far, arrays have been used to store values and re-arrange them, read them back out, to take variables from one function and use them in another, and many other purposes. ===Hypothesis=== I believe that this is possible, because arrays store similar variables, even if they are combined in some way and stored back into it. ===Experiment=== I will make a program that creates an array that will allow the user to store two integer numbers between one and one hundred into the array. From there, the program will call those two numbers from it, and multiply the two numbers together, then storing the third in the array. The array will then be printed. ===Data=== The program for the experiment: #include #include int main() { int storage1 = 0; int storage2 = 0; int *array; array = (int*)malloc(sizeof(int)*3); fprintf(stdout, "Please enter two numbers between 1 and 100:\n"); fscanf(stdin, "%u", &storage1); fscanf(stdin, "%u", &storage2); *(array) = storage1; *(array+1) = storage2; *(array+2) = (*(array+1)) * (*(array)); fprintf(stdout, "These are the numbers stored in the array:\n%u\n%u\n%u$ return(0); } The results: lab46:~/src/cprog/Opus/Opus1$ ./experiment3 Please enter two numbers between 1 and 100: 10 15 These are the numbers stored in the array: 10 15 150 lab46:~/src/cprog/Opus/Opus1$ ./experiment3 Please enter two numbers between 1 and 100: 100 100 These are the numbers stored in the array: 100 100 10000 lab46:~/src/cprog/Opus/Opus1$ ===Analysis=== Based on the data collected: * My hypothesis was correct. * My hypothesis was applicable. * What I thought was basically what happened, although I did think that due to the fact that I had to use the "*" symbol in the arrays and for multiplication, I thought that there may be some confusion in the program that would have caused an error. * The program didn't have a check for any numbers below one or above 100, even though the number could have been much, much greater than 100. (Not lower than one, because any number below zero doesn't technically exist for the unsigned integer data type) * Had I put in a number that caused the result to be greater than the threshold for integer data types, the result would have been incorrect by mathematical terms, and so the program would not have looked as if it worked, although it was fully functional and correct. ===Conclusions=== Arrays can be very useful when it comes to storing numbers and variables that you may need later or in other functions. An array could be created to take multiple numbers and use these numbers to show different operations and what the result of these operations would be.