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

Re: Arrays

 
0
  #11
Nov 24th, 2007
I don't understand what you have last posted up. Please explain.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,615
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: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Arrays

 
0
  #12
Nov 24th, 2007
See index values shown in RED. You should have used k instead of i and j.
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: 3
Reputation: balaji_m_11 is an unknown quantity at this point 
Solved Threads: 0
balaji_m_11 balaji_m_11 is offline Offline
Newbie Poster

Re: Arrays

 
0
  #13
Nov 24th, 2007
VOLTS=0. Here we cant initialize volts.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3
Reputation: balaji_m_11 is an unknown quantity at this point 
Solved Threads: 0
balaji_m_11 balaji_m_11 is offline Offline
Newbie Poster

Re: Arrays

 
0
  #14
Nov 24th, 2007
What is the purpose of initializing the var total?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3
Reputation: balaji_m_11 is an unknown quantity at this point 
Solved Threads: 0
balaji_m_11 balaji_m_11 is offline Offline
Newbie Poster

Re: Arrays

 
0
  #15
Nov 24th, 2007
Its not necessary to initialize an array to 0.
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

 
-1
  #16
Nov 24th, 2007
The program of the output shows everything correctly. Just have one more question. How can I show for each column heading display of its appropriate value.

For example:

Current Resistance Volts

This is my program:
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.  
  7. // This for loop enters the current values.
  8.  
  9. const int ARRAYSIZE = 10;
  10. int i, current[ARRAYSIZE];
  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. // This for loop enters the resistance values.
  19.  
  20. const int SIZE = 10;
  21. int j, resistance[SIZE];
  22.  
  23. for (j = 0; j < SIZE; j++) // Enter the resistance values
  24. {
  25. cout << "Enter a number: ";
  26. cin >> resistance[j];
  27. }
  28.  
  29. // This for loop calculates and displays the voltage.
  30.  
  31. const int MAXNUMS = 10;
  32. int k, volts[MAXNUMS];
  33.  
  34. for (k = 0; k < MAXNUMS; k++)
  35. {
  36. volts[k] = current[k] * resistance[k];
  37. cout << current[k] << '\t' << resistance[k] << '\t' << volts[k] << endl;
  38. }
  39.  
  40. return 0;
  41. }
Last edited by Ancient Dragon; Nov 24th, 2007 at 7:36 pm. Reason: add code tags -- 4th attempt
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,753
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Arrays

 
0
  #17
Nov 24th, 2007
Use cout to put the column lables you want between the second and third loop. Then format the output statement in the third loop so the output lines up under the appropriate label.
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
  #18
Nov 25th, 2007
Thank You. The other program worked fine.

Now for the last program, the program states to store the following prices in an array named resistance: 16, 27, 39, 56, and 81. Your program should also create two arrays named current and power each capable of storing five double precision numbers. Using a for loop and a cin statement have your program accept 5 user input values into the current array once the program is run. Your program should store the product of the corresponding values of the square of the current array and the resistance array in the power array (for example, power[1] = resistance[1] * pow(current[1],2) and displays the following output.

I have done the program. I just want some feedback if I have done the program the right way.
Thanks.
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. const int MAXELS = 5;
  8. int i, num, resistance[MAXELS] = {16, 27, 39, 56, 81};
  9.  
  10. num = resistance[0];
  11.  
  12. // This for loop contains the cin statement to accept five user input numbers for the current.
  13.  
  14. const int MAXNUMS = 5;
  15. int j, current[MAXNUMS];
  16.  
  17. for (j = 0; j < MAXNUMS; j++)
  18. {
  19. cout << " " << current[j];
  20. cin >> current[j];
  21. }
  22.  
  23. // This for loop that calculates the power and the total then it displays the result.
  24.  
  25. const int ARRAYSIZE = 5;
  26. int k, power[ARRAYSIZE];
  27.  
  28. for (k = 0; k < MAXNUMS; k++)
  29. {
  30. power[k] = resistance[k] * pow(current[k],2);
  31. power[k] = resistance[k] * pow(current[k],2);
  32. power[k] = resistance[k] * pow(current[k],2);
  33. power[k] = resistance[k] * pow(current[k],2);
  34. power[k] = resistance[k] * pow(current[k],2);
  35. cout << power[k] << endl;
  36. }
  37.  
  38. return 0;
  39. }
Last edited by Ancient Dragon; Nov 25th, 2007 at 9:48 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Arrays

 
0
  #19
Nov 25th, 2007
Are you dumber than a box of rocks?

Do none of these comments mean anything to you?
Last edited by Ancient Dragon : 1 Day Ago at 03:19. Reason: add code tags
Last edited by Ancient Dragon : 1 Day Ago at 04:53. Reason: add code tags
Last edited by Ancient Dragon : 1 Day Ago at 04:55. Reason: This is the third time I've had to add code tags
Last edited by Ancient Dragon : 8 Hours Ago at 23:36. Reason: add code tags -- 4th attempt

Are you not even the tiniest bit curious as to why your posts change to be nicely formatted?

Did you even bother to read this thread?
http://www.daniweb.com/forums/announcement8-3.html

Do you even notice the water mark at the back of the edit window telling you about code tags?

Have you ever considered using the "preview post" feature to make sure you're presenting your question in the best possible light?
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
  #20
Nov 25th, 2007
Sorry if I didn't get the code tags. I'm a new user and trying to be in the environment here.

Thank You for the other program it worked just fine.

Now for the last program, the program states to store the following prices in an array named resistance: 16, 27, 39, 56, and 81. Your program should also create two arrays named current and power each capable of storing five double precision numbers. Using a for loop and a cin statement have your program accept 5 user input values into the current array once the program is run. Your program should store the product of the corresponding values of the square of the current array and the resistance array in the power array (for example, power[1] = resistance[1] * pow(current[1],2) and displays the following output.

This is my program:

  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. const int MAXELS = 5;
  8. int i, num, resistance[MAXELS] = {16, 27, 39, 56, 81};
  9.  
  10. num = resistance[0];
  11.  
  12. // This for loop contains the cin statement to accept five user input numbers for the current.
  13.  
  14. const int MAXNUMS = 5;
  15. int j, current[MAXNUMS];
  16.  
  17. for (j = 0; j < MAXNUMS; j++)
  18. {
  19. cout << " " << current[j];
  20. cin >> current[j];
  21. }
  22.  
  23. // This for loop that calculates the power and the total then it displays the result.
  24.  
  25. const int ARRAYSIZE = 5;
  26. int k, power[ARRAYSIZE];
  27.  
  28. for (k = 0; k < MAXNUMS; k++)
  29. {
  30. power[k] = resistance[k] * pow(current[k],2);
  31. power[k] = resistance[k] * pow(current[k],2);
  32. power[k] = resistance[k] * pow(current[k],2);
  33. power[k] = resistance[k] * pow(current[k],2);
  34. power[k] = resistance[k] * pow(current[k],2);
  35. cout << power[k] << endl;
  36. }
  37.  
  38. return 0;
  39. }
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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC