arrays in function different values in main

I am creating a very large program and am rather new at programming in general. I am using a function to calculate room modes and store them to an array to be used later. I want to calculate it outside of the main because it allows the main program to be much cleaner and I can just see when this portion of the program is being run. However, when I try to access the array calculated I get some number that is obviously not was was calculated in the array. Is there a way to access the information in the main, or is it better to just calculate it entirely in the main?

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
#include <iostream>
#include <math.h>
#include<string>
using namespace std;

double l,w,h,temp,c,volume,surfaceArea; int a,n,m,i,j,k; //declaring global variables
double axialModes(double l,double w, double h,double c)
{
	double mode[n][n][n];
	for (int i=1; i<n; i++)
	{
		mode [i][0][0]=c/2*i/l;
		cout<<"length mode= "<<i<<" 0 0 "<<mode[i][0][0]<<" Hz"<<endl;
		if (mode[i][0][0]>m)
		break;
	}
}

int main ()
{
	cout<<"What is the width of the room?"<<endl;cin>>w;cout<<endl;
	cout<<"What is the length of the room?"<<endl;cin>>l;cout<<endl;
	cout<<"What is the height of the room?"<<endl;cin>>h;cout<<endl;
	cout<<"What is the temperature of the room in degrees Celsius?"<<endl;cin>>temp;cout<<endl;

	cout<<"width= "<<w<<"  length= "<<l<<"   height="<<h<<endl;
	cout<<"temperature= "<<temp;
	

	n=5; //determines the maximum number of modes calculated in each of the loops; stops infinite loops
	m=300; //determines the frequency at which the modes stop being calculated

//declare arrays used in modes:
	double mode[n][n][n];
	
	axialModes(l,w,h,c);
	
	cout<<"axial length mode 1= "<<mode[1][0][0]<<endl; //this give me a different result from where it was calculated in the function above
	return 0;
}

Last edited on
From what I can see, the array identifier variable of mode, (n), is never initialized to any specific value.

More importantly, the mode array is defined twice in your program, and each definition does not know the other exists.

Try using a reference to mode in your function axialModes's arguments list, to transfer the values to the variable in main() so you can access mode array values within main().

1
2
3
4
double axialModes(double l,double w, double h,double c, double &mode)
{
....
}


then remove the definition of mode from inside the body of the function axialModes:
1
2
3
double axialModes(double l,double w, double h,double c)
{
	double mode[n][n][n]; //  < ---- REMOVE ME 


and your calls to the axialModes function within main() should pass mode in the functions argument list:
 
axialModes(l,w,h,c, mode);


But what do I know, I just started learning C++ a few weeks ago
Last edited on
Okay, I defined it twice because otherwise it would not compile. I did as you suggested and now I have a "[Error] invalid types 'double[int]' for array subscript" within the function at the top for my "i" values and an "[Error] invalid initialization of non-const reference of type 'double&' from an rvalue of type 'double (*)[(((sizetype)(((ssizetype)n) + -1)) + 1)][(((sizetype)(((ssizetype)n) + -1)) + 1)]' " in my main when i try to call the function. It is not saying that mode[n][n][n] is not declared in the space though, so this may be a start. any other ideas?

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
#include <iostream>
#include <math.h>
#include<string>
using namespace std;

double l,w,h,temp,c,volume,surfaceArea; int a,n,m,i,j,k; //declaring global variables
double axialModes(double l,double w, double h,double c, double &mode)
{
	for (int i=1; i<n; i++)
	{
		mode [i][0][0]=c/2*i/l;
		cout<<"length mode= "<<i<<" 0 0 "<<mode[i][0][0]<<" Hz"<<endl;
		if (mode[i][0][0]>m)
		break;
	}
}

int main ()
{
	cout<<"What is the width of the room?"<<endl;cin>>w;cout<<endl;
	cout<<"What is the length of the room?"<<endl;cin>>l;cout<<endl;
	cout<<"What is the height of the room?"<<endl;cin>>h;cout<<endl;
	cout<<"What is the temperature of the room in degrees Celsius?"<<endl;cin>>temp;cout<<endl;

	cout<<"width= "<<w<<"  length= "<<l<<"   height="<<h<<endl;
	cout<<"temperature= "<<temp;

	c=20.047*pow(273.15+temp,0.5);  //c calculations
	volume=w*l*h;
	surfaceArea=2*((l*w)+(w*h)+(h*l));
	cout<<"The speed of sound is "<<c<<" meters/second"<<endl;
	cout<<"The volume of the room is "<<volume<<" cubic meters."<<endl;
	cout<<"The surface area of the room is "<<surfaceArea<<" square meters. "<<endl;

	n=5; //determines the maximum number of modes calculated in each of the loops; stops infinite loops
	m=300; //determines the frequency at which the modes stop being calculated

//declare arrays used in modes:
	double mode[n][n][n];
	
	axialModes(l,w,h,c, mode);
	
	cout<<"axial length mode 1= "<<mode[1][0][0]<<endl; //this give me a different result from where it was calculated in the function above
	return 0;
}
Last edited on
You never say what 'c' is used for or what its value is as well.
oh, this is just an abbreviated program. c is the speed of sound. i'll add that in to the previous response above.
Last edited on
Try this - I still have no clue what this program does, so I don't know if I got it working correct, but it runs and outputs some important looking results.

IT wouldn't compile because of your array mode. I'm not sure if you declared it incorrectly, or used it wrong, but a vector took care of it.

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
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;

double l,w,h,temp,c,volume,surfaceArea; int a,n,m,i,j,k; //declaring global variables

void axialModes(double l,double w, double h,double &c,int &m, vector<double> &mode)
{

	for (int i=0; i<n; i++)
	{
		mode.push_back(c/2*i/l);
		cout<<"length mode= "<<i<<" 0 0 "<< mode[i] <<" Hz"<<endl;
		if (mode[i]>m)
		break;
	}
}

int main ()
{

	n=5; //determines the maximum number of modes calculated in each of the loops; stops infinite loops
	m=300; //determines the frequency at which the modes stop being calculated
	c=3;           ////// DUMMY VALUE , FIX /////////////

	cout<<"What is the width of the room?"<<endl;cin>>w;cout<<endl;
	cout<<"What is the length of the room?"<<endl;cin>>l;cout<<endl;
	cout<<"What is the height of the room?"<<endl;cin>>h;cout<<endl;
	cout<<"What is the temperature of the room in degrees Celsius?"<<endl;cin>>temp;cout<<endl;

	cout<<"width= "<<w<<"  length= "<<l<<"   height="<<h<<endl;
	cout<<"temperature= "<<temp;
	



//declare arrays used in modes:
	vector<double> mode;
	axialModes(l,w,h,c,m,mode);
	cout<<"axial length mode 1= "<<mode[0]<<endl; //this give me a different result from where it was calculated in the function above
	return 0;
}
Last edited on
Thank you for all of your help! the values appear to match if i change mode[0] in line 42 to mode[1], which makes sense as the formula does nothing if i is equal to 0. So the push_back function is line 14 transfers the values to the main function?

I have not really worked with vectors, so I will have to look into this for sure! I still need to try it with the 3 dimensional array, but this is looking good. I have to do this calculation about 16 times (possibly more) to calculate room modes (prominent frequencies in certain parts of a room - sound terminology fun!) Using a 3 dimensional array allowed me to make these calculations and have all the values together. The arrays are labeled (0 0 1, 1 0 0, 0 1 0, 1 2 3, 2 0 2) and it gets quite complicated These numbers keep increasing until a calculated frequency that i need to calculate in this program after calculating something else to get that...

Are vectors only good with 1 dimensional arrays do you know and how did this declaration work in line 40? Did it just assume that "mode" was some 1 dimensional array without requiring the size of it?
In that case, I would ditch the vectors and return to multidimensional arrays. Start here: http://www.cplusplus.com/doc/tutorial/arrays/

I guess vectors are good for dynamic memory management and for manipulating small arrays (push_back, pop_back, etc..) and that's why I used it - but for complex array's that need to be specific I would stick with the multi-d arrays. Sorry for confusion
Last edited on
I could get it to here for you, but I have to go to class -

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
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;


double l,w,h,temp,volume,surfaceArea;
int a,k; //declaring global variables

void axialModes(double l,double w, double h,double c,int m,int n, double *pMode[5][5][5])
{
	for (int i=1; i<n; i++)
	{
		*pMode[i][0][0] = (c/2*i/l);
		cout<<"length mode= "<<i<<" 0 0 "<< pMode[i][0][0] <<" Hz"<<endl;
		if (*pMode[i][0][0]>m)
		break;
	}
}

int main ()
{
	int n=5;
	int m=300;
	double c=3.0;


	cout<<"What is the width of the room?"<<endl;cin>>w;cout<<endl;
	cout<<"What is the length of the room?"<<endl;cin>>l;cout<<endl;
	cout<<"What is the height of the room?"<<endl;cin>>h;cout<<endl;
	cout<<"What is the temperature of the room in degrees Celsius?"<<endl;cin>>temp;cout<<endl;

	cout<<"width= "<<w<<"  length= "<<l<<"   height="<<h<<endl;
	cout<<"temperature= "<<temp;
	


//declare arrays used in modes:
	double mode[5][5][5];
	axialModes(l,w,h,c,m,n,&mode);
	cout<<"axial length mode 1= "<<mode[1][0][0]<<endl; //this give me a different result from where it was calculated in the function above
	return 0;
}


Getting only 1 error - and that is:
1
2
1>c:\users\usernamehaha\documents\visual studio 2008\projects\template\template\template.cpp(45) : error C2664: 'axialModes' : cannot convert parameter 7 from 'double (*)[5][5][5]' to 'double *[][5][5]'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


Anybody assist? I'm afraid this is a bit over my head
Last edited on
The program design is a bit muddled. There are lots of global variables, then a great deal of trouble is taken to pass those same variables as parameters. Either declare the variables globally and just use them directly, or declare them locally and pass them.

As for the array mode[5][5][5], if it is passed as a parameter, what is actually passed is a pointer to the start of the array. There's no need to complicate matters with & or * symbols.

Although it's generally recommended to avoid the use of global variables, the array mode could be declared globally which would eliminate the need to use it as a parameter in the function call.

This is based on the code in the original post of the thread:
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
#include <iostream>
#include <cmath>
#include <string>

using namespace std;

// global variables
const int NUM=5; // maximum number of modes calculated
double mode[NUM][NUM][NUM];


void axialModes(double l,double w, double h,double c, int m)
{
    for (int i=1; i<NUM; i++)
    {
        mode [i][0][0]=c/2*i/l;
        cout<<"length mode= "<<i<<" 0 0 "<<mode[i][0][0]<<" Hz"<<endl;
        if (mode[i][0][0]>m)
            break;
    }
}

int main ()
{

    double l;
    double w;
    double h;
    double temp;

    cout<<"What is the width of the room?"<<endl;
    cin>>w;

    cout<<"\nWhat is the length of the room?"<<endl;
    cin>>l;

    cout<<"\nWhat is the height of the room?"<<endl;
    cin>>h;

    cout<<"\nWhat is the temperature of the room in degrees Celsius?"<<endl;
    cin>>temp;

    cout<<"\nwidth= "<<w<<"  length= "<<l<<"   height="<<h<<endl;
    cout<<"temperature= "<<temp << endl;


    int m=300; //determines the frequency at which the modes stop being calculated

    axialModes(l,w,h,temp,m);

    cout<<"axial length mode 1= "<<mode[1][0][0]<<endl;

    return 0;
}
Last edited on
Okay, I had tried declaring globally, but it didn't compile because I was working with n in the main. I wasn't sure if i needed to use a formula for "n" in order to make the array possibly smaller, but since it merely stops infinite loops, I think I will just set this to a high number and adjust it if needbe as a constant. Setting it as a variable helps me adjust this easily rather than typing it numerous times throughout the program. Is there a way to declare the array globally and determine its size in the main if I need to use a formula to calculate "n" or how you have it "NUM"?
Well, I'm maybe not quite understanding the question, but I'll try to answer.

If you want to determine the size if the array in main(), just use the value of NUM. I used all capitals as this is often a conventional way of reminding the reader that a value is constant. It also in this case makes the value stand out visually, so you can see where the value is used.

Normally, I would just suggest making the array generously large and simply use a small part of it as required.

But multi-dimensional arrays can rapidly grow very large. For example if NUM=1000, which sounds quite modest, the memory requirement is 1000*1000*1000*8 bytes which is about 8GB (assuming a double requires 8 bytes). So you would need to be a bit cautious in setting the size.

However, you could use a more modest value, such as NUM=100 without the need to worry too much, and the program may use just part of the array. That is the user may choose a value of n, in the range 1 to NUM. Or the limit seems to be set by the frequency m=300 in any case.
Last edited on
Topic archived. No new replies allowed.