Question

C++ Code Pass By Reference Practice Write the following functions for basic array operations based on...

C++ Code

Pass By Reference Practice Write the following functions for basic array operations based on the function descriptions given below.

Write the following functions for basic array operations based on the function descriptions given below.

  1. FindMinArray: This function will search an array of integers (passed to the function as a parameter) for its minimum value, then return that value.

  2. SumArray: This function will sum an array of integers (passed to the function through a parameter) then return that sum.

  3. CopyArray: This function will copy an array (passed to the function as the first parameter) to another array (passed to the function as the second parameter)

  4. SwapArray: This function will swap the values of two different arrays (passed to the function as two different parameters)

The output formatting and some variable declaration has already been done for you, fill in the rest.

// Student Code

#include <iostream>
using namespace std;

//function for finding min


//function for summing


//function for copying


//function for swaping

int main(){   


//call the min function and output the returned value
int minArray[10]={55,22,1,5,-5,-20,9,52,61,89};
cout<<"Min: "<<yourFunctionCallGoesHere<<endl;

//call the sum function and output the returned value
int sumArray[10]={55,22,1,5,-5,-20,9,52,61,89};
cout<<"Sum: "<<yourFunctionCallGoesHere<<endl;

//call the copy function and output the first arugment passed
int arrayArgumentOne[5]={1,2,3,4,5};
int arrayArgumentTwo[5]={0};
//yourFunctionCallGoesHere
cout<<endl<<"Copied array: "<<endl;;
for(int i=0;i<5;i++)
cout<<arrayArgumentTwo[i]<<endl;

//call the swap function and output the two arguments passed
int arrayArgumentThree[5]={1,2,3,4,5};
int arrayArgumentFour[5]={6,7,8,9,10};
//yourFunctionCallGoesHere
cout<<endl<<"Swapped arrays:"<<endl;;
for(int i=0;i<5;i++)
cout<<arrayArgumentThree[i]<<" "<<arrayArgumentFour[i]<<endl;
  

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
using namespace std;

//function for finding min
int findMin(int arr[], int size) {
        int min = arr[0];
        for(int i=1; i<size; i++) {
                if(arr[i] < min) {
                        min = arr[i];
                }
        }
        return min;
}

//function for summing
int findSum(int arr[], int size) {
        int sum = 0;
        for(int i=0; i<size; i++) {
                sum += arr[i];
        }
        return sum;
}


//function for copying
void copyArr(int arr1[], int arr2[], int size) {
        for(int i=0; i<size; i++) {
                arr2[i] =arr1[i];
        }
}


//function for swaping
void swapArr(int arr1[], int arr2[], int size) {
        for(int i=0; i<size; i++) {
                int t = arr2[i];
                arr2[i] =arr1[i];
                arr1[i] = t;
        }
}



int main(){   


//call the min function and output the returned value
int minArray[10]={55,22,1,5,-5,-20,9,52,61,89};
cout<<"Min: "<< findMin(minArray, 10) <<endl;

//call the sum function and output the returned value
int sumArray[10]={55,22,1,5,-5,-20,9,52,61,89};
cout<<"Sum: "<< findSum(sumArray, 10) <<endl;

//call the copy function and output the first arugment passed
int arrayArgumentOne[5]={1,2,3,4,5};
int arrayArgumentTwo[5]={0};
copyArr(arrayArgumentOne, arrayArgumentTwo, 5);
cout<<endl<<"Copied array: "<<endl;;
for(int i=0;i<5;i++)
        cout<<arrayArgumentTwo[i]<<endl;

//call the swap function and output the two arguments passed
int arrayArgumentThree[5]={1,2,3,4,5};
int arrayArgumentFour[5]={6,7,8,9,10};
swapArr(arrayArgumentThree, arrayArgumentFour, 5);
cout<<endl<<"Swapped arrays:"<<endl;;
for(int i=0;i<5;i++)
cout<<arrayArgumentThree[i]<<" "<<arrayArgumentFour[i]<<endl;
  

}

please upvote.

main.cpp saved 46 47 I/call the min function and output the returned value 48 int minArray[10]-[55,22,1,5, -5, -20,9,52,61,89

Add a comment
Know the answer?
Add Answer to:
C++ Code Pass By Reference Practice Write the following functions for basic array operations based on...
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
  • In C Write a couple of functions to process arrays. Note that from the description of...

    In C Write a couple of functions to process arrays. Note that from the description of the function you have to identify what would be the return type and what would be part of the parameter. display(): The function takes an int array and it’s size and prints the data in the array. sumArray(): It takes an int array and size, and returns the sum of the elements of the array. findMax(): It takes an int array and size, and...

  • Arrays in Functions 2. Arrays in Functions You can use both the array index variables and...

    Arrays in Functions 2. Arrays in Functions You can use both the array index variables and the entire array itself as arguments to functions. The following program updates the elements of the array grades, one-by-one. Thus, we have used a call-by-reference-ish mechanism to update the values (although no & was used). Note that there is a major difference between the grade in the function call and the one in the function definition. In the statement get_grade (grades[i]); "grades" is the...

  • Write in C code: Define an array f=[23,-12,34,-56,-10,12,19], using call-by-reference (use pointers), to pass the array...

    Write in C code: Define an array f=[23,-12,34,-56,-10,12,19], using call-by-reference (use pointers), to pass the array to a function, named Summation. In main: Define array, pass array, print out the array and the result returned from Summation on screen. In function Summation: 1)take array and size from main 2) sum each element according to the following rules 3) if the value of f(i) is positive, add 2*f(i) to the sum 4)if the value of f(i) is negative, add -3*f(i) to...

  • using the source code at the bottom of this page, use the following instructions to make...

    using the source code at the bottom of this page, use the following instructions to make the appropriate modifications to the source code. Serendipity Booksellers Software Development Project— Part 7: A Problem-Solving Exercise For this chapter’s assignment, you are to add a series of arrays to the program. For the time being, these arrays will be used to hold the data in the inventory database. The functions that allow the user to add, change, and delete books in the store’s...

  • Thank you! /*Lab 8 : Practicing functions and arrays Purpose: */ #include<iostream> #include<fstream> using namespace std;...

    Thank you! /*Lab 8 : Practicing functions and arrays Purpose: */ #include<iostream> #include<fstream> using namespace std; int read_function(int array[], int, int); int main() { int array[300], numba; read_function(array[300]); cout << "Enter a whole number between 2-20: " << endl; cin >> numba; read_function(numba); return 0; } int read_funtion (int arr[300], int num, int Values) { int sum=0; ifstream array_file; array_file.open("Lab8.dat"); for(int i=0; i < 300; i++) {   array_file >> arr[i];   cout << arr[i];   sum += i; } cout << sum;...

  • Hi!, having trouble with this one, In this class we use visual studio, C++ language --------------------------------------------------------------...

    Hi!, having trouble with this one, In this class we use visual studio, C++ language -------------------------------------------------------------- Exercise #10 Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions) Part 1 - Using Pointers int largeArray (const int [], int); int largePointer(const int * , int); void fillArray (int * , int howMany); void printArray (const char *,ostream &, const int *, int howMany); const int low = 50; const int high = 90; void main()...

  • c++, we have to write functions for the code given below and other instructions for it...

    c++, we have to write functions for the code given below and other instructions for it to compile. I am having issues understanding how to confront the problem and how to write functions and read the program so it can eventually be solved so it can be compiled 7/ * INSTRUCTIONS: Write two functions in the space // * indicated below. // * // * #1 => Find index of maximum value: Write a function that will // * find...

  • // PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); //...

    // PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); // finds average of all //grades int findHighest (int [], int); // finds highest of all //grades int findFirstFail( int[]); int main() { int grades[100]; // the array holding 100 grades. int numberOfGrades; // the number of grades read. int pos; // index to the array. float avgOfGrades; // contains the average of the grades. int highestGrade; // contains the highest grade. int inderOfFail; //...

  • Lab 6.6 – Using Value and Reference Parameters Below is a copy of the source code....

    Lab 6.6 – Using Value and Reference Parameters Below is a copy of the source code. 1 // Lab 6 swapNums.cpp -- Using Value and Reference Parameters 2 // This program uses a function to swap the values in two variables . 3 // PUT YOUR NAME HERE. 4 #include <iostream> 5 using namespace std; 6 7 // Function prototype 8 void swapNums(int, int); 9 10 /***** main *****/ 11 int main() 12 { 13 int num1 = 5, 14...

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