More function trouble.

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 enters non-numeric input, 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.

My question is where do I go from here? I figure I have to re-reverse the array to retrieve the original array, but how would I go about reversing all the integers in the original array except for the first and the last ones?If anyone could drop a hint or something that would be great.

P.s. is there a value i could return from Reverse_array() other than temp? could I just put 1 and it would have no effect on the function of the program? Or would it be best to just declare the function as type void since no return value is actually needed? If "void" would be the best options, does this conserve space the most since the compiler has one less step to do?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <cctype>

using namespace std;
int Fill_array(double myArr[], int);
void Show_array(const double myArr[], int);
int Reverse_array(double myArr[], int);

int main()
{
    cout << "How big would you like the array to be? ";
    int size;
    int num_entered = 0;
    cin >> size;
    double firstarray[size];
    num_entered = Fill_array(firstarray, size);
    cout << "You entered " << num_entered << " numbers into the array.\n\n";
    cout << "Showing the array...\n";
    Show_array(firstarray, num_entered);
    cout << "\nReversing the array...\n";
    Reverse_array(firstarray, num_entered);
    Show_array(firstarray, num_entered);

    return 0;
}

int Fill_array (double myArr[], int n)
{
    int num_entered = 0;
    cout << "Please fill the array(Non-numeric input to quit): \n";
    for (int i = 0; i < n; i++)
    {
        cout << "Input # " << i+1 << ": ";
        if (cin >> myArr[i])
            ++num_entered;
        else
            return num_entered;

    }
    return n
    ;
}

void Show_array(const double myArr[], int n)
{
    for(int i = 0; i < n; i++)
    {
        cout << "Value #" << i+1 << ": " << myArr[i] << endl;
    }
}

int Reverse_array(double myArr[], int n)
{
    double temp;
    for (int a = 0; a < --n; a++)
    {
        temp = myArr[a];
        myArr[a] = myArr[n];
        myArr[n] = temp;
    }
    return temp;
}




Also I had the idea to call Reverse_array(firstarray + 1, num_entered +1);

That solves the first/last variables being left as is but it doesn't solve the issue with the middle variables. They come out mumbo jumbo!

Last edited on
If it makes no sense for a function to return a value, then it shouldn't return a value.
Also, you didn't catch that value.

Reverse_array(firstarray + 1, num_entered +1); ¿why the array increased its size?
Actually the second parameter it is not the size, but how many elements do you want to work with. Considerer it as passing a range, like in void reverse ( Iterator first, Iterator last); // [)

By the way
1
2
    cin >> size;
    double firstarray[size];
is against the standard. If you don't know the size, use std::vector
"Reverse array takes a double array and array size as arguments."


num_entered + 1 means that your array is one element bigger than itself. That's like saying that I'm shorter than myself doesn't make sense does it?

what you should need is num_entered - 2, you take one element from array size because you are starting the count one element past the beggining and another one because you don't want to include the last one.

EDIT: The mumbo jumbo is there because you are reading past the memory that was allocated for your array.
Last edited on
@ne555 i don't know what a vector is & thank you for the help!
@eidge thanks for the help.
Topic archived. No new replies allowed.