Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

question 1 the code must be in c++ for both Add Negatives The existing code is t

ID: 641410 • Letter: Q

Question

question 1 the code must be in c++ for both

Add Negatives

The existing code is trying to call a function addNegatives that should take an array of ints and its size and return the sum of JUST the negative numbers from the array. Your task is to write the missing function.

Example: given input of an array {-4 2 1 -3} and size 4 you should print -7

#include <iostream>

using namespace std;


//Do not modify anything on or above the line below this
//START_PROVIDED

//YOUR_CODE


//END_PROVIDED
//Do not modify anything on or below the line above this

int main()
{
    int NUM_VALUES; //cheating to make this flexible
    cin >> NUM_VALUES;
    int values[NUM_VALUES];
    //Read in values
    for(int i = 0; i < NUM_VALUES; i++) {
        cin >> values[i];
    }

    int sum = addNegatives(values, NUM_VALUES);
    cout << sum;
}

and question 2 square arrays

The existing codetries to make use of a function void squareArray(int array[], int destination[], int size). This function should take each of the size elements from array, square the element, and place the answer into the same location in the destination array

i.e. if array is {1 2 3 4} and size is 4, when your function returns, destination should be {1 4 9 16}. uare array

#include <iostream>

using namespace std;


//Do not modify anything on or above the line below this
//START_PROVIDED


    //YOUR_CODE


//END_PROVIDED
//Do not modify anything on or below the line above this

int main()
{
    int NUM_VALUES; //cheating to make this flexible
    cin >> NUM_VALUES;

    int values[NUM_VALUES];
    //Read in values
    for(int i = 0; i < NUM_VALUES; i++) {
        cin >> values[i];
    }

    int squares[NUM_VALUES];

    //Call your function
    squareArray(values, squares, NUM_VALUES);

    for(int i = 0; i < NUM_VALUES; i++) {
        cout << squares[i] << " ";
    }

    return 0;
}

Explanation / Answer

#include <iostream>

using namespace std;


//Do not modify anything on or above the line below this
//START_PROVIDED

//YOUR_CODE

int addNegatives(int values[], int size){
   int sum = 0;
   for(int i = 0; i < size; ++i){
       if(values[i] < 0){
           sum += values[i];
       }
   }
   return sum;
}


//END_PROVIDED
//Do not modify anything on or below the line above this

int main()
{
int NUM_VALUES; //cheating to make this flexible
cin >> NUM_VALUES;
int values[NUM_VALUES];
//Read in values
for(int i = 0; i < NUM_VALUES; i++) {
cin >> values[i];
}

int sum = addNegatives(values, NUM_VALUES);
cout << sum;
}

_______________________________________________________________________________________

#include <iostream>

using namespace std;


//Do not modify anything on or above the line below this
//START_PROVIDED


//YOUR_CODE
void squareArray(int array[], int destination[], int size){
   for(int i = 0; i < size; ++i){
       destination[i] = array[i] * array[i];
   }
}


//END_PROVIDED
//Do not modify anything on or below the line above this

int main()
{
int NUM_VALUES; //cheating to make this flexible
cin >> NUM_VALUES;

int values[NUM_VALUES];
//Read in values
for(int i = 0; i < NUM_VALUES; i++) {
cin >> values[i];
}

int squares[NUM_VALUES];

//Call your function
squareArray(values, squares, NUM_VALUES);

for(int i = 0; i < NUM_VALUES; i++) {
cout << squares[i] << " ";
}

return 0;
}