This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:acrowle1:portfolio:cprogproject3 [2014/03/01 15:42] – [Background] acrowle1 | user:acrowle1:portfolio:cprogproject3 [2014/03/09 14:53] (current) – [Project: dayofweek] acrowle1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ======Project: | + | ======Project: |
A project for CSCS1320S14 by Alana Whittier during the Spring Semester 2014. | A project for CSCS1320S14 by Alana Whittier during the Spring Semester 2014. | ||
- | This project was first begun, on or around, February, 20, 2014 and took approximately 1 week to complete. The program I first submitted is not // | + | This project was first begun, on or around, February, 20, 2014 and took approximately 1 week to complete. The program I first submitted is not // |
=====Objectives===== | =====Objectives===== | ||
The purpose of this project is to become familiarized with using a pointer in order to use the scanf() feature for obtaining input from a user. Also, this project serves to introduce a new user of C programming to conditional statements in order to achieve the desired output. In order to successfully compile and execute the program using a mental math technique, a pointer must be applied appropriately, | The purpose of this project is to become familiarized with using a pointer in order to use the scanf() feature for obtaining input from a user. Also, this project serves to introduce a new user of C programming to conditional statements in order to achieve the desired output. In order to successfully compile and execute the program using a mental math technique, a pointer must be applied appropriately, | ||
Line 12: | Line 12: | ||
**Helpful Resources: | **Helpful Resources: | ||
* http:// | * http:// | ||
+ | * http:// | ||
* Kernighan, Ritchie //The C Programming Language// Second Edition, AT&T Bell Laboratories, | * Kernighan, Ritchie //The C Programming Language// Second Edition, AT&T Bell Laboratories, | ||
* email consultations and guidance from Matt Haas | * email consultations and guidance from Matt Haas | ||
Line 29: | Line 30: | ||
[[haas: | [[haas: | ||
+ | This mental math technique, as well as many others, | ||
- | **The char Data Type** | ||
- | Each char character such as a, A, b, or B has a unique numeric value associated with it that computers use to represent the characters. This is such since the computer can only store numeric code. The original ASCII set had only 128 characters, represented by 2^7. This ASCII character set has been expanded to 2^8, or 256 total characters. | ||
- | **The int Data Type** | ||
- | To specify a variable type as an integer, the int keyword is used. Integers are considered whole numbers, meaning that any fractional part is ignored. | ||
- | **Format Specifiers in fprintf() Function (for this assignment)** | ||
- | * %c: character | ||
- | * %d: integer | ||
- | * %s: string | ||
- | * %u: unsigned integer | ||
- | **Adding h, hh, l, ll to format specifiers** | ||
- | * %hhu: half half unsigned char | ||
- | * %hhd: half half signed char | ||
- | * %lu: unsigned long int | ||
- | * %ld: signed long int | ||
- | * %llu: unsigned long long int | ||
- | * %lld: signed long long int | ||
- | * %hu: unsigned short int | ||
- | * %hd: signed short int | ||
- | **Difference between signed and unsigned data types** | ||
- | * signed data types accept both positive and negative values | ||
- | * unsigned data types accept ONLY positive values | ||
- | For example, an unsigned character has a range of values from 0 to 255 (2^8 -1), where an signed char could have a range from -128 (2^7) to 127 (2^7 -1). Additionally, | ||
- | **Converting to Decimal to Hex or Binary** | ||
- | Binary is a 2-based number system where each digit, called a bit can be either 0 or 1. Hex values range from 0 to F in a 4 bit size, meaning there are 16 possibilities. | ||
- | Lets say for example there is a decimal number of 10. To convert to binary, consider that 10= 2^3 +2^1. Then let's consider that there is (1) 2^3, (0) 2^2, (1) 2^1, (0) 2^0. Then, 1*2^3 + 0*2^2 + 1*2^1 + 0* 2^0 = 1010 in binary. Since there are only 0 through 9 digits, in hex, 10 = A, 11 = B, 12 = C, 13 = D, 14 = E, and 15 = F. Thus to convert the decimal value of 10 to hex, the hex representation would be A. | ||
- | **Bitwise Operators (used in this assignment)** | + | =====Scope===== |
- | These are used to perform " | + | The motivation behind this project is to become familiarized with applying a pointer in order to use the Scanf() feature to acquire user input for computing the squares of two-digit integers ending |
- | * &: | + | =====Attributes===== |
- | * |: the bitwise OR operator (results in 1 if one or either bit is equal to one.) | + | State and justify |
- | **sizeof() function** | + | |
- | Yields | + | |
+ | | ||
+ | =====Procedure===== | ||
+ | My first attempt at writing | ||
+ | Example 1 (My first submitted program): | ||
+ | #include < | ||
+ | #include < | ||
+ | int main() | ||
+ | { | ||
+ | | ||
+ | int i; | ||
+ | int *p; | ||
+ | p=&i; | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | if(i==15){ | ||
+ | printf(" | ||
+ | else if(i==25){ | ||
+ | printf(" | ||
+ | else if(i==35){ | ||
+ | printf(" | ||
+ | else if(i==45){ | ||
+ | printf(" | ||
+ | else if(i==55){ | ||
+ | printf(" | ||
+ | else if(i==65){ | ||
+ | printf(" | ||
+ | else if(i==75){ | ||
+ | printf(" | ||
+ | else if(i==85){ | ||
+ | printf(" | ||
+ | else if(i=95){ | ||
+ | printf(" | ||
+ | else | ||
+ | printf(" | ||
- | =====Scope===== | + | return(0); |
- | The motivation behind this project is to become familiar with a number of data types available to us in C programming. The assumptions are that a program will be written successfully to acquire information related to each data type specified including, how much space is allocated to each data type, how many unique numbers are possible, and the available ranges of values | + | } |
- | The specific data types explored in this project are (includes signed | + | After more consideration, |
- | * char | + | |
- | * short int | + | |
- | * int | + | |
- | * long int | + | |
- | * long long int | + | |
- | =====Attributes===== | + | |
- | State and justify | + | |
- | * ability to edit code: this project will help immensely since the data types must be declared | + | Example 2 (The second program using modulus operator |
- | * ability to convert to hexadecimal: this is a requirement to use the bitwise AND/OR logical operators per data type | + | |
- | * ability to obtain the low (negative values) for the signed data types. | + | |
- | =====Procedure===== | + | |
- | The actual steps taken to accomplish the project. Include images, code snippets, command-line excerpts; whatever is useful for intuitively communicating important information for accomplishing the project. | + | |
- | =====Code===== | ||
- | <code c> | ||
- | ''/ | ||
- | for the signed and unsigned data types in C.; | ||
+ | #include < | ||
+ | #include < | ||
- | written by: Alana Whittier for CSCS1320S14; | + | int main() |
+ | { | ||
+ | |||
+ | int i; | ||
+ | int factor, R; | ||
+ | int *p; | ||
+ | p=&i; | ||
- | on February | + | printf(" |
+ | scanf(" | ||
+ | |||
+ | factor = i/10; | ||
+ | R = i % 10; | ||
+ | |||
+ | if(R==5) | ||
+ | printf(" | ||
+ | if(i> | ||
+ | printf(" | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | return(0); | ||
+ | } | ||
+ | |||
+ | |||
+ | =====Code===== | ||
+ | <code c> | ||
+ | /*Squares.c - A program which implements a mental math; | ||
+ | technique for computing the square of any; | ||
+ | two digit integer ending in 5, given by | ||
+ | user input; | ||
+ | |||
+ | Written by: Alana Whittier for CSCS1320S14 | ||
- | Compile with: gcc -o datatypes datatypes.c; | + | Compile with: gcc- - squares squares.c; |
- | Execute with: ./datatypes | + | Execute with: ./squares |
*/ | */ | ||
#include < | #include < | ||
+ | #include < | ||
+ | |||
int main() | int main() | ||
{ | { | ||
- | unsigned char uchr = 0; //unsigned char code | + | |
- | fprintf(stdout, | + | int i; |
- | fprintf(stdout, | + | int *p; |
- | fprintf(stdout, | + | p=&i; |
- | fprintf(stdout, | + | |
- | uchr = uchr -1; // | + | |
- | fprintf(stdout, | + | |
- | signed char schr = 0; //signed char code | + | printf("type up to a two digit integer ending in 5;\n"); |
- | fprintf(stdout, | + | scanf("%d", |
- | fprintf(stdout, | + | if(i==15){ |
- | fprintf(stdout, "low: %hhd, ", (schr | -0X80)); //returns low value for signed char (need to type cast AND change to bitwise OR) | + | |
- | fprintf(stdout, | + | else if(i==25){ |
- | schr = schr -1; // | + | |
- | fprintf(stdout, | + | else if(i==35){ |
- | + | | |
- | unsigned short int usi = 0; //unsigned short int code | + | else if(i==45){ |
- | fprintf(stdout, | + | |
- | fprintf(stdout, " | + | else if(i==55){ |
- | fprintf(stdout, | + | |
- | fprintf(stdout, | + | else if(i==65){ |
- | usi = usi -1; // | + | |
- | fprintf(stdout, | + | else if(i==75){ |
- | + | | |
- | signed short int ssi = 0; //signed short int code | + | else if(i==85){ |
- | | + | |
- | fprintf(stdout, | + | else if(i=95){ |
- | fprintf(stdout, | + | |
- | fprintf(stdout, | + | else |
- | ssi = ssi -1; //decrement | + | printf("Error: Invalid Entry\n"); |
- | fprintf(stdout, | + | |
- | + | ||
- | unsigned int ui = 0; //unsigned int code | + | |
- | fprintf(stdout, | + | |
- | | + | |
- | fprintf(stdout, | + | |
- | fprintf(stdout, | + | |
- | ui = ui -1; // | + | |
- | fprintf(stdout, | + | |
- | + | ||
- | signed int si = 0; //signed int code | + | |
- | fprintf(stdout, | + | |
- | | + | |
- | fprintf(stdout, | + | |
- | fprintf(stdout, | + | |
- | si = si -1; | + | |
- | fprintf(stdout, | + | |
- | + | ||
- | unsigned long int uli = 0; //unsigned long int code | + | |
- | fprintf(stdout, | + | |
- | | + | |
- | fprintf(stdout, | + | |
- | fprintf(stdout, | + | |
- | uli = uli -1; // | + | |
- | fprintf(stdout, | + | |
- | + | ||
- | signed long int sli = 0; //signed long int code | + | |
- | | + | |
- | fprintf(stdout, | + | |
- | fprintf(stdout, | + | |
- | fprintf(stdout, | + | |
- | sli = sli -1; //decrement | + | |
- | fprintf(stdout, | + | |
- | unsigned long long int ulli = 0; //unsigned long long int code | ||
- | fprintf(stdout, | ||
- | fprintf(stdout, | ||
- | fprintf(stdout, | ||
- | fprintf(stdout, | ||
- | ulli = ulli -1; //decrement | ||
- | fprintf(stdout, | ||
- | | ||
- | signed long long int slli = 0; //signed long long int code | ||
- | fprintf(stdout, | ||
- | fprintf(stdout, | ||
- | fprintf(stdout, | ||
- | fprintf(stdout, | ||
- | slli = slli -1; //decrement | ||
- | fprintf(stdout, | ||
- | |||
- | } | ||
+ | return(0); | ||
+ | } | ||
+ | |||
=====Execution===== | =====Execution===== | ||
<cli> | <cli> | ||
- | lab46: | + | lab46: |
- | lab46: | + | lab46: |
- | lab46: | + | lab46: |
- | TYPE: unsigned char, bytes: 1, low: 0, high: 255, qty: 256 | + | type up to a two digit integer ending in 5; |
- | TYPE: signed char, bytes: 1, low: -128, high: 127, qty: 256 | + | 25 |
- | TYPE: unsigned short int, bytes: 2, low: 0, high: 65535, qty: 0 | + | 25: 625 |
- | TYPE: | + | lab46:~/ |
- | TYPE: unsigned int, bytes: 4, low: 0, high: 4294967295, qty: 0 | + | type up to a two digit integer ending in 5; |
- | TYPE: signed int, bytes: 4, low: -2147483648, | + | 55 |
- | TYPE: unsigned long int, bytes: 8, low: 0, high: 18446744073709551615, | + | 55: 3025 |
- | TYPE: signed long int, bytes: 8, low: -9223372036854775808, | + | lab46:~/ |
- | TYPE: unsigned long long int, bytes: 8, low: 0, high: 18446744073709551615, | + | type up to a two digit integer ending in 5; |
- | TYPE: | + | 60 |
+ | Error: Invalid Entry | ||
+ | lab46:~/ | ||
+ | type up to a two digit integer ending in 5; | ||
+ | 95 | ||
+ | 95: 9025 | ||
+ | lab46:~/ | ||
+ | type up to a two digit integer ending in 5; | ||
+ | 100 | ||
+ | Error: Invalid Entry | ||
+ | lab46:~/ | ||
</ | </ | ||
=====Reflection===== | =====Reflection===== | ||
- | Considering the difficulties | + | In writing this program, |
+ | Second mistake: The way I used the curly braces. Essentially, I was embedding several else if statements within | ||
- | **Observations** | + | Since my initial attempt at writing this program included the modulus operator, which had been quickly abandoned, I decided to revisit that since I now had a working program written in a manner I felt was more simplistic. What I realized that I was missing from my initial program was the factor (the number of times that 10 went into the integer). For example, if I declared the factor to be i/10 and R= i%10, then for an integer value of 25, R=5 and the factor=2, since 10 can go into 25 twice, with a remainder of 5. With this program written this way, the same mental math technique works for 3 and 4 digit integers ending in 5 as well, to compute the squares. This code is shown above in Example 2 of the Procedure section. |
- | The long and long long int (signed and unsigned) appear the same. This is because they are both 64 bit and that is the most the compiler can handle. | ||
- | printf() and fprintf() basically do the same thing. The difference being that printf can only print on the monitor, has the default stream of STDOUT, while fprintf can print to a user defined stream (or file). In our project, fprintf uses the STDOUT to the screen AS if it were a file. | ||
- | STDOUT is by default printed to the screen unless user specified. | ||
- | %s is the format specifier used to print a string of characters, %hhu is the format specifier for half half unsigned char, % hu is the format specifier for unsigned short int. | ||
- | |||
- | The difference between %u and %d are that %u denotes an unsigned int type, while %d denotes a signed int type. | ||
- | |||
- | Considering the 13 in %13 in the first stanza for unsigned char in the program, this just specifies the number of characters in the string, including spaces to be printed for " | ||
- | |||
- | If a sign is left unspecified, | ||
- | |||
- | The & and | operators are the bitwise logic operators, which in our case took the hex representation of our data types to help us to obtain the appropriate high/low values within our ranges. | ||
- | |||
- | I experienced some difficulty in initial attempts to obtain the low values for the signed data types. I later learned that not only did I need to change the expression for the " | ||
- | |||
- | Based on my program' | ||
- | * signed char = 8 bits | ||
- | * unsigned short int = 16 bits | ||
- | * unsigned int = 32 bits | ||
- | * signed int = 32 bits | ||
- | * signed long long int = 64 bits | ||
- | |||
- | However, due to the decrementing and incrementing per data type, only the unsigned char actually stored ANY memory at all and stored a total of 16 bits! | ||
Line 257: | Line 227: | ||
In performing this project, the following resources were referenced: | In performing this project, the following resources were referenced: | ||
- | * http://en.cppreference.com/w/cpp/language/types1 | + | * http://wildaboutmath.com/2007/11/11/impress-your-friends-with-mental-math-tricks/comment-page-6/ |
- | * http://www.youtube.com/ | + | * http://saurabhg.hubpages.com/hub/ |
- | * http://www.binaryhexconverter.com/decimal-to-hex-converter | + | * Kernighan, Ritchie |
- | * http://en.wikipedia.org/wiki/Signed_number_representations | + | * email consultations and guidance from Matt Haas |