Question

in C++ and also each function has its own main function so please do the main function as well. Previously when i asked the question the person only did the function and didn't do the main function so please help me do it.

1-1. Write a function that returns the sum of all elements in an int array. The parameters of the function are the array and

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have included my code and screenshots in this answer. In case, there is any indentation issue due to editor, then please refer to code screenshots to avoid confusion.

------------------function-1-1.cpp----------

int sum_array(int array[], int n) //returns sum of array
{
   if(n < 1) //if n is less than 1, then return 0
       return 0;

   int sum = 0;
   for(int i = 0; i < n; i++)
   {
       sum = sum + array[i]; //add each element to sum
   }
   return sum;
}

------------------main-1-1.cpp----------

#include <iostream>
#include "function-1-1.cpp" //include function
using namespace std;

int main()
{
   int arr1[6] = {2, 3, 4, 6, 11, 4}; //sample test arays
   int arr2[8] = {12, 13, 48, 6, 11, 4, 7, 10};

   cout << "\nThe array 1 is: ";
   for(int i = 0; i < 6; i++) //print array
   {
       cout << arr1[i] << ", ";
   }
   cout << "\nThe sum is: " << sum_array(arr1, 6) << endl; //print sum

   cout << "\nThe array 2 is: ";
   for(int i = 0; i < 8; i++) //print array
   {
       cout << arr2[i] << ", ";
   }
   cout << "\nThe sum is: " << sum_array(arr2, 8) << endl; //print sum
   return 0;
}

------------------function-1-2.cpp----------

double average(int array[], int n) //returns average of array
{
   if(n < 1) //if n is less than 1, then return 0.0
       return 0.0;

   double average;
   int sum = 0;
   for(int i = 0; i < n; i++)
   {
       sum = sum + array[i]; //add each element to sum
   }
   average = (double)sum/n; //type cast sum from int to double
   return average; //return average
}

------------------main-1-2.cpp----------

#include <iostream>
#include "function-1-2.cpp"
using namespace std;

int main()
{
   int arr1[6] = {2, 3, 4, 6, 11, 4}; //sample test arays
   int arr2[8] = {12, 13, 48, 6, 11, 4, 7, 10};

   cout << "\nThe array 1 is: ";
   for(int i = 0; i < 6; i++) //print array
   {
       cout << arr1[i] << ", ";
   }
   cout << "\nThe average is: " << average(arr1, 6) << endl; //print average

   cout << "\nThe array 2 is: ";
   for(int i = 0; i < 8; i++) //print array
   {
       cout << arr2[i] << ", ";
   }
   cout << "\nThe average is: " << average(arr2, 8) << endl; //print average
   return 0;
}

------------------function-1-3.cpp----------

int count(int array[], int n, int number) //returns count of number in array
{
   if(n < 1) //if n is less than 1, then return 0
       return 0;

   int count = 0;
   for(int i = 0; i < n; i++)
   {
       if(array[i] == number) //check if array element is same as number
           count++; //If yes then increment count
   }
   return count; //return count
}

------------------main-1-3.cpp----------

#include <iostream>
#include "function-1-3.cpp"
using namespace std;

int main()
{
   int arr[8] = {12, 10, 48, 6, 10, 4, 12, 10}; //sample test aray

   cout << "\nThe array is: ";
   for(int i = 0; i < 8; i++) //print array
   {
       cout << arr[i] << ", ";
   }
   cout << "\nThe count of is 10 is : " << count(arr, 8, 10) << endl; //print count of 10 in array
   cout << "\nThe count of is 12 is : " << count(arr, 8, 12) << endl; //print count of 12 in array
   cout << "\nThe count of is 3 is : " << count(arr, 8, 3) << endl; //print count of 3 in array
   return 0;
}

------------------function-1-4.cpp----------

int sum_two(int array[], int secondarray[], int n) //returns sum of two arrays
{
   if(n < 1) //if n is less than 1, then return 0
       return 0;

   int sum = 0;
   for(int i = 0; i < n; i++)
   {
       sum = sum + array[i] + secondarray[i]; //add both elements at index i to sum
   }
   return sum; //return sum
}

------------------main-1-4.cpp----------

#include <iostream>
#include "function-1-4.cpp"
using namespace std;

int main()
{
   int arr1[6] = {10, 20, 30, 40, 50}; //sample test arays
   int arr2[8] = {2, 4, 6, 8, 10};

   cout << "\nThe array 1 is: ";
   for(int i = 0; i < 5; i++) //print array
   {
       cout << arr1[i] << ", ";
   }
   cout << "\nThe array 2 is: ";
   for(int i = 0; i < 5; i++) //print array
   {
       cout << arr2[i] << ", ";
   }

   cout << "\nThe sum of two arrays is : " << sum_two(arr1, arr2, 5) << endl; //print sum of two arrays
   return 0;
}

------------------Code screenshots----------

/cpp/temp/temp/function-1-1.cpp - Sublime Text (UNREGISTERED) 2:53 AM * function-1-1.cpp X main-1-1.cpp function-1-2.cpp main/cpp/temp/temp/main-1-1.cpp - Sublime Text (UNREGISTERED) 2:53 AM * X Х main-1-2.cpp Х function-1-3.cpp x main-1-3.cpp x func/cpp/temp/temp/function-1-2.cpp - Sublime Text (UNREGISTERED) 2:53 AM function-1-1.cpp main-1-1.cpp function-1-2.cpp main-1-2/cpp/temp/temp/main-1-2.cpp - Sublime Text (UNREGISTERED) 2:53 AM * function-1-2.cpp Х main-1-2.cpp function-1-3.cpp x main-1/cpp/temp/temp/function-1-3.cpp - Sublime Text (UNREGISTERED) 2:53 AM function-1-1.cpp main-1-1.cpp function-1-2.cpp main-1-2/cpp/temp/temp/main-1-3.cpp - Sublime Text (UNREGISTERED) 2:57 AM function-1-2.cpp Х main-1-2.cpp Х function-1-3.cpp x main-1/cpp/temp/temp/function-1-4.cpp - Sublime Text (UNREGISTERED) 2:57 AM function-1-1.cpp main-1-1.cpp function-1-2.cpp main-1-2/cpp/temp/temp/main-1-4.cpp - Sublime Text (UNREGISTERED) 2:57 AM function-1-2.cpp Х main-1-2.cpp Х function-1-3.cpp x main-1

------------------Output----------

2:48 AM vs@ubuntu:-/pp/temp/temp vs@ubuntu:-/cpp/temp/temp$ g++ main-1-1.cpp vs@ubuntu:-/cpp/temp/temp$ ./a.out The array 1 i

------------------------------------------

I hope this helps you,

Please rate this answer if it helped you,

Thanks for the opportunity

Add a comment
Know the answer?
Add Answer to:
in C++ and also each function has its own main function so please do the main...
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
  • (C++) Write a function that accepts an int array and the array’s size as arguments. The function...

    (C++)Write a function that accepts an int array and the array’s size as arguments.The function should create a new array that is twice the size of the argument array.The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0.The function should return a pointer to the new array.Demonstrate the function by using it in a main program that reads an integer N (that is not more...

  • 1. All functions should be written AFTER the main procedure. 2. A function prototype should be...

    1. All functions should be written AFTER the main procedure. 2. A function prototype should be written for each function and placed BEFORE the main procedure. 3. Each function should have a comment explaining what it does. 4. Each function parameter should have a comment explaining the parameter. 5. Prompt for the number of elements of the list. 6. Allocate an array whose size equals the number of elements specified by the user. 7. Read into the array the elements...

  • Write the implementation (.cpp file) // i cant figure out what it wants where the is a ? the...

    Write the interface (.h file) of a class Accumulator containing:A data member named sum of type integer.A constructor accepting an integer parameter.A function named getSum that accepts no parameters and returns an integer.A function named add that accepts an integer parameter and returns no value.class Accumulator{int sum;Accumulator(int);int getSum ();void add (int);};Write the implementation (.cpp file) of the Accumulator class of the previous exercise. The full specification of the class is:An data member named sum of type integer.A constructor that accepts...

  • Fix this C++ code so that the function prototypes come before main. Main needs to be...

    Fix this C++ code so that the function prototypes come before main. Main needs to be declared first. Then call to the functions from inside of main. #include<cstdlib> using namespace std; //prompt user for input void getGrades(int gradeArray[],int size) { for(int i=0; i<size; i++) { cout<<"Enter the grade "<<i+1<<": "; cin>>gradeArray[i]; } } // finding average of all numbers in array float computeAverage(int numbers[],int size) { float sum=0; for(int i=0; i<size; i++) { sum = sum + numbers[i]; //compute sum...

  • Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and...

    Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and returns the index of the "last occurrence" of the largest element in the array. Include another function to print the array. Also, write a program to test your function. [HINTS) Create an array of size 15 in the main function and initialize it with the values shown in the below sample output. Pass the array and its size to function lastLargestindex; function lastLargestindex returns...

  • must be done in C You will implement a function that matches one of the prototypes...

    must be done in C You will implement a function that matches one of the prototypes below. int maxArray(int size, int* arr); int maxArray(int size, int arr]); The first parameter is the number of elements in the array. The second parameter is an address to the beginning of an int array. In the body of the function, use a looping structure to identify and return the maximum (largest number) of the array. NOTE: The maximum of the same number, say...

  • 22:217 learn-ap-southeast-1-prod-fleet02-xythos.s3-ap-southeast-1.am purse Code: CSCI1540 (2019-20, Term 1) Page 2 of 6 2. (20%) Write a...

    22:217 learn-ap-southeast-1-prod-fleet02-xythos.s3-ap-southeast-1.am purse Code: CSCI1540 (2019-20, Term 1) Page 2 of 6 2. (20%) Write a program (file name: Q2.cpp) with a function of int distinct (int *p, int n); where the parameter pointer p is the base address of an array of size n. The function returns the number of distinct (unique) elements in the array. E.g., suppose array x has contents {1,6,4,6,1). Then the call distinct (x, 5) should return 3. (The three distinct elements are 1,6, 4.)...

  • In C++ Write a function int * return_matches(int a[], int & size,TEST t) which returns an...

    In C++ Write a function int * return_matches(int a[], int & size,TEST t) which returns an array (allocated off of the heap in the function) containing only the elements of array a that pass the “test” t. The function iterates through all the elements of a, and constructs a new array containing all the elements that pass the test. When the parameter corresponding to “size” is passed in (by reference), it contains the size of the array a. In the...

  • 1. Write a recursive function that computes the sum of all numbers from 1 to n,...

    1. Write a recursive function that computes the sum of all numbers from 1 to n, where n is given as parameter. Here is the method header: public static int sum (int n){...} 2. Write a recursive function that finds and returns the minimum value in an array, where the array and its size are given as parameters. Here is the method header: public static int minValue (int [] data, int size){...} 3. Write a recursive function that reverses the...

  • In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array...

    In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array of integers size: an integer representing the size of array target: a number The function returns true if the target is inside the array and false otherwise 2. Write a function min Valve that takes two parameters: myArray an array of doubles - size: an integer representing the size of array The function returns the smallest value in the array 3 Wrile a funcion...

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