Question
Step 4: Write a Sum Function Since we can write, compile and run simple c files, lets add a bit to make a program that
Step 4: Write a Sum Function
Since we can write, compile and run simple c files, lets add a bit to make a program that will sum an entire array of integers. To do this we are going to simply overwrite the main.c file to include the new function. The sum function below is not complete and must be finished before the program will execute properly.
%%file main.c #include
We are now going to edit the file now that you know which directory your main.c file is in. To edit the file 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 "main.c" file to print the values of the array separated by commas. i.e. the integer array used in main will print as "1, 7, 9". There is a function skeleton you can use in the cell below. Main should also be changed to print the value list of integers and the sum. So there are two things to be done,
First, write the code for the "printIntegerArray" function.
Then edit the main function to print the values of the array before the sum.
void printIntegerArray(int arr[], int arrLength) { while (){void printIntegerArray(int arr[], int arrLength) { } } int main() { int arr[] = {1, 7, 9}; printf("hello, world "); printf("The total sum of "); printIntegerArray(arr, 3); printf(" is %d ", sum(arr, 3)); return 0; }
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