Could you solve this problem using C++? ----------------------------------------
ID: 3604743 • Letter: C
Question
Could you solve this problem using C++?
--------------------------------------------
You will need to write the following functions in array.cpp, add function prototypes for them to array.h and invoke the functions in main.cpp. You should label the output of your test, such as “the list after removal: “ etc.
• int sumOfArray(int list[], int size) compute and return the sum of integers in list
• int remove(int list[], int& size, int target)
remove all the integers that equals “target” in the list and update the size of the list
---------------------------------------------
//array.cpp
#include "array.h"
//put the implementations of your assigned functions here
---------------------------------------------
#ifndef ARRAY_H
#define ARRY_H
//array.h
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
/* These functions are already written and can be called to test out your code */
void build(int list[], int size); //supplied
void display(int list[], int size); //supplied
/* *****************YOUR TURN! ******************************** */
//Write your function prototype here:
#endif
~
----------------------------------------------
// main.cpp
#include "array.h"
using namespace std;
const int CAP = 100;
int main()
{
int list[CAP];
int size = 10;
build(list, size);
display(list, size);
//PLEASE PUT YOUR CODE HERE to call the function assigned
display(list, size);
return 0;
}
Explanation / Answer
Please find my implementation of required functions.
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
/* These functions are already written and can be called to test out your code */
void build(int list[], int size); //supplied
void display(int list[], int size); //supplied
int sumOfArray(int list[], int size); // compute and return the sum of integers in list
int remove(int list[], int& size, int target);
/* *****************YOUR TURN! ******************************** */
//Write your function prototype here:
int sumOfArray(int list[], int size) {
int sum = 0;
for(int i=0; i<size; i++) {
sum = sum + list[i];
}
return sum;
}
int remove(int list[], int& size, int target) {
int i= 0;
for(int k=0; k<size; k++) {
if(list[k] != target) {
list[i] = list[k];
i++;
}
}
size = k;
}
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.