Question
To start this lab, we are going to create a file named lab1.c. In the lab1.c file we will write the main function that will
To start this lab, we are going to create a file named "lab1.c". In the "lab1.c" file we will write the main function that will print a "hello, world" program. Just like in lab1a, we can use %% to run a multi-line expression. To run the highlighted cell either click the run cell at the top of your screen or press "shift enter".
If the cell above says, "Writing main.c" above, the file has been created. Before we move on to compiling files we're going to create a header file to go with the lab1.c file. The included header file is for self-defined data structures, function prototypes and included libraries (I like to put most of my documentation in header files but that is just personal preference). To include a header file, we need to create a directory named "include".
%%file lab1.c /*** * Name: * Date: * Course: * * Description: ***/ #include "lab1.h" int main() { printf("hello, world "); return 0; } %%bash mkdir include %%file include/lab1.h /*** * Name: * Date: * Course: * * Description: ***/ #include
Step 4: Write a Sum Function
We are now going to add a sum function like in lab one, but this time we need to fill in the blank code blocks and add a function prototype to the lab1.h file. We also need to debug any of the errors in the code!
Hint: There is a total of 3 errors between the "lab1.c" code and bash script for compiling.
Note: You can call the sum function below the "lab1.h" function without the prototype in the "lab1.c" file because the compiler reads the prototype before calling "main" when reading the "lab1.h" header file.
%%file include/lab1.h /*** * Name: * Date: * Course: * * Description: ***/ /*** * Included libraries ***/ /* Standard input/output in c */ #include
Step 6: Using The Command Line
Now that we have learned the basics of using a Jupyter Notebook (writing, compiling and running C programs), it's time to do it with the command line. Navigate to the location you created your "lab1.c" file. Don't worry if you're not sure where you saved the "lab1.c" file, just type the following bash script to print work directory and check the location.
We are now going to edit the file now that you know which directory your "lab1.c" file is in. Just like we did before, we need to cd to the directory and open your favourite text editor on the virtual machine. Nano, vim, or atom will be fine but atom is likely the most user friendly.
Now your job is to add a single function in the "lab1.c" file to print the values of the array separated by commas and the last has an and written in front of it. i.e. the integer array used in "main" will print as "1, 6, 7 and 9". Use the function skeleton below to write the required code, you must use an if/else if statement in the loop instead of a switch statement. You should check what will happen when you use a switch statement fot this example.
First, write the code for the "printIntegerArray" function.
Then edit the "main" function to print the values of the array before the sum.
Note: Remember to prototype the new function in the "lab1.h" file or you will receive an implicit declaration statement from the compiler.
%%file include/lab1.h /*** * Included libraries ***/ /* Standard input/output in c */ #include
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started