Question

Step 4: Write a Sum Function Since we can write, compile and run simple c files,...

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 <stdio.h>

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\n");
    printf("The total sum is %d\n", 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\n");
    printf("The total sum of ");
    printIntegerArray(arr, 3);
    printf(" is %d\n", sum(arr, 3));
  
    return 0;
}


0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>

int sum(int array[], int arrayLength) {

    int i = 0;

    int totalSum = 0;

   

    while(i < arrayLength) {

        totalSum += array[i];

        i++;

    }

   

    return totalSum;

}

void printIntegerArray(int arr[], int arrLength)

{

    int i;

   

    for( i = 0 ; i < arrLength - 1 ; i++ )

        printf("%d, ", arr[i]);

       

    printf("%d", arr[arrLength - 1]);

}

int main() {

    int arr[] = {1, 7, 9};

   

    printf("hello, world\n");

    printf("The total sum of ");

   printIntegerArray(arr, 3);

    printf(" is %d\n", sum(arr, 3));

   

    return 0;

}

Sample Output

hello, world he total sum of 1, 7. 9 is 1?

Add a comment
Know the answer?
Add Answer to:
Step 4: Write a Sum Function Since we can write, compile and run simple c files,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT