This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
haas:spring2016:cprog:projects:gfo0 [2016/04/12 16:42] – [function calling] wedge | haas:spring2016:cprog:projects:gfo0 [2016/04/13 16:46] (current) – [Bonus] wedge | ||
---|---|---|---|
Line 7: | Line 7: | ||
======Project: | ======Project: | ||
+ | |||
+ | =====Errata===== | ||
+ | |||
+ | * __correction__: | ||
=====Objective===== | =====Objective===== | ||
Line 24: | Line 28: | ||
This information has been accessible via the **status** tool. | This information has been accessible via the **status** tool. | ||
+ | =====Program===== | ||
+ | Throughout the semester, your class-related activities have been numerically captured and made available to you via the **status** tool. | ||
- | =====Task===== | + | Now that we are nearing the end, and your programming skillset is significantly expanded, I would like you to write a program to take this data and calculate your grade (reflective of currently recorded data). |
- | The task at hand can benefit from loop and array assistance. | + | |
- | For instance, taking the number input and processing | + | I've designed a tool that will take all your **status** data and ' |
- | =====Functions===== | + | For example: |
- | As indicated, this task shares many attributes with the **mbe1** project; in fact, the mental math process itself may be slightly simpler. That affords us the opportunity to introduce and learn about further programming optimizations, | + | |
- | Specifically, | + | < |
+ | lab46: | ||
+ | lab46: | ||
+ | -1 0 1 1 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 -2 1 1 1 0 1 0 1 0 1 0 0 1 -3 11 13 6 9 4 9 5 9 6 9 19 20 6 0 18 20 -4 | ||
+ | lab46: | ||
+ | </ | ||
- | We've been using functions all along (everytime you use **fprintf()** or **fscanf()**, | + | Your program needs to do the following: |
- | ====Function prototype==== | + | * check for and open the **status.flat** file |
- | Like variables, functions need to be declared. | + | * if error occurs, display message and exit |
+ | * reading one integer at a time, load the values into an array according | ||
+ | * there will be three categories (arrays), each one identified by a header/ | ||
+ | * -1 indicates the start of participation data (28% of grade) | ||
+ | * -2 indicates end of participation data / start of journal data (36% of grade) | ||
+ | * -3 indicates end of journal data / start of project data (36% of grade) | ||
+ | * -4 indicates end of project data (and end of data stream) | ||
+ | * once loaded into their arrays, calculate the grade component | ||
+ | * tally up all three, and print out the results | ||
- | We can declare them at various scopes (file/ | + | Final program |
- | If a particular function is only to be used by a specific function, and no others, you can opt to declare it local scope (ie within the function that will be calling it). | + | <cli> |
- | + | lab46:~/src/cprog/gfo0$ ./mygrade | |
- | A function is basically a module or subroutine. It is a mini-program, | + | Participation Component: 20 / 28 |
- | + | Journal Component: 20 / 36 | |
- | Like a program, it takes input, does processing, and provides output. | + | Projects Component: |
- | + | Total: 70 /100 | |
- | Unlike a program, its input may not come from the keyboard, but instead from particular variables, and may not send output to the screen, but instead channel output in a way that it can be stored into a variable. | + | lab46:~/src/ |
- | + | </cli> | |
- | This distinctions aside, a function can in many ways be viewed as a micro- or sub-program/routine. We use functions to assist us in making our code more readable/organized/navigable. | + | |
- | + | ||
- | Keeping everything in ONE file, ONE big function in that one file, is rather monolithic. In time, with sufficiently large programs, such an arrangement would become a tad unwieldy. So functions help to keep our focus short yet attentive. | + | |
- | + | ||
- | To create a function we must first declare (or prototype) it. This needs to happen BEFORE said function is ever used (just as with variables- you must declare a variable before it is first used, otherwise the compiler yells). | + | |
- | + | ||
- | A function, in many ways, is like a programmable variable (or is a variable with programming attached). | + | |
- | + | ||
- | As such, it has a return value of a type (the function' | + | |
- | + | ||
- | We see this with main()... here are two variations of a **main()** function declaration (technically also the start of the definition as well, in the case of **main()**): | + | |
- | + | ||
- | ===Parameterless function=== | + | |
- | + | ||
- | <code c> | + | |
- | int main() | + | |
- | </code> | + | |
- | + | ||
- | In this example, we see the declaration of main() where it has a return value of **int**, meaning, upon completion, main() will return a value corresponding with an int data type (also in main()' | + | |
- | + | ||
- | main(), in this case, takes no parameters (just an empty set of parenthesis)... due to this, we refer to this function as a parameterless function. A function without parameters. Without input. | + | |
- | + | ||
- | Now: this is technically a different form of input and output than you are used to. Input doesn' | + | |
- | + | ||
- | Additionally, | + | |
- | + | ||
- | ===Parametered function=== | + | |
- | + | ||
- | <code c> | + | |
- | int main(int argc, char **argv) | + | |
- | </code> | + | |
- | + | ||
- | In this case, our **main()** function actually takes parameters- two, in fact: | + | |
- | + | ||
- | - an integer, we are calling **argc** | + | |
- | - a double pointer, we are calling **argv** | + | |
- | + | ||
- | This function takes two parameters, two pieces of input, available to us in the form of variables, by those names, of those types. We make use of them as we need to in accomplishing the program at hand. | + | |
- | + | ||
- | So, when we wish to create functions of our own, we need: | + | |
- | + | ||
- | * the return type | + | |
- | * the function name | + | |
- | * 0 or more parameters, identifying their order and type | + | |
- | + | ||
- | For example, let us make a sum() function. Here would be a likely prototype (we'd place it above main()): | + | |
- | + | ||
- | < | + | |
- | int sum(int *, int); | + | |
- | </code> | + | |
- | + | ||
- | A function prototype (vs. its definition) will have a terminating semi-colon, as you see above. | + | |
- | + | ||
- | In our case, our sum() function has the following: | + | |
- | + | ||
- | * a return type of **int** (particular variable name doesn' | + | |
- | * the function' | + | |
- | * a comma-separated list of types corresponding to the parameters (again, variable names do not matter, but the type is important). | + | |
- | + | ||
- | Our sum() function will take an integer array (denoted by the int pointer above), and a size (the second, regular int). | + | |
- | + | ||
- | Now, parameter order very much matters. In our case, an "int *" came first, followed by an " | + | |
- | + | ||
- | ====Function definition==== | + | |
- | While a function prototype is technically optional (you can put the definition in place of the prototype-- we just often use prototypes to further allow organization), | + | |
- | + | ||
- | Our sum() function will be defined (below the main() function) as follows: | + | |
- | + | ||
- | <code c> | + | |
- | int sum(int *array, int size) | + | |
- | { | + | |
- | int result = 0; | + | |
- | int i = 0; | + | |
- | + | ||
- | for (i = 0; i < size; i++) | + | |
- | result = result + array[i]; | + | |
- | + | ||
- | return(result); | + | |
- | } | + | |
- | </code> | + | |
Line 159: | Line 96: | ||
You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches. | You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches. | ||
- | ====0x4: Data Processing==== | ||
- | |||
- | Throughout the semester, your class-related activities have been numerically captured and made available to you via the **status** tool. | ||
- | |||
- | Now that we are at the end, and your programming skillset is significantly expanded, I would like you to write a program to take this data and calculate your grade (reflective of currently recorded data). | ||
- | |||
- | I've designed a tool that will take all your **status** data and ' | ||
- | |||
- | For example: | ||
- | |||
- | <cli> | ||
- | lab46: | ||
- | lab46: | ||
- | -1 0 1 1 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 -2 1 1 1 0 1 0 1 0 1 0 0 1 -3 11 13 6 9 4 9 5 9 6 9 19 20 6 0 18 20 -4 | ||
- | lab46: | ||
- | </ | ||
- | |||
- | Your program needs to do the following: | ||
- | |||
- | * check for and open the **status.flat** file | ||
- | * if error occurs, display message and exit | ||
- | * reading one integer at a time, load the values into an array according to their category | ||
- | * there will be three categories (arrays), each one identified by a header/ | ||
- | * -1 indicates the start of participation data (28% of grade) | ||
- | * -2 indicates end of participation data / start of journal data (36% of grade) | ||
- | * -3 indicates end of journal data / start of project data (36% of grade) | ||
- | * -4 indicates end of project data (and end of data stream) | ||
- | * once loaded into their arrays, calculate the grade component | ||
- | * tally up all three, and print out the results | ||
- | |||
- | Final program output should resemble the following: | ||
- | |||
- | <cli> | ||
- | lab46: | ||
- | Participation Component: 20 / 28 | ||
- | Journal Component: | ||
- | Projects Component: | ||
- | Total: | ||
- | lab46: | ||
- | </ | ||