passing struct and array into a function

I am trying to create a function for a formula that uses a structure with an array to input the data but am having a hard time figuring it out. Does anybody have any suggestions?

Read through the tutorials accessible from the cplusplus.com homepage.

Also, don't use arrays, because they are evil. Use a vector or deque or a list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct point_t
  {
  int x, y;
  point_t( int x = 0, int y = 0 ): x( x ), y( y ) { }
  };

vector <point_t> read_points()
  {
  vector <point_t> result;
  string s;

  cout << "Enter as many x, y pairs as you like, separated by spaces.\n"
       << "Press ENTER twice to finish.\n";

  while (getline( cin, s ) and !s.empty())
    {
    stringstream ss( s );
    int x, y;
    while (ss >> x >> y)
      result.push_back( point_t( x, y ) );
    }

  return result;
  }


To pass an object to a function, use a reference parameter:
1
2
3
4
5
void print_points( vector <point_t>& points )
  {
  for (unsigned i = 0; i < points.size(); i++)
    cout << '(' << points[ i ].x << ',' << points[ i ].y << ") ";
  }


Hope this helps.
Here is my program so far... I am trying to pass the array with a structure through the intTemp function. I read that this can be done, but it isn't working. Where am I going wrong?

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
//Interface Temp Calculator for Composite Planar Wall with a Variable # of Materials 
//Use of Structure, Loop and Array

#include <iostream>
#include <stdlib.h>
#include <iomanip>
using namespace std;


double flux(double H, double tsurf, double tfluid) 
{
		double Q;
		Q = H * (tfluid - tsurf);
		return Q;
}

const int MAXNUMWALLS = 50;

struct tWallData
{ //20
      double L;
      double K;
};

double intTemp(double Q, double tsurf, int numWalls, wallCalc[j].L, wallCalc[j].K)
{
	double Tout;
	for (int j = 0; j < numWalls; j++)
	{
	Tout = tsurf - ((Q * (wallCalc[j].L)) / (wallCalc[j].K)); 
	tsurf = Tout;
	}
	return Tout;
}

int main()
{
		//input for flux calc
		double T2;
		double q;
		double h; 
		cout << "Enter Outside Convection Coefficient (w/m^2*K): ";
		cin >> h;
		double T1;
		cout << "Enter Outside Material Surface Temp. (Deg C): ";
		cin >> T1;
		double Tfluid;
		cout << "Enter Outside Fluid Temp. (Deg C): ";
		cin >> Tfluid;
		q = flux(h, T1, Tfluid);

	// array of structures 
      tWallData wallCalc[MAXNUMWALLS]; 
      int numWalls;

     do
     {
		 cout << "Enter # of materials in wall (1-50): ";
          cin >> numWalls;
          cout << endl;
     } while ( numWalls < 1 || numWalls > 50); 

     // store data
      for (int k = 0; k < numWalls; k++)
      {
            cout << "Enter thickness of material (m) " << k+1 << ": ";
            cin >> wallCalc[k].L;
            cout << "Enter conductivity of material (w/m*k) " << k+1 << ": ";
            cin >> wallCalc[k].K;

		if (wallCalc[k].K < .0001) 
		{
			cout << setw(12) << " " << "K value must be larger than .0001 w/m*k" << endl;
			return 0;
		}
		
		if (wallCalc[k].K > 300)
		{
		cout << setw(12) << " " << "K value must be smaller than 300 w/m*k" << endl;
		return 0;
		} //80
            cout << endl;
      }

     // Calculate with stored data from structure 
		cout << endl << endl;
		cout << "Interface Temperatures" << endl;
		cout << setfill('-') << setw(60) << "-" << setfill(' ') << endl;	
		for (int j = 0; j < numWalls; j++)
     {   
		T2 = intTemp(q, T1, numWalls, wallCalc[j].L, wallCalc[j].K); 
		cout << setw(12) << " " << "The exit surface temp of material # " << j + 1 << " is: " << T2 << endl;
	}
		return T1;
		cout << endl << endl << endl;
		return 0;
}





Line 25:
 
double intTemp(double Q, double tsurf, int numWalls, wallCalc[j].L, wallCalc[j].K)


Should be double intTemp(double Q, double tsurf, int numWalls, tWallData wallCalc[])

And called on line 91 like: T2 = intTemp(q, T1, numWalls, wallCalc);
Last edited on
Topic archived. No new replies allowed.