944,221 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2174
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 23rd, 2007
0

Arrays

Expand Post »
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.
Similar Threads
Reputation Points: 8
Solved Threads: 0
Newbie Poster
fudawala is offline Offline
11 posts
since Nov 2007
Nov 23rd, 2007
0

Re: Arrays

>>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.
Last edited by Ancient Dragon; Nov 23rd, 2007 at 10:55 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,966 posts
since Aug 2005
Nov 23rd, 2007
0

Re: Arrays

Hi,

This is my program.
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. const int ARRAYSIZE = 10;
  7. int i, current[ARRAYSIZE];
  8.  
  9. for (i = 0; i < ARRAYSIZE; i++) // Enter the current values
  10. {
  11. cout << "Enter a number: ";
  12. cin >> current[i];
  13. }
  14.  
  15. const int SIZE = 10;
  16. int j, resistance[SIZE];
  17.  
  18. for (j = 0; j < SIZE; j++) // Enter the resistance values
  19. {
  20. cout << "Enter a number: ";
  21. cin >> resistance[j];
  22. }
  23.  
  24. const int MAXNUMS = 10;
  25. int k, volts[MAXNUMS], volts = 0;
  26.  
  27. for (k = 0; k < MAXNUMS; k++) // Display and calculate the voltage
  28. {
  29. cout << "The voltage is " << volts[k] << endl;
  30. volts[k] = current[i] * resistance[j];
  31. }
  32.  
  33. return 0;
  34. }
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;.
Last edited by Ancient Dragon; Nov 23rd, 2007 at 11:19 pm. Reason: add code tags
Reputation Points: 8
Solved Threads: 0
Newbie Poster
fudawala is offline Offline
11 posts
since Nov 2007
Nov 23rd, 2007
0

Re: Arrays

you are attempting to duplicate the symbol name of the array and a variable. Variable names can not be duplicated.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,966 posts
since Aug 2005
Nov 23rd, 2007
0

Re: Arrays

I don't understand your last post thread about duplicating the variable names.
Reputation Points: 8
Solved Threads: 0
Newbie Poster
fudawala is offline Offline
11 posts
since Nov 2007
Nov 23rd, 2007
0

Re: Arrays

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

They must have different names
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,966 posts
since Aug 2005
Nov 24th, 2007
0

Re: Arrays

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:
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.  
  7. // This is for Current
  8.  
  9. const int ARRAYSIZE = 10;
  10. int i, current[ARRAYSIZE], sum = 0;
  11.  
  12. for (i = 0; i < ARRAYSIZE; i++) // Enter the current values
  13. {
  14. cout << "Enter a number: ";
  15. cin >> current[i];
  16. }
  17.  
  18. cout << "\nThe sum of the currents";
  19.  
  20. for (i = 0; i < ARRAYSIZE; i++) // Display the sum of the currents
  21. {
  22. cout << " " << current[i];
  23. sum = sum + current[i];
  24. }
  25.  
  26. cout << " is " << sum << endl;
  27.  
  28.  
  29. // This is for Resistance
  30.  
  31. const int SIZE = 10;
  32. int j, resistance[SIZE], number = 0;
  33.  
  34. for (j = 0; j < SIZE; j++) // Enter the resistance values
  35. {
  36. cout << "Enter a number: ";
  37. cin >> resistance[j];
  38. }
  39.  
  40. cout << "\nThe number of total resistances";
  41.  
  42. for (j = 0; j < SIZE; j++) // Display the number of total resistances
  43. {
  44. cout << " " << resistance[j];
  45. number = number + resistance[j];
  46. }
  47.  
  48. cout << " is " << number << endl;
  49.  
  50. const int MAXNUMS = 10;
  51. int k, volts[MAXNUMS], total = 0;
  52.  
  53. for (k = 0; k < MAXNUMS; k++) // Display and calculate the voltage
  54. {
  55. cout << "The voltage is " << volts[k] << endl;
  56. volts[k] = current[i] * resistance[j];
  57. }
  58.  
  59. cout << " is " << volts[k] << endl;
  60.  
  61. return 0;
  62. }
Last edited by Ancient Dragon; Nov 24th, 2007 at 12:53 am. Reason: add code tags
Reputation Points: 8
Solved Threads: 0
Newbie Poster
fudawala is offline Offline
11 posts
since Nov 2007
Nov 24th, 2007
0

Re: Arrays

C++ Syntax (Toggle Plain Text)
  1. for (k = 0; k < MAXNUMS; k++) // Display and calculate the voltage
  2. {
  3. cout << "The voltage is " << volts[k] << endl;
  4. volts[k] = current[i] * resistance[j];
  5. }
not 'Display and calculate the voltage'
but 'calculate first and then display the voltage'
swap the two lines in the for loop.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Nov 24th, 2007
0

Re: Arrays

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:
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.  
  7. // This is for Current
  8.  
  9. const int ARRAYSIZE = 10;
  10. int i, current[ARRAYSIZE], sum = 0;
  11.  
  12. for (i = 0; i < ARRAYSIZE; i++) // Enter the current values
  13. {
  14. cout << "Enter a number: ";
  15. cin >> current[i];
  16. }
  17.  
  18. cout << "\nThe sum of the currents";
  19.  
  20. for (i = 0; i < ARRAYSIZE; i++) // Display the sum of the currents
  21. {
  22. cout << " " << current[i];
  23. sum = sum + current[i];
  24. }
  25.  
  26. cout << " is " << sum << endl;
  27.  
  28.  
  29. // This is for Resistance
  30.  
  31. const int SIZE = 10;
  32. int j, resistance[SIZE], number = 0;
  33.  
  34. for (j = 0; j < SIZE; j++) // Enter the resistance values
  35. {
  36. cout << "Enter a number: ";
  37. cin >> resistance[j];
  38. }
  39.  
  40. cout << "\nThe number of total resistances";
  41.  
  42. for (j = 0; j < SIZE; j++) // Display the number of total resistances
  43. {
  44. cout << " " << resistance[j];
  45. number = number + resistance[j];
  46. }
  47.  
  48. cout << " is " << number << endl;
  49.  
  50. const int MAXNUMS = 10;
  51. int k, volts[MAXNUMS], total = 0;
  52.  
  53. for (k = 0; k < MAXNUMS; k++) // Display and calculate the voltage
  54. {
  55. volts[k] = current[i] * resistance[j];
  56. cout << "The voltage is " << volts[k] << endl;
  57. }
  58.  
  59. cout << " is " << volts[k] << endl;
  60.  
  61. return 0;
  62. }
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
Reputation Points: 8
Solved Threads: 0
Newbie Poster
fudawala is offline Offline
11 posts
since Nov 2007
Nov 24th, 2007
0

Re: Arrays

for (k = 0; k < MAXNUMS; k++) // calculate and display the voltage
{
volts[k] = current[k] * resistance[k];
cout << "The voltage is " << volts[k] << endl;
}
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: need help writing function
Next Thread in C++ Forum Timeline: Microsoft Visual C++ Express





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC