Hi,

I need some help writing this program. The question states to write a program that specifies three one-dimensional arrays named current, resistance, and volts. Each array should be capable of holding 10 elements. Using a for loop, input values for the current and resistance arrays. The entries in the volts array should be the product of the corresponding values in the current and resistance arrays (thus, volts = current * resistance). After all of the data has been entered, display the following output:
Current Resistance Volts:
Under each column heading display the appropriate value.

>>I need some help writing this program
Which part are you confused about? We are not going to write the program for you. So you might as well do what you can, post your program, and ask specific questions. The instructions seem to be pretty clear to me.

Hi,

This is my program.

#include <iostream>
using namespace std;

int main()
{
	const int ARRAYSIZE = 10;
	int i, current[ARRAYSIZE];

	for (i = 0; i < ARRAYSIZE; i++) // Enter the current values
	{
		cout << "Enter a number: ";
		cin >> current[i];
	}

	const int SIZE = 10;
	int j, resistance[SIZE];

	for (j = 0; j < SIZE; j++) // Enter the resistance values
	{
		cout << "Enter a number: ";
		cin >> resistance[j];
	}

	const int MAXNUMS = 10;
	int k, volts[MAXNUMS], volts = 0;

	for (k = 0; k < MAXNUMS; k++) // Display and calculate the voltage
	{
		cout << "The voltage is " << volts[k] << endl;
		volts[k] = current[i] * resistance[j];
	}

	return 0;
}

But the program didn't succeed here. The error message that I'm receiving is c:\documents and settings\fidaali\my documents\computer science 102\professor hadi - homework 2 - prg2\prg2.cpp(25) : error C2040: 'volts' : 'int' differs in levels of indirection from 'int [10]'. The error message points me in the line int k, volts[MAXNUMS], volts = 0;.

you are attempting to duplicate the symbol name of the array and a variable. Variable names can not be duplicated.

I don't understand your last post thread about duplicating the variable names.

volts[MAXNUMS] is the name of an array
volts = 0 is the name of a simple integer

They must have different names

I have edited the program that I have created. Can you please check if this is right so far. The only problem I'm having is that it is calculating the voltage but the value I'm getting every time is -858993460 whenever I input any value. The program is suppose to calculate the voltage by multiplying current times resistance.

This is my program:

#include <iostream>
using namespace std;

int main()
{

// This is for Current

	const int ARRAYSIZE = 10;
	int i, current[ARRAYSIZE], sum = 0;

	for (i = 0; i < ARRAYSIZE; i++) // Enter the current values
	{
		cout << "Enter a number: ";
		cin >> current[i];
	}

	cout << "\nThe sum of the currents";

	for (i = 0; i < ARRAYSIZE; i++) // Display the sum of the currents
	{
		cout << " " << current[i];
		sum = sum + current[i];
	}

	cout << " is " << sum << endl;


// This is for Resistance

	const int SIZE = 10;
	int j, resistance[SIZE], number = 0;

	for (j = 0; j < SIZE; j++) // Enter the resistance values
	{
		cout << "Enter a number: ";
		cin >> resistance[j];
	}

	cout << "\nThe number of total resistances";

	for (j = 0; j < SIZE; j++) // Display the number of total resistances
	{
		cout << " " << resistance[j];
		number = number + resistance[j];
	}

	cout << " is " << number << endl;

	const int MAXNUMS = 10;
	int k, volts[MAXNUMS], total = 0;

	for (k = 0; k < MAXNUMS; k++) // Display and calculate the voltage
	{
		cout << "The voltage is " << volts[k] << endl;
		volts[k] = current[i] * resistance[j];
	}

	cout << " is " << volts[k] << endl;

	return 0;
}
for (k = 0; k < MAXNUMS; k++) // Display and calculate the voltage
{
  cout << "The voltage is " << volts[k] << endl;
  volts[k] = current[i] * resistance[j];
}

not 'Display and calculate the voltage'
but 'calculate first and then display the voltage'
swap the two lines in the for loop.

The output that I have gotten is still not correct. When I try putting in 2 for 10 integers for the current and resistance it should be give me a total of 40 for both. When I multiply them, to get the voltage it should be give me 400 volts not 687194768.

This is my program again:

#include <iostream>
using namespace std;

int main()
{

// This is for Current

	const int ARRAYSIZE = 10;
	int i, current[ARRAYSIZE], sum = 0;

	for (i = 0; i < ARRAYSIZE; i++) // Enter the current values
	{
		cout << "Enter a number: ";
		cin >> current[i];
	}

	cout << "\nThe sum of the currents";

	for (i = 0; i < ARRAYSIZE; i++) // Display the sum of the currents
	{
		cout << " " << current[i];
		sum = sum + current[i];
	}

	cout << " is " << sum << endl;


// This is for Resistance

	const int SIZE = 10;
	int j, resistance[SIZE], number = 0;

	for (j = 0; j < SIZE; j++) // Enter the resistance values
	{
		cout << "Enter a number: ";
		cin >> resistance[j];
	}

	cout << "\nThe number of total resistances";

	for (j = 0; j < SIZE; j++) // Display the number of total resistances
	{
		cout << " " << resistance[j];
		number = number + resistance[j];
	}

	cout << " is " << number << endl;

	const int MAXNUMS = 10;
	int k, volts[MAXNUMS], total = 0;

	for (k = 0; k < MAXNUMS; k++) // Display and calculate the voltage
	{
		volts[k] = current[i] * resistance[j];
		cout << "The voltage is " << volts[k] << endl;
	}

	cout << " is " << volts[k] << endl;

	return 0;
}
for (k = 0; k < MAXNUMS; k++) // calculate and display the voltage
{
volts[k] = current[[B]k[/B]] * resistance[[B]k[/B]];
cout << "The voltage is " << volts[k] << endl;
}

I don't understand what you have last posted up. Please explain.

See index values shown in RED. You should have used k instead of i and j.

VOLTS=0. Here we cant initialize volts.

What is the purpose of initializing the var total?

Its not necessary to initialize an array to 0.

The program of the output shows everything correctly. Just have one more question. How can I show for each column heading display of its appropriate value.

For example:

Current Resistance Volts

This is my program:

#include <iostream>
using namespace std;

int main()
{

// This for loop enters the current values.

	const int ARRAYSIZE = 10;
	int i, current[ARRAYSIZE];

	for (i = 0; i < ARRAYSIZE; i++) // Enter the current values
	{
		cout << "Enter a number: ";
		cin >> current[i];
	}
	
// This for loop enters the resistance values.

	const int SIZE = 10;
	int j, resistance[SIZE];

	for (j = 0; j < SIZE; j++) // Enter the resistance values
	{
		cout << "Enter a number: ";
		cin >> resistance[j];
	}

// This for loop calculates and displays the voltage.

	const int MAXNUMS = 10;
	int k, volts[MAXNUMS];

	for (k = 0; k < MAXNUMS; k++)
	{
		volts[k] = current[k] * resistance[k];
		cout << current[k] << '\t' << resistance[k] << '\t' << volts[k] << endl;
	}

	return 0;
}
commented: 4th attempt at posting code, still no code tags. Do you even listen? -2

Use cout to put the column lables you want between the second and third loop. Then format the output statement in the third loop so the output lines up under the appropriate label.

Thank You. The other program worked fine.

Now for the last program, the program states to store the following prices in an array named resistance: 16, 27, 39, 56, and 81. Your program should also create two arrays named current and power each capable of storing five double precision numbers. Using a for loop and a cin statement have your program accept 5 user input values into the current array once the program is run. Your program should store the product of the corresponding values of the square of the current array and the resistance array in the power array (for example, power[1] = resistance[1] * pow(current[1],2) and displays the following output.

I have done the program. I just want some feedback if I have done the program the right way.
Thanks.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	const int MAXELS = 5;
	int i, num, resistance[MAXELS] = {16, 27, 39, 56, 81};

	num = resistance[0];

// This for loop contains the cin statement to accept five user input numbers for the current.

	const int MAXNUMS = 5;
	int j, current[MAXNUMS];

	for (j = 0; j < MAXNUMS; j++)
	{
		cout << " " << current[j];
		cin >> current[j];
	}

// This for loop that calculates the power and the total then it displays the result.

	const int ARRAYSIZE = 5;
	int k, power[ARRAYSIZE];

	for (k = 0; k < MAXNUMS; k++)
	{
		power[k] = resistance[k] * pow(current[k],2);
		power[k] = resistance[k] * pow(current[k],2);
		power[k] = resistance[k] * pow(current[k],2);
		power[k] = resistance[k] * pow(current[k],2);
		power[k] = resistance[k] * pow(current[k],2);
		cout << power[k] << endl;
	}

	return 0;
}

Are you dumber than a box of rocks?

Do none of these comments mean anything to you?
Last edited by Ancient Dragon : 1 Day Ago at 03:19. Reason: add code tags
Last edited by Ancient Dragon : 1 Day Ago at 04:53. Reason: add code tags
Last edited by Ancient Dragon : 1 Day Ago at 04:55. Reason: This is the third time I've had to add code tags
Last edited by Ancient Dragon : 8 Hours Ago at 23:36. Reason: add code tags -- 4th attempt

Are you not even the tiniest bit curious as to why your posts change to be nicely formatted?

Did you even bother to read this thread?
http://www.daniweb.com/forums/announcement8-3.html

Do you even notice the water mark at the back of the edit window telling you about code tags?

Have you ever considered using the "preview post" feature to make sure you're presenting your question in the best possible light?

Sorry if I didn't get the code tags. I'm a new user and trying to be in the environment here.

Thank You for the other program it worked just fine.

Now for the last program, the program states to store the following prices in an array named resistance: 16, 27, 39, 56, and 81. Your program should also create two arrays named current and power each capable of storing five double precision numbers. Using a for loop and a cin statement have your program accept 5 user input values into the current array once the program is run. Your program should store the product of the corresponding values of the square of the current array and the resistance array in the power array (for example, power[1] = resistance[1] * pow(current[1],2) and displays the following output.

This is my program:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	const int MAXELS = 5;
	int i, num, resistance[MAXELS] = {16, 27, 39, 56, 81};

	num = resistance[0];

// This for loop contains the cin statement to accept five user input numbers for the current.

	const int MAXNUMS = 5;
	int j, current[MAXNUMS];

	for (j = 0; j < MAXNUMS; j++)
	{
		cout << " " << current[j];
		cin >> current[j];
	}

// This for loop that calculates the power and the total then it displays the result.

	const int ARRAYSIZE = 5;
	int k, power[ARRAYSIZE];

	for (k = 0; k < MAXNUMS; k++)
	{
		power[k] = resistance[k] * pow(current[k],2);
		power[k] = resistance[k] * pow(current[k],2);
		power[k] = resistance[k] * pow(current[k],2);
		power[k] = resistance[k] * pow(current[k],2);
		power[k] = resistance[k] * pow(current[k],2);
		cout << power[k] << endl;
	}

	return 0;
}

>> each capable of storing five double precision numbers
That means you have to declare the arrays as double, not int.

Also, I find it a lot easier to normally declare all variables at the top of the function unless there is a good reason not to. I know c++ allows you to declare them anywhere but if you put them at the top its a lot easier to find them -- you don't have to search the entire function to see how a variable is declared.

> power[k] = resistance[k] * pow(current[k],2);
This same line is repeated 5 times.
The answer isn't going to change, so trim the code.

> power[k] = resistance[k] * pow(current[k],2);
This same line is repeated 5 times.
The answer isn't going to change, so trim the code.

I think he just wants to make sure the answer sicks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.