Question

c++, need help thank you

Given an array of ints, return the sum of all elements from the array that come before the first element that equals number 4

0 0
Add a comment Improve this question Transcribed image text
Answer #1
int pre4(int array[], int size){
    int sum = 0;
    for(int i = 0;i<size;i++){
        if(array[i]==4){
            return sum;
        }
        sum += array[i];
    }
    return sum;
}
Add a comment
Know the answer?
Add Answer to:
c++, need help thank you Given an array of ints, return the sum of all elements...
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
  • Need help with java code, this is all in a method 1) 3 different arrays are...

    Need help with java code, this is all in a method 1) 3 different arrays are brought in 2) I create local array called arrayWithLargestValues which is the size of the largest array from the 3 brought in 3) I need to somehow fill the local 'arrayWithLargestValues' with the all different largest values from the 3 different arrays hopefully I made sense sample input/output int [ ] arrayWithLargestValues = new int [ ] // this has to be set to...

  • (C++) compute and return the sum of the ints contained in the Array table using RECURSION....

    (C++) compute and return the sum of the ints contained in the Array table using RECURSION. Start with the following function. int sum(node * head[]);    // // //This is the provided header file. //arr.h #include #include #include const int SIZE = 5;   //size of the array of head pointers struct node { int data; node * next; }; /* These functions are already written and can be called to test out your code */ void build(node * head[]); //supplied...

  • Last assignment: C programming Submission must include: a function, an array, and a menu system Goal...

    Last assignment: C programming Submission must include: a function, an array, and a menu system Goal is to create a program that will generate a random exercise for the target area selected and the number of times it will be done (all of the options except cardio) or the amount of time doing the task.(the cardio option), but I'm having trouble figuring out how to generate a random option using an array. This is what I have so far... #include...

  • Write a program to merge two sorted arrays into a third array. Ask the user to...

    Write a program to merge two sorted arrays into a third array. Ask the user to enter the size of the two arrays. The length of array1 could be greater than, equal to or less than the length of array2. Fill both with random numbers 0-99. Sort both arrays as explained below. Write a method merge that takes the two arrays as arguments. The method returns a merged array with size equal to the size of both arrays combined. Note:...

  • Consider a non-empty int array ints. A contiguous subarray ints[ start .. start + len -1...

    Consider a non-empty int array ints. A contiguous subarray ints[ start .. start + len -1 ] (with starting index start and length len) is called a flat if all elements of that subarray are equal. Furthermore, such a subarray is called a plateau if it is flat and each of the elements ints[start -1] and ints[start + len] that immediately proceed/succeed the subarray are either nonexistent (i.e., out of array’s index range) or are strictly smaller than the elements...

  • i need help writing these programs in c++ format 1. Enter two integer arrays: array1-129, 0,...

    i need help writing these programs in c++ format 1. Enter two integer arrays: array1-129, 0, -56, 4, -7 and array2-19, 12, -36, -2, 12 3. Write the code to form and display another array array3 in which each element is the sum of numbers in the position in both arrays. Use pointers and functions: get array0. process arrays0 and show array0 (50 points) 2. Enter a positive 5-digit integer via the keyboard. Display each digit and fol- lowed by...

  • Given an array of ints length 3, return a new array with the elements in reverse...

    Given an array of ints length 3, return a new array with the elements in reverse order, so (1, 2, 3) becomes (3, 2, 1). reverse3 ([1, 2, 3]) [3, 2, 1] reverse3(15, 11, 9])9, 11, reverse3(17, e, 0]) [e, e, 7] 5]

  • 1- Complete all the blanks in the function f1() below such that it implements the operations...

    1- Complete all the blanks in the function f1() below such that it implements the operations described in the comments. You may only fill in the blanks and not add or alter other code. Also complete main(). // f1 copies the last *half* of vec's values into a new array pointed to 'arr'. // 'arr' is garbage coming in and must be modified by this function so the // caller can then use the produced array. The values copied from...

  • (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element...

    (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....

  • C++ Language Given an array length 1 or more of ints, return the difference between the...

    C++ Language Given an array length 1 or more of ints, return the difference between the largest and smallest values in the array. Examples: largeDiff (410, 3, 5, 6}, 4) - 7 largeDiff ({7, 2, 10, 9}, 4) - 8 largeDiff ({2, 10, 7, 2}, 4) + 8 largeDiff ({2}, 1) = 0 code.cpp + New I Full Screen 2- int largeDiff (const int a[], int n) { 3 4 5}

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