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
  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • 2. Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics...

    2. Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and dereferencing. b) Write a function “void computeMinMax(double arr[], int length, double * min, double * max)” that finds the minimum and the maximum values in an array and stores the result in min and max. You are only allowed to use a single for loop in the function (no while loops). Find both the min and the max using the same for loop....

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++...

    How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++ .data # Defines variable section of an assembly routine. array: .word x, x, x, x, x, x, x, x, x, x # Define a variable named array as a word (integer) array # with 10 unsorted integer numbers of your own. # After your program has run, the integers in this array # should be sorted. .text # Defines the start of the code...

  • c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each...

    c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...

  • Please help with this question (the two functions). I've attempted it but it's not working. We...

    Please help with this question (the two functions). I've attempted it but it's not working. We have to complete the 2 functions int largest(int * x); and void display(int *arr); (one prints the values in array and the other gets max value in array). But we can only use pointer indirection and address arithmetic to access and traverse the array. No array index [] should be used in the functions. C PROGRAMMING: ----------------- /* Passing an array to a function....

  • If void * is a pointer to void is a "generic" pointer type, and a void...

    If void * is a pointer to void is a "generic" pointer type, and a void * can be converted to any other pointer type * without an explicit cast, why in the ,myarrcopy function the malloc is created like char and not like void? if we are going to work with different type of arrays? Here is de program: *This is a function that make a copy of any given array. * We then use this function make a...

  • Write a program in C that creates an array of 100 random numbers from 0-99. The...

    Write a program in C that creates an array of 100 random numbers from 0-99. The program sums the random numbers and prints the sum.  It then writes the numbers to a new file using open, close and write. Then looks in the current directory for files that match the pattern “numbers.XXXX”. For each file, open the file and read the file. You can assume that the file will contain 100 integers. Sum the integers. Print the filename and the sum...

  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

  • Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and...

    Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and dereferencing. b) Write a function “void computeMinMax(double arr[], int length, double * min, double * max)” that finds the minimum and the maximum values in an array and stores the result in min and max. You are only allowed to use a single for loop in the function (no while loops). Find both the min and the max using the same for loop. Remember...

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