944,123 Members | Top Members by Rank

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

min value

Expand Post »
hello,
why the output below produce number 0, eventhought there is no 0 in the array?
c++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 9
Solved Threads: 0
Posting Whiz in Training
nurulshidanoni is offline Offline
219 posts
since Nov 2007
Nov 6th, 2007
0

Re: min value

please use code tags or other people won't be able to help you....

here is your code


c++ Syntax (Toggle Plain Text)
  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) ) );
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
n.aggel is offline Offline
202 posts
since Nov 2006
Nov 6th, 2007
0

Re: min value

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;
Reputation Points: 9
Solved Threads: 0
Posting Whiz in Training
nurulshidanoni is offline Offline
219 posts
since Nov 2007
Nov 6th, 2007
0

Re: min value

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Nov 6th, 2007
0

Re: min value

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

c++ Syntax (Toggle Plain Text)
  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
Reputation Points: 9
Solved Threads: 0
Posting Whiz in Training
nurulshidanoni is offline Offline
219 posts
since Nov 2007
Nov 6th, 2007
0

Re: min value

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Nov 6th, 2007
0

Re: min value

What is the purpose of X array?
the pupose is to get the value.


Quote ...
[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?
Reputation Points: 9
Solved Threads: 0
Posting Whiz in Training
nurulshidanoni is offline Offline
219 posts
since Nov 2007
Nov 6th, 2007
0

Re: min value

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.





I have compile bit for the initial solution, why is it 0?
See above.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Nov 6th, 2007
0

Re: min value

Initialize to zero,? Dont you see that the array gives the value. Its a mathematic eqyuation, and i arrange in a matrix array.
Reputation Points: 9
Solved Threads: 0
Posting Whiz in Training
nurulshidanoni is offline Offline
219 posts
since Nov 2007
Nov 6th, 2007
0

Re: min value

Yes I see now its initialized on line 19. But still, why do you need that array. You can just simply do this:
c++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005

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 error checking my Line editor functions
Next Thread in C++ Forum Timeline: The difference of '\n' in windows and linux.





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


Follow us on Twitter


© 2011 DaniWeb® LLC