User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Nov 2007
Posts: 11
Reputation: fudawala is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
fudawala fudawala is offline Offline
Newbie Poster

Help Arrays

  #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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Arrays

  #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.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Nov 2007
Posts: 11
Reputation: fudawala is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
fudawala fudawala is offline Offline
Newbie Poster

Help Re: Arrays

  #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  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Arrays

  #4  
Nov 23rd, 2007
you are attempting to duplicate the symbol name of the array and a variable. Variable names can not be duplicated.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Nov 2007
Posts: 11
Reputation: fudawala is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
fudawala fudawala is offline Offline
Newbie Poster

Help Re: Arrays

  #5  
Nov 23rd, 2007
I don't understand your last post thread about duplicating the variable names.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Arrays

  #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
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Nov 2007
Posts: 11
Reputation: fudawala is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
fudawala fudawala is offline Offline
Newbie Poster

Help Re: Arrays

  #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  
Join Date: Dec 2006
Location: india
Posts: 1,084
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 
Rep Power: 9
Solved Threads: 163
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Arrays

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

Help Re: Arrays

  #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  
Join Date: Dec 2006
Location: india
Posts: 1,084
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 
Rep Power: 9
Solved Threads: 163
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Arrays

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:11 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC