Question

In: Computer Science

I need to write a function the will take in an array (of type double), and...

I need to write a function the will take in an array (of type double), and will return the array with all of the elements doubled while using pass-by-reference and addressing and/or pointers. This is what i have so far, but im messing up in line 31 where i call the function i believe so im not returning the correct array? please help edit.

#include <iostream>
#include <iomanip>

using namespace std;

double *doubleArray ( double arr[], int count, int SIZE);

int main()
{
    const int SIZE = 5;
    double numbers [SIZE];
    int count;
    double *doublePtr;
    doublePtr = numbers;

    cout<< "Enter "<< SIZE << " numbers: "<< endl;
    for(count = 0; count < SIZE; count ++)
    {
        cin>> *(numbers + count);
    }

    cout<< "Here is your original array : \n";
    for(count = 0; count < SIZE; count ++)
    {
        cout<<  doublePtr[count]<< " " ;
    }
    cout<< endl;

    double *doubleArray(double numbers[], int count, int SIZE);

    cout<< "Here is your new array : \n";
    for(count = 0; count < SIZE; count ++)
    {
        cout<<  doublePtr[count]<< " " ;
    }

    return 0;
}

double *doubleArray ( double arr[], int count, int SIZE)
{
    for(count = 0; count < SIZE; count ++)
    {
        arr[count] = arr[count] *2;
         cout<<  arr[count]<< " " ;
    }

return arr;
}

Solutions

Expert Solution

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

NOTE :When parameters are passing to a function by reference then no need to return anything directly changes will be made in the memory location.

Here a new c++ program with name "main.cpp" is created, which contains following code.

main.cpp :

#include <iostream> //header files
#include <iomanip>
using namespace std;
//function prototype
void *doubleArray ( double arr[], int count, int SIZE);
//main method
int main()
{
const int SIZE = 5;//declaring variables
double numbers [SIZE];
int count;
double *doublePtr;
doublePtr = numbers;
//asking to enter size
cout<< "Enter "<< SIZE << " numbers: "<< endl;
for(count = 0; count < SIZE; count ++)
{
cin>> *(numbers + count);//reading number
}
//using for loop displaying array elements
cout<< "Here is your original array : \n";
for(count = 0; count < SIZE; count ++)
{
cout<< doublePtr[count]<< " " ;//display array elements
}
cout<< endl;
//function call
doubleArray(numbers,count,SIZE);
//print each array element using for loop
cout<< "Here is your new array : \n";
for(count = 0; count < SIZE; count ++)
{
cout<< doublePtr[count]<< " " ;
}

return 0;
}
//function defination
void *doubleArray ( double arr[], int count, int SIZE)
{ //using array double the value
for(count = 0; count < SIZE; count ++)
{
arr[count] = arr[count] *2;//print each array element
}
}

======================================================

Output : Compile and Run main.cpp to get the screen as shown below

Screen 1 :main.cpp

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.


Related Solutions

Write a function that will take in an array (of type double), and will return the...
Write a function that will take in an array (of type double), and will return the array with all of the elements doubled. You must use pass-by-reference and addressing and/or pointers to accomplish this task. C++
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
Write a class DataSet that stores a number of values of type double in an array...
Write a class DataSet that stores a number of values of type double in an array list. Provide methods to insert a value to the array list and to compute the sum, average, and maximum value. Please put a note on what you did is for what. So I can understand this question better. Code provided: import java.util.*; public class DataSet {    private ArrayList<Double> data; /** Constructs an empty data set. @param maximumNumberOfValues the maximum this data set can...
(C++ only please) Write a function called maximum that takes an array of double values as...
(C++ only please) Write a function called maximum that takes an array of double values as a parameter and returns the largest value in the array. The length of the array will also be passed as a parameter. (Note that the contents of the array should NOT be modified.) Write a function called printReverse that takes an array of characters and the length of the array as parameters. It should print the elements of the array in reverse order. The...
Write, specify and prove the function reverse that reverses an array in place. Take care of...
Write, specify and prove the function reverse that reverses an array in place. Take care of the unmodified part of the array at some iteration of the loop. Assume that the swap function is already proved. Note: Prototype is as below. [7 M] [CO2] void swap(int* a, int* b); void reverse(int* array, size_t len){ }
In C++, Write the definition of the function MaximumCount() whose header is int MaximumCount(Array<double>& data) It...
In C++, Write the definition of the function MaximumCount() whose header is int MaximumCount(Array<double>& data) It returns the amount of times the maximum value of data appears in data. If data is empty, it returns 0. For instance, if data = [7, 1, 4, 9, 6, 7, 7, 3, 2, 6, 9, 5, 9], it will return 3 since 9 appears three times
JavaScript Write out a function that takes in array of numbers. You do not need to...
JavaScript Write out a function that takes in array of numbers. You do not need to check if each item of the array is a number, assume this has already been done. Create a new array using the map function that that the original item and adds 4 to it. Then iterate through the new array and log each item to the console. If you do not use map it will be counted wrong. Call your function with the following...
I need to write four pages double spaced paper on a wealth inequality in USA. I...
I need to write four pages double spaced paper on a wealth inequality in USA. I can use three sources for my paper. Can someone help me please.
I need to write a function that counts the number of total wins and losses. I...
I need to write a function that counts the number of total wins and losses. I have a text file called analysis_data.txt with 1000 lines written Won Loss Won Loss Won Won ... The function need to do the following: Opens the analysis_data.txt file and reads through all the data, counting number of 'Won' and 'Loss' words stored in the file. Returns two integers: count of wins and count of losses from the text file. **Can't use the break statement
c++ I need a code that will fill an array size of 1000, an array of...
c++ I need a code that will fill an array size of 1000, an array of size 2000, and an array size of 10000, with random int values. Basically like this: array1[1000] = filled all with random numbers array2[2000] = filled all with random numbers array3[10000] = filled all with random numbers C++ no need for print
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT