This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
user:psechris:portfolio:cprogproject6 [2013/03/11 14:29] – [Objectives] psechris | user:psechris:portfolio:cprogproject6 [2013/03/11 14:38] (current) – psechris | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ======Project: | ||
+ | A project for CSCS 1320 by Paul Sechrist during the Spring 2013. | ||
+ | |||
+ | =====Objectives===== | ||
+ | This project is to help learn about pointers, what they are used for, and how to use them. | ||
+ | |||
+ | =====Code===== | ||
+ | |||
+ | ==== Pointer Fun 1 ==== | ||
+ | |||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | char a, *b; | ||
+ | |||
+ | a = ' | ||
+ | b = &a; | ||
+ | | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | ==== Pointer Fun 2 ==== | ||
+ | |||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | char *a; | ||
+ | | ||
+ | a = (char *) malloc (sizeof(char)); | ||
+ | | ||
+ | *a = 48; | ||
+ | |||
+ | // | ||
+ | | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Pointer Arrays ==== | ||
+ | <code c 1> | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define NUM_SCORES 5 | ||
+ | #define SCORE0 87 | ||
+ | #define SCORE1 92 | ||
+ | #define SCORE2 97 | ||
+ | #define SCORE3 83 | ||
+ | #define SCORE4 79 | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | unsigned char *scores; | ||
+ | float average; | ||
+ | |||
+ | scores = (unsigned char *) malloc (sizeof(unsigned char) * NUM_SCORES); | ||
+ | *(scores+0) = SCORE0; | ||
+ | *(scores+1) = SCORE1; | ||
+ | *(scores+2) = SCORE2; | ||
+ | *(scores+3) = SCORE3; | ||
+ | *(scores+4) = SCORE4; | ||
+ | |||
+ | /* Please provide the equation to do average of the scores */ | ||
+ | //average = EQUATION TO DO AVERAGE | ||
+ | | ||
+ | /* Display the scores via array referencing and then the average. | ||
+ | * %f can be used to display a floating point value. */ | ||
+ | // | ||
+ | | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | =====Execution===== | ||
+ | |||
+ | <cli> | ||
+ | |||
+ | </ | ||
+ | |||
+ | =====Reflection===== | ||
+ | ==== Pointer Fun 1 ==== | ||
+ | |||
+ | * What do the variables a and b have in common? | ||
+ | * Where do they differ? | ||
+ | * If you change variable **a**, what will happen to **b**? | ||
+ | * If you change variable **b**, will it impact **a**? | ||
+ | * How can you change **b** without breaking things? | ||
+ | ==== Pointer Fun 2 ==== | ||
+ | * If we uncomment that **fprintf()** line, what will be displayed? | ||
+ | * What is **malloc()** doing? | ||
+ | * Why is it needed? | ||
+ | |||
+ | =====References===== |