array displayed through recursive function and loop

Hi guys, I m going through chapther 15 of jumping into C++, and the 2nd exercise says :
"Write a recursive function that takes an array and display the elements in reverse order without starting the index of the array at the end (in other word the opposite of what you'd do with a loop)"

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
#include <iostream>

using namespace std;

string NameYourFriends(string array[],int size)
{
    for(int i=0;i<size;i++)
    {
        cout<<i<<")friend's name: "<<endl;
        cin>>array[i];
    }

}

void DisplayArrayLoop (string array,int size)
{
    for (int i =0;i<size;i++)
    {
        cout<< array[i];
    }
}

void DisplayArrayRecrusion (string array,int size)
{
    cout<<array[size-1];
    if (size >0)
        DisplayArrayRecrusion (array, size-1);
}

int main()
{
    int size;
    string friends [size];
    cout<<"Enter the how many friends you have"<<endl;
    cin>>size;
    friends [size] = NameYourFriends (friends,size);
    DisplayArrayLoop (friends,size);       //this last 2 lines give me an error 
    DisplayArrayRecrusion (friends,size);  //while building
}

The problem is in the last 2 lines of code....
Well, if you're passing an array then you should probably write the function to receive an array. Compare the signature of NameYourFriends to the other functions.
Last edited on
Your function void DisplayArrayLoop (string array,int size). The first parameter is a string. One single string.

What are you trying to pass to the function as the first parameter? The object named friends. Is friends one single string?
absolutely right ahaha :/ now it runs but when I enter the friends names it quits at the setting of the third one...
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
#include <iostream>
#include <string>

using namespace std;

string NameYourFriends(string array[],int size)
{
    for(int i=0;i<size;i++)
    {
        cout<<i+1<<")friend's name: "<<endl;
        cin>>array[i];
    }
    return array [size] ;

}

void DisplayArrayLoop (string array[],int size)
{
    cout<<"{";
    for (int i =0;i<size;i++)
    {
        cout<< array[i]<<", ";
    }
    cout<<"}"<<endl;
}

void DisplayArrayRecrusion (string array[],int size)
{
    cout<<array[size-1]<<", ";
    if (size >0)
        DisplayArrayRecrusion (array, size-1);
}

int main()
{
    int size;
    string friends [size];
    cout<<"Enter the how many friends you have"<<endl;
    cin>>size;
    friends [size]= NameYourFriends (friends,size); // This function doesn t finish its task
    DisplayArrayLoop (friends,size);       
    DisplayArrayRecrusion (friends,size);  
}
Look at this snippet:

1
2
    int size;
    string friends [size];

Do you realize that you seem to be trying to create an array of an undefined size?

Also do you realize that in C++ array sizes must be compile time constants. If you want a runtime "array" I suggest you consider std::vector.

friends [size]= NameYourFriends (friends,size);
Do you realize that you passed a pointer to the array into the function and that any change made to the array is reflected in the calling function? There is really no need to try to return an "array", just use the parameter.

Lastly for now: return array [size] ;

Do you realize that this line is trying to return a single element of your array? And remember if the size of the array is the same as "size" that element doesn't exist. Remember arrays start at zero and end at size minus one.



@gdz98
1. Use dummy data and solve a simple version of the problem. You're just distracting yourself with cin input, displays, etc.
2. Simplest is to index i to the function and keep incrementing it until the size is reached

I think you missed one of the points here -- you can use recursion itself to produce the reversal if you recur before output. You end up with a bunch of function calls on the function stack that end up resolving before getting to the output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

// Prints in reverse
void RecursiveDisplay(const int* const arr, const int arr_size, int i)
{
    if (i == arr_size)
        return;

    RecursiveDisplay(arr, arr_size, i+1);
    cout << arr[i] << endl;
}

int main() 
{
    int arr[] = { 1, 2, 3, 4, 5 };
    RecursiveDisplay(arr, sizeof(arr)/sizeof(arr[0]), 0);

    return 0;
}

5
4
3
2
1


In effect we called f(0), then piled f(1) on top of that, then f(2) on top of that, etc..., finally returning when f(5) because 5 is the array size. So it looks like this in the end:
f(4)
f(3)
f(2)
f(1)
f(0)


That's why the output is the way it is
Topic archived. No new replies allowed.