Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 int sum(int array[], int arrayLength) { int i = 0; int totalSum = 0; while(i < arrayLength) { totalSum += array[i]; i++; } return totalSum; } int main() { int arr[] = {1, 7, 9}; printf("hello, world "); printf("The total sum is %d ", sum(arr, 3)); }

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke

1st Edition

0130086509, 978-0130086501

Students also viewed these Databases questions