Question

Problem 2 (USE C++) Assume you are given two functions: a function that returns –in a...

Problem 2 (USE C++)

Assume you are given two functions: a function that returns –in a variable passed by reference- the GCD of all integer numbers in an array void GCD(int A[], int size, int &g) and another function that allows you to divide all numbers of an array by a given integer void Div(int A[], int size, int gcd) (assume the functions are in a library, therefore you don’t need to implement them, but just use them). 1. Write a function that accepts an array of integers and its size and reduces all the numbers by their GCD. You need to use the above functions in your function. 2. Write a main function where you make a call to your function with appropriate parameters of your choice.

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

Following is the code in C++:

#include <iostream>
#include <algorithm>
using namespace std;

void GCD(int A[],int size, int *g) //function to calculate gcd of elements of array and store it in pointer g
{
*g=A[0]; //storing first element in g
for(int i=1;i<size;i++)
{
*g=__gcd(*g,A[i]); //taking each element and calculating gcd of that element and gcd of the previous elements
}
}
void div(int A[],int size, int gcd) //function to divide the array elements by gcd
{
for(int i=0;i<size;i++)
{
A[i]=A[i]/gcd;
}
}
void inputoutput() //function to take input and display output
{
int n;
cout << "Enter the size of the array : ";
cin >> n;
int A[n];
cout << "Enter the elements of the array separated by a space : ";
for(int i=0;i<n;i++)
{
cin >> A[i];
}
int g;
GCD(A,n,&g);
div(A,n,g);
cout << "The elements of the array after reducing are : ";
for(int i=0;i<n;i++)
{
cout << A[i] << " ";
}
}
int main() //main function
{
inputoutput();
return 0;
}

Screenshot of output:

Add a comment
Know the answer?
Add Answer to:
Problem 2 (USE C++) Assume you are given two functions: a function that returns –in a...
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
  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end...

    PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end please include a main function that tests the functions that will go into a separate driver file. Prototypes int R_get_int (void); int R_pow void R Jarvis int start); void R_fill_array(int arrayll, int len); void R_prt_array (int arrayl, int len) void R-copy-back (int from[], int to [], int len); int R_count_num (int num, int arrayll, int len) (int base, int ex) 3. Build and run...

  • c++ show code thanks, only use library <iostream> Question 1 Write the following two functions The...

    c++ show code thanks, only use library <iostream> Question 1 Write the following two functions The first function accepts as input a two-dimensional array of integers. It returns two results: the sum of the elements of the array and the average The second function accepts as input a two-dimensional array of integers and integer value V. It returns true if the value V is found in the array, false otherwise. Write a main program that declares and initializes the following...

  • Language C Code Write a program that takes two integer arrays (A and B) and sums...

    Language C Code Write a program that takes two integer arrays (A and B) and sums them together (element wise). A third array to accept the result should be passed in as the output argument. Assume the arrays are all the same size. The argument N is the size of the arrays. Your code should provide a function with the following signature: void array Addition (int A[], int B[], int N, int output[]) { } Your code must also provide...

  • Using the program segment below, write two short functions to complete the program. Use the test...

    Using the program segment below, write two short functions to complete the program. Use the test cases to ensure the program works properly. Prototypes: void int2bin(int, int[8]); The first function will convert an integer that is between 0 - 255 to its binary representation. You are to store the binary number in the array passed to the function. void printBinary(int[8]); The second function will print the binary number stored in the array passed to the function. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Test 1: 13...

  • c++ I have to write the following functions that I will later use for testt but...

    c++ I have to write the following functions that I will later use for testt but you just have to write the following functions again I have written the interface of it and please make sure it works. will have an array of randomly generated numbers - An array that is already sorted in ascending order - An array that is sorted in reverse (descending) order - An array that is sorted except for the last 10 numbers which are...

  • Map, Filter, and Reduce are three functions commonly used in functional programming. For this assignment you...

    Map, Filter, and Reduce are three functions commonly used in functional programming. For this assignment you will be implementing all three, along with three minor functions that can be passed to them. Map The map function takes as arguments a function pointer and a integer vector pointer. It applies the function to every element in the vector, storing the results in-order in a new vector. It then returns a pointer to a new vector. You should pass your square function...

  • Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over...

    Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over an array of integer numbers and size n. The function should return the index of the search key if the search key exists and return - 1 if the search key doesn't exist. [10 Points] Q2] Write a C function to implement the selection sort algorithm, to sort an array of float values and size n. The function should sort the array in ascending...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • Note: None of these functions should use cout. It is OK to use cout while debugging...

    Note: None of these functions should use cout. It is OK to use cout while debugging to check how your function is working, but your final submission should not contain cout in any function but main. Head ==== Name the source file for this section head.cpp. Write a function named "head" which takes an array of integers as an argument, and returns the first element in the array. Write a function named main which outputs the result of testing your...

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