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
(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
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
In java write a method that will take an array and change it into a linkedlist...
In java write a method that will take an array and change it into a linkedlist and then display it in the main method
1) Write a function searchValue that accepts an array of integers, the size of the array,...
1) Write a function searchValue that accepts an array of integers, the size of the array, and an integer. Find the last occurrence of the integer passed in as an input argument in the array. Return the index of the last occurrence of the value. If the value is not found, return a -1 2) Write the line of code to call the previous function assuming you have an array vec with length n, and are looking for the number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT