| | |
Arrays
![]() |
•
•
Join Date: Nov 2007
Posts: 11
Reputation:
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.
>>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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Nov 2007
Posts: 11
Reputation:
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: Nov 2007
Posts: 11
Reputation:
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
Posts: 1,089
Reputation:
Solved Threads: 164
C++ Syntax (Toggle Plain Text)
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:
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
![]() |
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
| Thread Tools | Search this Thread |
action api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node noob output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






