954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Arrays

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[i] = current[i] * resistance[i]). After all of the data has been entered, display the following output:
Current Resistance Volts:
Under each column heading display the appropriate value.

fudawala
Newbie Poster
11 posts since Nov 2007
Reputation Points: 8
Solved Threads: 0
 

>>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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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 lineint k, volts[MAXNUMS], volts = 0;.

fudawala
Newbie Poster
11 posts since Nov 2007
Reputation Points: 8
Solved Threads: 0
 

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

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

fudawala
Newbie Poster
11 posts since Nov 2007
Reputation Points: 8
Solved Threads: 0
 

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

They must have different names

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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;
}
fudawala
Newbie Poster
11 posts since Nov 2007
Reputation Points: 8
Solved Threads: 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.

vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

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;
}
fudawala
Newbie Poster
11 posts since Nov 2007
Reputation Points: 8
Solved Threads: 0
 
for (k = 0; k < MAXNUMS; k++) // calculate and display the voltage
{
volts[k] = current[<strong>k</strong>] * resistance[<strong>k</strong>];
cout << "The voltage is " << volts[k] << endl;
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

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

fudawala
Newbie Poster
11 posts since Nov 2007
Reputation Points: 8
Solved Threads: 0
 

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

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

VOLTS=0. Here we cant initialize volts.

balaji_m_11
Newbie Poster
3 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

What is the purpose of initializing the var total?

balaji_m_11
Newbie Poster
3 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

Its not necessary to initialize an array to 0.

balaji_m_11
Newbie Poster
3 posts since Nov 2007
Reputation Points: 10
Solved Threads: 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;
}
fudawala
Newbie Poster
11 posts since Nov 2007
Reputation Points: 8
Solved Threads: 0
 

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.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

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;
}
fudawala
Newbie Poster
11 posts since Nov 2007
Reputation Points: 8
Solved Threads: 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?

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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;
}
fudawala
Newbie Poster
11 posts since Nov 2007
Reputation Points: 8
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You