c++ array program..need help please

this is the Qustion:
Write C++ program that contains two functions: main and a function named "reverse". You should initialize an array of floating point values in the main and print it then send it to the function “reverse” that takes as its arguments the following:
(1) an array of floating point values;
(2) an integer that tells how many floating point values are in the array.
The function must reverse the order of the values in the array. Thus, for example, if the array that's passed to the function looks like this:
0 1 2 3 4
5.8 | 2.6 | 9.0 | 3.4 | 7.1
then when the function returns, the array will have been modified so that it looks like this:
0 1 2 3 4
7.1 | 3.4 | 9.0 | 2.6 | 5.8
The function should not return any value. Finally you should print the reversed array in the main.

and this is my code there is somrthing wrong with the output but i didn't know where it is, really need help.

#include<iostream>
using namespace std;
float reverse(float a[]);
int main()
{
int n;
float x[5];
cout<<"please enter the range of array: "<<endl;
for(int i=0;i<5;i++)
{
cout<<"please enter a float number :"<<endl;
cin>>x[i];
}



float reverse(x[5]);

}

float reverse(float a[5])

{
float y[5]={0.0};
for( int i=0;i<5;i++)
{
y[i]=a[i];
}

for(int i=4;i<=0;i--)
cout<<y[i];

cout<<"the number of the numbers is"<<5;
return 0;


}

Put your code in code blocks. See right side of writing area.

reverse should be called like this:

reverse(x); //not like this float reverse(x[5]);

check your for loop in the reverse function. it should be like this:

for(int i=4;i>=0;i--)

Also, why are you putting everything in y, you can print a also like y.
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
#include<iostream>
using namespace std;
float reverse(float a[]);
int main()
{

float x[5];
cout<<"please enter the range of array: "<<endl;
for(int i=0;i<5;i++)
{
cout<<"please enter a float number :"<<endl;
cin>>x[i];
}
 reverse(x);
}

float reverse(float a[5])
{
	float y[5]={0.0};
int j=5;
for( int i=0;i<5;i++)
{
y[i]=a[j];
j--;
}
for(int i=0;i<5;i++)
cout<<y[i];
cout<<"the number of the numbers is"<<5;
return 0;
}

i did't like this but it didn't work..

I told you two comments but you have incorporated only one.
closed account (z05DSL3A)
Just a small point:
...the function “reverse” that takes as its arguments the following:
(1) an array of floating point values;
(2) an integer that tells how many floating point values are in the array.
...The function should not return any value....


void reverse(float data_set[], int data_count);

Edit: also note that reverse() should not print anything, only reverse the array. You need to read your requirements more accurately.
Last edited on
Topic archived. No new replies allowed.