•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 455,969 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,760 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1210 | Replies: 22
![]() |
•
•
Join Date: Nov 2007
Posts: 11
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation:
Rep Power: 40
Solved Threads: 972
>>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.
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.
Last edited by Ancient Dragon : Nov 23rd, 2007 at 10:55 pm.
•
•
Join Date: Nov 2007
Posts: 11
Reputation:
Rep Power: 0
Solved Threads: 0
Hi,
This is my program.
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;.
This is my program.
c++ Syntax (Toggle Plain Text)
#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; }
Last edited by Ancient Dragon : Nov 23rd, 2007 at 11:19 pm. Reason: add code tags
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation:
Rep Power: 40
Solved Threads: 972
you are attempting to duplicate the symbol name of the array and a variable. Variable names can not be duplicated.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation:
Rep Power: 40
Solved Threads: 972
volts[MAXNUMS] is the name of an array
volts = 0 is the name of a simple integer
They must have different names
volts = 0 is the name of a simple integer
They must have different names
•
•
Join Date: Nov 2007
Posts: 11
Reputation:
Rep Power: 0
Solved Threads: 0
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:
This is my program:
c++ Syntax (Toggle Plain Text)
#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; }
Last edited by Ancient Dragon : Nov 24th, 2007 at 12:53 am. Reason: add code tags
•
•
Join Date: Dec 2006
Location: india
Posts: 1,084
Reputation:
Rep Power: 9
Solved Threads: 163
for (k = 0; k < MAXNUMS; k++) // Display and calculate the voltage
{
cout << "The voltage is " << volts[k] << endl;
volts[k] = current[i] * resistance[j];
}but 'calculate first and then display the voltage'
swap the two lines in the for loop.
•
•
Join Date: Nov 2007
Posts: 11
Reputation:
Rep Power: 0
Solved Threads: 0
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:
This is my program again:
c++ Syntax (Toggle Plain Text)
#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; }
Last edited by Ancient Dragon : Nov 24th, 2007 at 12:55 am. Reason: This is the third time I've had to add code tags
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- (reformatted) How to return Multi-Dimensional Arrays (C++)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- Arrays (C++)
- How to Return Multidimensional Arrays (C++)
- C file input/output 2D arrays. (C)
- passing arrays in visual basic (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: need help writing function
- Next Thread: Microsoft Visual C++ Express



Linear Mode