Array Help

Can Someone help me with this problem , been breaking my head over it for a while now :)


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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//Problem. The first line of the file "trees1.txt" holds the number of the trees in the park. 
//The following lines contain the names and heights of the trees in the park. 
//Read the data to the collection of objects Tree. Find out the height of the highest tree in the park.
//Count how many trees are higher than 30 meters. Find out the average of the heights of the trees. 
//Display the initial data and results on the screen. Organize the solving of the problem in the main() function.






#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>

using namespace std ;

class trees
{
public:
	string name;
	double height;
};

void readdata(string fv, trees A[], int & nc);//Reading data from the file fv to the array
void writedata(string fv, trees A[],int & nc);//Writing data from the file fv to the array
void DisplayData(trees A[], int nc);// Displaying values from S(n) on the screen
int Max(trees A[], int nc); //Finding the maximal value in the array
int Aver(trees A[], int nc);//Finding the average of the height
int Many(trees A[], int nc); //Finding the number of objects that satisfy the condition
int main ()
{
	 trees A[100];
	 int nc; 
	 ofstream fr("Result.txt");
	 
	 readdata("trees1.txt", A, nc); 
	 writedata("Result.txt", A, nc); 
	 fr.open("Result.txt", ios::app); 
	 fr.setf(ios::fixed); fr.setf(ios::left); 

}





	 void readdata(string fv, trees A[], int & nc)
{
ifstream fd(fv.c_str()); 
fd >> nc; 
fd.ignore();
for (int i = 0; i < nc; i++)
{ getline(fd, A[i].name, ','); 
fd >> ws; fd >> A[i].height; 
fd.ignore();
} 
fd.close(); 
	 
}

	 void writedata(string fv, trees A[], int & nc) 

	 { 
	 ofstream fr(fv.c_str(), ios::app);
	 fr.setf(ios::fixed); fr.setf(ios::left); 
	 fr << "Number of Trees: " << nc << endl;
	 fr << "List of types of Trees:\n"; 
	 fr << "--------------------------------\n"; 
	 fr << "| Name            | Height     |\n";
	 fr << "--------------------------------\n";
	 for (int i = 0; i < nc; i++) 
	 { fr << "| " << setw(15) << A[i].name << " | " << setw(10) << A[i].height<< " | " << setprecision(2) <<endl; }
	 fr << "--------------------------------\n";
	 fr << "Average Height of Trees : " << setprecision(2)
<< Aver(A, nc) << " m" << endl;
	 fr<< "Height Of the Highest Tree in the Park : "<<setprecision(2)<<Max(A,nc)<<endl;
fr.close(); }

	 void DisplayData(trees A[], int nc)
	 {

		 cout<<"Number of Trees: " << nc << endl;
		 cout<<"List of types of Trees:\n";
		 cout<<"--------------------------------\n";
		 cout<<"| Name            | Height     |\n";
		 cout<<"--------------------------------\n";
		 for (int i = 0; i < nc; i++) 
	 { cout<< "| " << setw(15) << A[i].name << " | " << setw(10) << A[i].height<< " | " << setprecision(2) <<endl; }
	   cout<< "--------------------------------\n";
	 }


	 int Max(trees A[], int nc)
	 {
		  double A[100] ;
    double temp = 0;

    for(int i=0;i<nc;i++)
    {
        if(A[i].height>temp)
		temp=A[i].height;
    }


	 }
	 


	   int Aver(trees A[], int nc)
	   
 {
double sum = 0;
int count = 0;
for (int i=0; i<nc; i++)
	{
sum = sum + A[i].height;
count++;
}
	if(count>0)
return sum / count;
	else
		return 0.;
}
	   
	   

	


Result.txt
Number of Trees: 5
List of types of Trees:
--------------------------------
| Name            | Height     |
--------------------------------
| Maple           | 25.000000  | 
| Mango           | 22.00      | 
| Apple           | 35.00      | 
| Coconut         | 40.00      | 
| Palm            | 42.00      | 
--------------------------------
Average Height of Trees : 32 m


trees1.txt ------>INPUT
5
Maple, 25
Mango, 22
Apple, 35
Coconut, 40
Palm, 42
 
Can you describe the problems that you're encountering?

I tried to compile the code and received this error:
1>e:\programming\practice\freestyle\cplusplus help\barry allen array help\barryallen\barryallen\barryallen.cpp(97): error C2082: redefinition of formal parameter 'A'

As the error mentions, like 97 attempts to redefine an argument that you passed to the Max() function. A[] has already been passed to the function, so you may not need to create a new array. If you do need to create a new one, name it something else. Preferably with a clear, easy-to-understand name.

1
2
3
4
5
 int Max(trees A[], int nc)
 {
	  double A[100] ;
          ...
 }
Did you place "trees1.txt" in the project files directory ?
The error I mentioned comes up for me whether the trees1.txt file is in the directory or not, as as it causes a compile-time error. Can you describe the issue you've encountered in more detail?
yeah , cause the code is incomplete .

The program is correct till displaying the average value ,
I just dont know how to find

1)The Maximum number in an array .

2)How many tree's are higher than 30m
Understood.

It looks like you have the correct logic to traverse the array and store the highest value in a variable. The problem is that line 97 is trying to create a brand new array called A[], but you already have an array named A[]. Since A[] was passed as an argument, you can use it directly. Delete line 97 completely double A[100] ; and go from there.

The function still needs to return a value, so you'll have to add that before it will compile. Hopefully that gets you back on track.
Topic archived. No new replies allowed.