Arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 11
Reputation: fudawala is an unknown quantity at this point 
Solved Threads: 0
fudawala fudawala is offline Offline
Newbie Poster

Arrays

 
0
  #1
Nov 23rd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,344
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Arrays

 
0
  #2
Nov 23rd, 2007
>>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.
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 11
Reputation: fudawala is an unknown quantity at this point 
Solved Threads: 0
fudawala fudawala is offline Offline
Newbie Poster

Re: Arrays

 
0
  #3
Nov 23rd, 2007
Hi,

This is my program.
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,344
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Arrays

 
0
  #4
Nov 23rd, 2007
you are attempting to duplicate the symbol name of the array and a variable. Variable names can not be duplicated.
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 11
Reputation: fudawala is an unknown quantity at this point 
Solved Threads: 0
fudawala fudawala is offline Offline
Newbie Poster

Re: Arrays

 
0
  #5
Nov 23rd, 2007
I don't understand your last post thread about duplicating the variable names.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,344
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Arrays

 
0
  #6
Nov 23rd, 2007
volts[MAXNUMS] is the name of an array
volts = 0 is the name of a simple integer

They must have different names
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 11
Reputation: fudawala is an unknown quantity at this point 
Solved Threads: 0
fudawala fudawala is offline Offline
Newbie Poster

Re: Arrays

 
0
  #7
Nov 24th, 2007
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:
  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Arrays

 
0
  #8
Nov 24th, 2007
  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 11
Reputation: fudawala is an unknown quantity at this point 
Solved Threads: 0
fudawala fudawala is offline Offline
Newbie Poster

Re: Arrays

 
0
  #9
Nov 24th, 2007
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:
  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Arrays

 
0
  #10
Nov 24th, 2007
for (k = 0; k < MAXNUMS; k++) // calculate and display the voltage
{
volts[k] = current[k] * resistance[k];
cout << "The voltage is " << volts[k] << endl;
}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC