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 456,607 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,492 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: 1304 | Replies: 24
Reply
Join Date: Nov 2007
Posts: 219
Reputation: nurulshidanoni is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nurulshidanoni's Avatar
nurulshidanoni nurulshidanoni is offline Offline
Posting Whiz in Training

min value

  #1  
Nov 6th, 2007
hello,
why the output below produce number 0, eventhought there is no 0 in the array?
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <cstdio>
  6. #include <ctime>
  7. using namespace std;
  8.  
  9. int i;
  10. int x[7];
  11. int A[7], B[7], C[7], D[7], E[7], F[7];
  12. int Min( const int *A, const int Count);
  13. int minimum, minA, minB, flagB;
  14. int main(void);
  15. int count = 7;
  16. {
  17. for(i=1;i<7;i++)
  18. {
  19. x[i]=i;
  20. A[i]=(2+pow(x[i],2));
  21. B[i]=(1+pow(x[i],3));
  22. C[i]=(2+pow(x[i],2)-1);
  23. D[i]=(3*pow(x[i],3));
  24. E[i]=(1+pow(x[i],2));
  25. F[i]=(3+pow(x[i],2));
  26. }
  27. {
  28. cout <<"\n";
  29.  
  30. for(i=1;i<7; i++)
  31. { printf("[%d][%d] A[%d]=%d\n", i,1, i, A[i]);
  32. }
  33. cout <<"\n";
  34. for(i=1;i<7; i++)
  35. {
  36. printf("[%d][%d] B[%d]=%d\n", i,2, i, B[i]);
  37. }
  38. cout <<"\n";
  39. for(i=1;i<7; i++)
  40. {
  41. printf("[%d][%d] C[%d]=%d\n", i,3, i, C[i]);
  42. }
  43. cout <<"\n";
  44. for(i=1;i<7; i++)
  45. {
  46. printf("[%d][%d] D[%d]=%d\n", i,4, i, D[i]);
  47.  
  48. }
  49. cout <<"\n";
  50. for(i=1;i<7; i++)
  51. {
  52. printf("[%d][%d] E[%d]=%d\n", i,5, i, E[i]);
  53. }
  54. cout <<"\n";
  55. for(i=1;i<7; i++)
  56. {
  57. printf("[%d][%d] F[%d]=%d\n", i,6, i, F[i]);
  58. }
  59. }
  60. printf("\n");
  61. printf ("A\tB\tC\tD\tE\tF");
  62. printf ("\n_\t_\t_\t_\t_\t_\t");
  63. for(i=1;i<7;i++)
  64. {
  65. cout<<endl;
  66. printf("%d\t", A[i]);
  67. printf("%d\t", B[i]);
  68. printf("%d\t", C[i]);
  69. printf("%d\t", D[i]);
  70. printf("%d\t", E[i]);
  71. printf("%d\t", F[i]);
  72. }
  73. cout <<"\n";
  74. // Compare the members
  75. int Minimum = A[0];
  76. for (int i=1; i<7; i++)
  77. if (minimum > A[i]) Minimum = A[i];
  78. return minimum;
  79. }
  80.  
  81. // Announce the result
  82. cout << "The minimum value of the array A is "<< A[minA] << "." << endl;
  83. }
  84. }
Last edited by Ancient Dragon : Nov 6th, 2007 at 9:26 pm. Reason: add code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2006
Location: Athens, Greece
Posts: 199
Reputation: n.aggel is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 9
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Junior Poster

Re: min value

  #2  
Nov 6th, 2007
please use code tags or other people won't be able to help you....

here is your code


  1. #include <iostream>
  2. #include <conio.h>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <cstdio>
  6. #include <ctime>
  7. using namespace std;
  8.  
  9. int i;
  10. int x[7];
  11. int A[7], B[7], C[7], D[7], E[7], F[7];
  12. int Min( const int *A, const int Count);
  13. int minimum, minA, minB, flagB;
  14. int main(void);
  15. int count = 7;
  16. {
  17. for(i=1;i<7;i++)
  18. {
  19. x[i]=i;
  20. A[i]=(2+pow(x[i],2));
  21. B[i]=(1+pow(x[i],3));
  22. C[i]=(2+pow(x[i],2)-1);
  23. D[i]=(3*pow(x[i],3));
  24. E[i]=(1+pow(x[i],2));
  25. F[i]=(3+pow(x[i],2));
  26. }
  27. {
  28. cout <<"\n";
  29.  
  30. for(i=1;i<7; i++)
  31. { printf("[%d][%d] A[%d]=%d\n", i,1, i, A[i]);
  32. }
  33. cout <<"\n";
  34. for(i=1;i<7; i++)
  35. {
  36. printf("[%d][%d] B[%d]=%d\n", i,2, i, B[i]);
  37. }
  38. cout <<"\n";
  39. for(i=1;i<7; i++)
  40. {
  41. printf("[%d][%d] C[%d]=%d\n", i,3, i, C[i]);
  42. }
  43. cout <<"\n";
  44. for(i=1;i<7; i++)
  45. {
  46. printf("[%d][%d] D[%d]=%d\n", i,4, i, D[i]);
  47.  
  48. }
  49. cout <<"\n";
  50. for(i=1;i<7; i++)
  51. {
  52. printf("[%d][%d] E[%d]=%d\n", i,5, i, E[i]);
  53. }
  54. cout <<"\n";
  55. for(i=1;i<7; i++)
  56. {
  57. printf("[%d][%d] F[%d]=%d\n", i,6, i, F[i]);
  58. }
  59. }
  60. printf("\n");
  61. printf ("A\tB\tC\tD\tE\tF");
  62. printf ("\n_\t_\t_\t_\t_\t_\t");
  63. for(i=1;i<7;i++)
  64. {
  65. cout<<endl;
  66. printf("%d\t", A[i]);
  67. printf("%d\t", B[i]);
  68. printf("%d\t", C[i]);
  69. printf("%d\t", D[i]);
  70. printf("%d\t", E[i]);
  71. printf("%d\t", F[i]);
  72. }
  73. cout <<"\n";
  74. // Compare the members
  75. int Minimum = A[0];
  76. for (int i=1; i<7; i++)
  77. if (minimum > A[i]) Minimum = A[i];
  78. return minimum;
  79. }
  80.  
  81. // Announce the result
  82. cout << "The minimum value of the array A is "<< A[minA] << "." << endl;
  83. }
  84. }

what compiler do you use? on gcc it doesn't compile....

remove line 2:: #include <conio.h>

also in lines like A[i]=(2+pow(x[i],2)); you must cast the operands to double, so that there is no amguity as to which overloaded function of pow the compiler selects..

to do this type:: A[i]=( 2+pow( static_cast<double>(x[i]),static_cast<double>(2) ) );
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote  
Join Date: Nov 2007
Posts: 219
Reputation: nurulshidanoni is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nurulshidanoni's Avatar
nurulshidanoni nurulshidanoni is offline Offline
Posting Whiz in Training

Re: min value

  #3  
Nov 6th, 2007
How to use code tags?
whata is complier?
I use visual basic c++ 6.0 edition, is that compiler?
Thanks for the correction, but why A[minA], doesnt produce the value that I want?

cout << "The minimum value of the array A is "<< A[minA] << "." << endl;
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
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 offline Offline
Most Valuable Poster

Re: min value

  #4  
Nov 6th, 2007
>>How to use code tags?
Look in the edit box. The instructions are in grey letters. All you have to do is read them.

>>whata is complier?
Too bad I can't issue an infraction for being such a dumbass. But I hope you were just teasing.

lines 20-25 of your original post: array x is initialized by the program's start-up code to be 0 because the array is in global memory. So 0 ^ <any number here> is always 0.
Last edited by Ancient Dragon : Nov 6th, 2007 at 9:30 pm.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Nov 2007
Posts: 219
Reputation: nurulshidanoni is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nurulshidanoni's Avatar
nurulshidanoni nurulshidanoni is offline Offline
Posting Whiz in Training

Re: min value

  #5  
Nov 6th, 2007
i have get the minimum value that I want, but how to get the min from which array?I have done in bold, but its seems like wrong

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <cstdio>
  5. #include <ctime>
  6. using namespace std;
  7.  
  8. int i;
  9. int x[7];
  10. int A[7], B[7], C[7], D[7], E[7], F[7];
  11. // The members of the array int minimum = numbers[0] ;
  12. int minimum, minA, minB, flagB;
  13. int main(void)
  14. {
  15. for(i=1;i<7;i++)
  16. {
  17. x[i]=i;
  18. A[i]=(2+pow(static_cast<double>(x[i]),static_cast<double>(2)));
  19. B[i]=(1+pow(static_cast<double>(x[i]),static_cast<double>(3)));
  20. C[i]=(2+pow(static_cast<double>(x[i]),static_cast<double>(2)-1));
  21. D[i]=(3*pow(static_cast<double>(x[i]),static_cast<double>(3)));
  22. E[i]=(1+pow(static_cast<double>(x[i]),static_cast<double>(2)));
  23. F[i]=(3+pow(static_cast<double>(x[i]),static_cast<double>(2)));
  24. }
  25. {
  26. cout <<"\n";
  27.  
  28. for(i=1;i<7; i++)
  29. { printf("[%d][%d] A[%d]=%d\n", i,1, i, A[i]);
  30. }
  31. cout <<"\n";
  32. for(i=1;i<7; i++)
  33. {
  34. printf("[%d][%d] B[%d]=%d\n", i,2, i, B[i]);
  35. }
  36. cout <<"\n";
  37. for(i=1;i<7; i++)
  38. {
  39. printf("[%d][%d] C[%d]=%d\n", i,3, i, C[i]);
  40. }
  41. cout <<"\n";
  42. for(i=1;i<7; i++)
  43. {
  44. printf("[%d][%d] D[%d]=%d\n", i,4, i, D[i]);
  45.  
  46. }
  47. cout <<"\n";
  48. for(i=1;i<7; i++)
  49. {
  50. printf("[%d][%d] E[%d]=%d\n", i,5, i, E[i]);
  51. }
  52. cout <<"\n";
  53. for(i=1;i<7; i++)
  54. {
  55. printf("[%d][%d] F[%d]=%d\n", i,6, i, F[i]);
  56. }
  57. }
  58. printf("\n");
  59. printf ("A\tB\tC\tD\tE\tF");
  60. printf ("\n_\t_\t_\t_\t_\t_\t");
  61. for(i=1;i<7;i++)
  62. {
  63. cout<<endl;
  64. printf("%d\t", A[i]);
  65. printf("%d\t", B[i]);
  66. printf("%d\t", C[i]);
  67. printf("%d\t", D[i]);
  68. printf("%d\t", E[i]);
  69. printf("%d\t", F[i]);
  70. }
  71. cout <<"\n";
  72. // Compare the members
  73. int minimum = A[1];
  74. for (i = 1; i < 7; ++i)
  75. {
  76. if ( A[i] < minimum)
  77. {
  78.  
  79. minimum=A[i];
  80. minA = i;
  81. return minimum;
  82.  
  83. } cout <<"\n";
  84. // Announce the result
  85. cout << "The minimum value of the array A is "<< minimum << "." << endl;
  86.  
  87. minimum = B[1];
  88. for (i = 1; i < 7; ++i)
  89. {
  90. if (B[i]< minimum)
  91. {
  92. minimum = B[i];
  93. minB = i;
  94. }
  95. cout <<"\n";
  96. cout << "The minimum value of the arrays B is "<< minimum << "." << endl;
  97.  
  98. // to determine the minimum whether in A or B
  99. minimum = A[minA];
  100. if(B[minB] < minimum)
  101. {
  102. minimum = B[minB];
  103. flagB = 1; //the minimum is in B if flagB=1
  104. }
  105. cout <<"\n";
  106. [b]if(flagB = 1)
  107. cout<< "Initial solution in A:" <<minimum << "."<<endl;
  108. else
  109. cout<< "Initial solution in B:" <<minimum << "."<<endl;
  110.  
  111. return 0;[/b]}
  112. }
  113. }
Last edited by Ancient Dragon : Nov 6th, 2007 at 10:49 pm. Reason: replace quote tags with code tags
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
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 offline Offline
Most Valuable Poster

Re: min value

  #6  
Nov 6th, 2007
did you compile that? Probably not because it has lots of errors. Start out by counting the open and close braced and make sure they match correctly. Next fix the indention -- its absolutely horrible. Don't be afraid to use the space bar to align the braces and code.

lines 18-23. What is the purpose of X array? Why do you even need it? Did you read what I previously posted about it? Obviously not because you didn't change it.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Nov 2007
Posts: 219
Reputation: nurulshidanoni is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nurulshidanoni's Avatar
nurulshidanoni nurulshidanoni is offline Offline
Posting Whiz in Training

Re: min value

  #7  
Nov 6th, 2007
What is the purpose of X array?
the pupose is to get the value.


[1][1] A[1]=3
[2][1] A[2]=6
[3][1] A[3]=11
[4][1] A[4]=18
[5][1] A[5]=27
[6][1] A[6]=38

[1][2] B[1]=2
[2][2] B[2]=9
[3][2] B[3]=28
[4][2] B[4]=65
[5][2] B[5]=126
[6][2] B[6]=217

[1][3] C[1]=3
[2][3] C[2]=4
[3][3] C[3]=5
[4][3] C[4]=6
[5][3] C[5]=7
[6][3] C[6]=8

[1][4] D[1]=3
[2][4] D[2]=24
[3][4] D[3]=81
[4][4] D[4]=192
[5][4] D[5]=375
[6][4] D[6]=648

[1][5] E[1]=2
[2][5] E[2]=5
[3][5] E[3]=10
[4][5] E[4]=17
[5][5] E[5]=26
[6][5] E[6]=37

[1][6] F[1]=4
[2][6] F[2]=7
[3][6] F[3]=12
[4][6] F[4]=19
[5][6] F[5]=28
[6][6] F[6]=39

A B C D E F
_ _ _ _ _ _
3 2 3 3 2 4
6 9 4 24 5 7
11 28 5 81 10 12
18 65 6 192 17 19
27 126 7 375 26 28
38 217 8 648 37 39

The minimum value of the array A is 3.

The minimum value of the arrays B is 2.

Initial solution in B: 0.
Press any key to continue


I have compile bit for the initial solution, why is it 0?
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
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 offline Offline
Most Valuable Poster

Re: min value

  #8  
Nov 6th, 2007
Originally Posted by nurulshidanoni View Post
What is the purpose of X array?
the pupose is to get the value.
To get the value of what? All elements of that array are initialized to 0 and you never change them.





Originally Posted by nurulshidanoni View Post
I have compile bit for the initial solution, why is it 0?


See above.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Nov 2007
Posts: 219
Reputation: nurulshidanoni is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nurulshidanoni's Avatar
nurulshidanoni nurulshidanoni is offline Offline
Posting Whiz in Training

Re: min value

  #9  
Nov 6th, 2007
Initialize to zero,? Dont you see that the array gives the value. Its a mathematic eqyuation, and i arrange in a matrix array.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
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 offline Offline
Most Valuable Poster

Re: min value

  #10  
Nov 6th, 2007
Yes I see now its initialized on line 19. But still, why do you need that array. You can just simply do this:
  1. for(i=1;i<7;i++)
  2. {
  3. A[i]=(2+pow(i, 2.0);
  4. B[i]=(1+pow(i,3.0);
  5. C[i]=(2+pow(i,1.0);
  6. D[i]=(3*pow(i,3.0);
  7. E[i]=(1+pow(I,2.0);
  8. F[i]=(3+pow(I,2.0);
  9. }

Also not that line 6 appears to be wrong -- using * instead of + like the other lines
Last edited by Ancient Dragon : Nov 6th, 2007 at 11:57 pm.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
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