Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Would you care to help me c++?

So I'm supposed to write a program that uses the following functions:

Fill_array() takes as arguments the name of an array of double values and an array size. It

prompts the user to enter double values to be entered in the array. It ceases taking input when

the array is full or when the user enter a negative number, and it returns the actual number of

entries.

Show_array() takes as arguments the name of an array of double values and an array size and

displays the contents of the array.

Reverse_array() takes as arguments the name of an array of double values and an array size and

reverses the order of the values stored in the array.

The program should use these functions to fill an array, show the array, reverse the array, show

the array, reverse all but the first and last elements of the array, and then show the array.

Here is what i have so far:

#include <iostream>

using namespace std;

int fill_array(double *pt,int size);

void show_array(double *pt, int size);

int reverse_array(double *pt, int size);

int main()

{

double amat[10];

int n;

n = fill_array(amat,10);

show_array(amat,10);

n = reverse_array(amat,10);

show_array(amat,10);

char x;

cout << "\nPress any key and Enter to exit";

cin >> x;

return 0;

}

int fill_array(double *pt,int size)

{

int i;

cout << "Enter " << size << " values\n";

for(i=0;i<size;i++)

{

cin >> pt[i];

}

return i;

}

void show_array(double *pt,int size)

{

int i;

cout << "\n\nArray values:\n";

for(i=0;i<size;i++)

cout << pt[i]<< "\t";

return;

}

int reverse_array(double *pt, int size)

{

int i;

int swap;

for(i=0;i<--size;i++) //increment i and decrement size until they meet each other

{

swap=pt[i];

pt[i]=pt[size];

pt[size]=swap;

}

return i;

}

The main problems that I have is that I don't know how to make the array stop taking values when a negative number is entered and I don't know how to do the last thing where it wants me to keep the first and last number but reverse the others.

Please help.

Update:

thank you!

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    My C++ is a little rusty but to answer the second question first.

    To reverse all but the first and last elements:

    n = reverse_array(*amat[1],8);

    This will pass in the address of the second entry in the array and tell the function that there are two less than the total number of entries.

    As to the first question, one way would be to use a DO WHILE

    double temp;

    i = 0;

    Do

    {

    cin >> temp;

    if (temp >= 0) {

    pt[i] = temp;

    i = i + 1;

    }

    } while(i<size && temp>=0);

    Append additional to let me know if this is OK

    Note. I followed your lead by using 10 but I am guessing you realize that the user can enter in fewer than 10 (and what about more than 10?). Anyway good luck.

Still have questions? Get your answers by asking now.