Help with arrays?

Hi, i am supposed to write a program that declares 3 one dimensional arrays. I am supposed to pass three arrays (volts, current, resistance) to a function called calc_volts. This should calculate the volts in the array named volts. This is what i have so far, but i am confused as to how to pass the three arrays to the function, and then how to calculate the volts in the volt arrray and then output that to the screen.

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

using namespace std;

int main()
{
	const double maxnum1 = 10;
	int i, double current[maxnum1] = {10.62,14.89,13.21,16.55,19.62,9.47,6.58,18.32,12.15,3.98};
	const double maxnum2 = 10;
	int j, double volts[maxnum2];
	const double maxnum3 = 10;
	int k, double resistance[maxnum3] = {4,8.5,6,7.35,9,15.3,3,5.4,2.9,4.8};


	for (i=0, i<maxnum1, i++)
		
	for (j=0, j<maxnum2 , j++)
	volts = calc_volts;


}

double calc_volts(double current[],double volts[], double resistance[])
{double volts;
	volts = current[i]*resistance[j];
	return volts;

}
You need a way to tell the program how large the array's are. Luckily you already have this ... three times in fact. You realize that you need all three variable for this equation to work right? So declaring maxnum2 and maxnum3 is a bit redundant.

As for calling a function you just need to pass the variables to it in the same order that they are expected so Line 20 becomes: volts = calc_volts(current, volts, resistance, maxnum1); //Assuming you've added the variable to the function definition

Also, either copy and paste the definition for "calc_volts" so that it is above main or forward declare it, but what you have right now won't work.
Last edited on
Ok, i made your recommended changes. I don't get why you have maxnum1 being passes into the function? isnt that already taken into account when i call the arrays? Here is what i have so far, not done yet obviously.

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

using namespace std;
double calc_volts(double, double, double);

int main()
{
	const int maxnum1 = 10;
	int i, double current[maxnum1] = {10.62,14.89,13.21,16.55,19.62,9.47,6.58,18.32,12.15,3.98};
	
	int j, double volts[maxnum1];

	int k, double resistance[maxnum1] = {4,8.5,6,7.35,9,15.3,3,5.4,2.9,4.8};


	for (i=0, i<maxnum1, i++)
		
	for (j=0, j<maxnum1 , j++)
	volts[j] = calc_volts(current, volts, resistance, maxnum1);


}

double calc_volts(double current[i],double volts[j], double resistance[k])
{double volts;
	volts[j] = current[i]*resistance[k];
	return volts;

}
Wow, I didn't even notice those for loops the first time around. Shows how much I pay attention to what I'm doing. Why are those there? If part of the assignment is to pass the variables as arrays then you don't need to iterate through them. Also, you need to get rid of the comma's in Lines 11, 13 and 15 and replace them with semi-colons. You should also put a carriage return after the semi-colons but you do not technically have to.

You changed Line 26, undo that (get rid of the 'i', 'j' and 'k'). You can't pass a variable to a function if the function isn't expecting it (without an ellipse but don't worry about that right now), so on Lines 6 and 26 add an integer value in the function definition. We are passing the "maxnum1" variable into "calc_volts" to tell it how large each array is. You should only need one for loop inside "calc_values" to iterate through the array's and you need to get rid of the declaration for "volts" on Line 27.
How do i add an integer value to line 6 and 26? which integer value? also, how should my for loop look like? i don't know how to put both the 'resistance' array and the 'current' array values in it? Which for loop should i take out?


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

using namespace std;
double calc_volts(double, double, double);

int main()
{
	const int maxnum1 = 10;
	int i; double current[maxnum1] = {10.62,14.89,13.21,16.55,19.62,9.47,6.58,18.32,12.15,3.98};
	
	int j; double volts[maxnum1];

	int k; double resistance[maxnum1] = {4,8.5,6,7.35,9,15.3,3,5.4,2.9,4.8};


	for (i=0, i<maxnum1, i++)
		
	for (j=0, j<maxnum1 , j++)
	volts[j] = calc_volts(current, volts, resistance, maxnum1);


}

double calc_volts(double current[],double volts[], double resistance[])
{
	volts[j] = current[i]*resistance[k];
	return volts;

}
Line 6: double calc_volts(double, double, double, int);

Line 26: double calc_volts(double current[],double volts[], double resistance[], int Max)

You should get rid of both of the for loops in "main" and add a for loop to "calc_volts". You're really close to the answer on this one, forum rules actually tell us not to give out answers to assignments though. Take a short break if you need one and come back to it.
oh ok, sorry. but thanks for your help, ill come back if i cant get it still.
I don't mean take a break from this forum, I meant take a break from this project if you need to. Mental fatigue is a real thing.
i know, i am.
Ok, here is what i have now. I get errors saying 'unreferenced local variable' and 'uninitialized local variable' for i, j, and k. I can't figure out how to fix this.

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

using namespace std;
double calc_volts(double[], double[], double[], int);


int main()
	
{

	const int max = 10;
	int i; double current[max] = {10.62,14.89,13.21,16.55,19.62,9.47,6.58,18.32,12.15,3.98};
	
	int j; double volts[max];

	int k; double resistance[max] = {4,8.5,6,7.35,9,15.3,3,5.4,2.9,4.8};

	volts[j] = calc_volts(current, resistance, resistance, max);

	cout<<"This program will output the voltage from a given current and resistance"<<endl<<endl;
	

	for (j = 0; j<max; j++)
		cout<<current<<endl<<endl;
		cout<<resistance<<endl<<endl;
		cout<<j<<endl;


}

double calc_volts(double current[],double volts[], double resistance[], int max)
{int i, j,k;

	for (j = 0; j<max; j++)
	volts[j] = current[i]*resistance[k];
	
	return volts[j];

}
 
int j; double volts[max];


j doesn't have a value, then:

 
volts[j] = calc_volts(current, resistance, resistance, max);

you can't initialize your volts array using j


same here:

for: i,k
-to iterate through volts array-

1
2
3
4
5
6
7
8
9
double calc_volts(double current[],double volts[], double resistance[], int max)
{int i, j,k;

	for (j = 0; j<max; j++)
	volts[j] = current[i]*resistance[k];
	
	return volts[j];

}


and in main()
i do not know for what are you
using
i
variable
Last edited on
i is for the array current. Ok, i get what you are saying. so how do i pass my values from my calc_volts functions back to the volts array, and then output those from my main.
I have an example for you:
i think that this is what
you need to do

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
#include <iostream>
using std::cout;
using std::endl;


//function prototypes
void write_in_array(int original_array[],int array_1[],int array_2[],const int SIZE); 

void print_array(int array_to_output[],const int SIZE);



int main(){

const int SIZE=5;

int original_array[SIZE]={};
int array_1[SIZE]={2,2,2,2,2};
int array_2[SIZE]={4,4,4,4,4};

//print original array
print_array(original_array,SIZE);

//using write_in_array
write_in_array(original_array,array_1,array_2,SIZE);

//print original array again
print_array(original_array,SIZE);



return 0; //indicates success
}//end main


void write_in_array(int original_array[],int array_1[],int array_2[],const int SIZE){
	for(int index=0;index<SIZE;index++)
		original_array[index]=array_1[index]*array_2[index];

}//end function write_in_array

void print_array(int array_to_output[],const int SIZE){
	for(int i=0;i<SIZE;i++)
		cout<<array_to_output[i]<<' ';

cout<<endl;
}//end function print_array 



0 0 0 0 0 
8 8 8 8 8
Last edited on
I got it to compile, but the numbers are not right and have other random digits in between.

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

using namespace std;
double calc_volts(double[], double[], double[]);


int main()
	
{

	
	int j=0; 
	
	double current[10] = {10.62,14.89,13.21,16.55,19.62,9.47,6.58,18.32,12.15,3.98};
	
	double volts[10];

	double resistance[10] = {4,8.5,6,7.35,9,15.3,3,5.4,2.9,4.8};

	volts[j] = calc_volts(current, volts, resistance);

	cout<<"This program will output the voltage from a given current and resistance"<<endl<<endl;
	

	for (j = 0; j<10; j++)
	{
		cout<<setw(6)<<fixed<<setprecision(3)<<current<<endl<<endl;
		cout<<setw(6)<<fixed<<setprecision(3)<<resistance<<endl<<endl;
		cout<<setw(6)<<fixed<<setprecision(3)<<volts[j]<<endl<<endl;
	}

	system("pause");
		return 0;

}
double calc_volts(double current[],double volts[], double resistance[])
{int j;

	for (j = 0; j<10; j++)
	volts[j] = current[j]*resistance[j];
	
	return volts[j];

}
double printvolts (double outputvoltage[])
{for(int v=0;v<10;v++)
	cout<<setw(6)<<fixed<<setprecision(3)<<outputvoltage<<endl;
return 0;
}
it is a better idea
pass a SIZE value
as an argument
-to print or write in an array
inside a body function-

 
double calc_volts(double[], double[], double[]);


in main()

you don't need:
1
2
3
int i;
int j;
int k;


resistance array
twice?
 
volts[j] = calc_volts(current, resistance, resistance);


use a constant value as
in your previous version:
const int max = 10;

you can use it to
pass as argument
to write in or print
an array

in:
1
2
3
4
5
6
7
8
9
double calc_volts(double current[],double volts[], double resistance[])
{int i, j,k;

	for (j = 0; j<10; j++)
	volts[j] = current[i]*resistance[k];
	
	return volts[j];

}


you don't need
1
2
i and
k
variables

you can use the
same variable to
iterate through
1
2
3
volts[]
current[] and
resistance[]
arrays


and in "for loops"
i prefer do this:
1
2
3
for(int j=0;j<SOMEVALUE;j++){
   //body
}


and in the end
1
2
3
4
5
6
7
8
9
double calc_volts(double current[],double volts[], double resistance[])
{int i, j,k;

	for (j = 0; j<10; j++)
	volts[j] = current[i]*resistance[k];
	
	return volts[j];

}

you are returning
the value of the array
volts in
the position j not
the whole array i
think that is a
logic error

take a time to study my example
and try to figure out how
you have to implement it
to resolve your problem.





Last edited on
Topic archived. No new replies allowed.