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++ File I/O Create a program that will work for all "infile.txt" of similar structure: The...

    C++ File I/O Create a program that will work for all "infile.txt" of similar structure: The first line will contain two integers. The first integer is the number of elements in array1, and the second integer the number of elements in array2. The second line will contain all the values of array1, and the third line will contain all values of array2. All numbers are separated by a space and all lines separated by a newline character. "infile.txt" has three...

  • (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]

  • c++ please help Create two arrays of 20 elements, called myArray1 and myArray2 Create a function...

    c++ please help Create two arrays of 20 elements, called myArray1 and myArray2 Create a function randBetween that returns an integer between 2 integer inputs (int randBetween(int min, int max)); Fill the myArray1 with the random values. Loop thru the myArray1 and find the difference between the element i and element i + 1; put that difference in array2 example: array1 = {1, 3, 4, 5} array2 ends up having {-2, -1, -1} Loop thru myArray1 and find the min...

  • 1. Define an array of ints with the ten elements { 0, 1,2,3, 4, 5, 6,...

    1. Define an array of ints with the ten elements { 0, 1,2,3, 4, 5, 6, 7, 8, 9 }. 2. Define a vector with those ten elements. 3. Define a list with those ten elements. 4. Define a second array, vector, and list, each initialized as a copy of the first array, vector, and list, respectively. 5. Increase the value of each element in the array by 2; increase the value of each element in the vector by 3;...

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