954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

min value

hello,
why the output below produce number 0, eventhought there is no 0 in the array?

#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <ctime>
using namespace std;

int i;
int x[7];
int A[7], B[7], C[7], D[7], E[7], F[7];
int Min( const int *A, const int Count);
int minimum, minA, minB, flagB;
int main(void);
int count = 7;
{
    for(i=1;i<7;i++)
{
       x[i]=i;
       A[i]=(2+pow(x[i],2));
       B[i]=(1+pow(x[i],3));
       C[i]=(2+pow(x[i],2)-1);
       D[i]=(3*pow(x[i],3));
       E[i]=(1+pow(x[i],2));
       F[i]=(3+pow(x[i],2));
}
{
       cout <<"\n";

       for(i=1;i<7; i++)
{      printf("[%d][%d] A[%d]=%d\n", i,1, i, A[i]);
}
       cout <<"\n";
       for(i=1;i<7; i++)
{
       printf("[%d][%d] B[%d]=%d\n", i,2, i, B[i]);
}
       cout <<"\n";
       for(i=1;i<7; i++)
{
       printf("[%d][%d] C[%d]=%d\n", i,3, i, C[i]);
}
       cout <<"\n";
       for(i=1;i<7; i++)
{
       printf("[%d][%d] D[%d]=%d\n", i,4, i, D[i]);

}
       cout <<"\n";
       for(i=1;i<7; i++)
{
       printf("[%d][%d] E[%d]=%d\n", i,5, i, E[i]);
}
       cout <<"\n";
       for(i=1;i<7; i++)
{
       printf("[%d][%d] F[%d]=%d\n", i,6, i, F[i]);
}
}
       printf("\n");
       printf ("A\tB\tC\tD\tE\tF");
       printf ("\n_\t_\t_\t_\t_\t_\t");
       for(i=1;i<7;i++)
{
       cout<<endl;
       printf("%d\t", A[i]);
       printf("%d\t", B[i]);
       printf("%d\t", C[i]);
       printf("%d\t", D[i]);
       printf("%d\t", E[i]);
       printf("%d\t", F[i]);
}
       cout <<"\n";
    // Compare the members
       int Minimum = A[0];
	   for (int i=1; i<7; i++)
		   if (minimum > A[i]) Minimum = A[i];
		   return minimum;
}

    // Announce the result
       cout << "The minimum value of the array A is "<< A[minA] << "." <<   endl;
}
	   }
nurulshidanoni
Posting Whiz in Training
219 posts since Nov 2007
Reputation Points: 9
Solved Threads: 0
 

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

here is your code

#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <ctime>
using namespace std;

int i;
int x[7];
int A[7], B[7], C[7], D[7], E[7], F[7];
int Min( const int *A, const int Count);
int minimum, minA, minB, flagB;
int main(void);
int count = 7;
{
    for(i=1;i<7;i++)
{
       x[i]=i;
       A[i]=(2+pow(x[i],2));
       B[i]=(1+pow(x[i],3));
       C[i]=(2+pow(x[i],2)-1);
       D[i]=(3*pow(x[i],3));
       E[i]=(1+pow(x[i],2));
       F[i]=(3+pow(x[i],2));
}
{
       cout <<"\n";

       for(i=1;i<7; i++)
{      printf("[%d][%d] A[%d]=%d\n", i,1, i, A[i]);
}
       cout <<"\n";
       for(i=1;i<7; i++)
{
       printf("[%d][%d] B[%d]=%d\n", i,2, i, B[i]);
}
       cout <<"\n";
       for(i=1;i<7; i++)
{
       printf("[%d][%d] C[%d]=%d\n", i,3, i, C[i]);
}
       cout <<"\n";
       for(i=1;i<7; i++)
{
       printf("[%d][%d] D[%d]=%d\n", i,4, i, D[i]);

}
       cout <<"\n";
       for(i=1;i<7; i++)
{
       printf("[%d][%d] E[%d]=%d\n", i,5, i, E[i]);
}
       cout <<"\n";
       for(i=1;i<7; i++)
{
       printf("[%d][%d] F[%d]=%d\n", i,6, i, F[i]);
}
}
       printf("\n");
       printf ("A\tB\tC\tD\tE\tF");
       printf ("\n_\t_\t_\t_\t_\t_\t");
       for(i=1;i<7;i++)
{
       cout<<endl;
       printf("%d\t", A[i]);
       printf("%d\t", B[i]);
       printf("%d\t", C[i]);
       printf("%d\t", D[i]);
       printf("%d\t", E[i]);
       printf("%d\t", F[i]);
}
       cout <<"\n";
    // Compare the members
       int Minimum = A[0];
	   for (int i=1; i<7; i++)
		   if (minimum > A[i]) Minimum = A[i];
		   return minimum;
}

    // Announce the result
       cout << "The minimum value of the array A is "<< A[minA] << "." <<   endl;
}
	   }


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

remove line 2:: #include

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) ) );

n.aggel
Posting Whiz in Training
203 posts since Nov 2006
Reputation Points: 23
Solved Threads: 12
 

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;

nurulshidanoni
Posting Whiz in Training
219 posts since Nov 2007
Reputation Points: 9
Solved Threads: 0
 

>>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 ^ is always 0.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <ctime>
using namespace std;

int i;
int x[7];
int A[7], B[7], C[7], D[7], E[7], F[7];
// The members of the array int minimum = numbers[0] ;
int minimum, minA, minB, flagB;
int main(void)
{
    for(i=1;i<7;i++)
{
       x[i]=i;
       A[i]=(2+pow(static_cast<double>(x[i]),static_cast<double>(2)));
       B[i]=(1+pow(static_cast<double>(x[i]),static_cast<double>(3)));
       C[i]=(2+pow(static_cast<double>(x[i]),static_cast<double>(2)-1));
       D[i]=(3*pow(static_cast<double>(x[i]),static_cast<double>(3)));
       E[i]=(1+pow(static_cast<double>(x[i]),static_cast<double>(2)));
       F[i]=(3+pow(static_cast<double>(x[i]),static_cast<double>(2)));
}
{
       cout <<"\n";

       for(i=1;i<7; i++)
{      printf("[%d][%d] A[%d]=%d\n", i,1, i, A[i]);
}
       cout <<"\n";
       for(i=1;i<7; i++)
{
       printf("[%d][%d] B[%d]=%d\n", i,2, i, B[i]);
}
       cout <<"\n";
       for(i=1;i<7; i++)
{
       printf("[%d][%d] C[%d]=%d\n", i,3, i, C[i]);
}
       cout <<"\n";
       for(i=1;i<7; i++)
{
       printf("[%d][%d] D[%d]=%d\n", i,4, i, D[i]);

}
       cout <<"\n";
       for(i=1;i<7; i++)
{
       printf("[%d][%d] E[%d]=%d\n", i,5, i, E[i]);
}
       cout <<"\n";
       for(i=1;i<7; i++)
{
       printf("[%d][%d] F[%d]=%d\n", i,6, i, F[i]);
}
}
       printf("\n");
       printf ("A\tB\tC\tD\tE\tF");
       printf ("\n_\t_\t_\t_\t_\t_\t");
       for(i=1;i<7;i++)
{
       cout<<endl;
       printf("%d\t", A[i]);
       printf("%d\t", B[i]);
       printf("%d\t", C[i]);
       printf("%d\t", D[i]);
       printf("%d\t", E[i]);
       printf("%d\t", F[i]);
}
       cout <<"\n";
    // Compare the members
       int minimum = A[1];
       for (i = 1; i < 7; ++i)
{
       if ( A[i] < minimum) 
{
    
	   minimum=A[i];
	   minA = i;
	   return minimum;

}      cout <<"\n";
    // Announce the result
       cout << "The minimum value of the array A is "<< minimum << "." <<   endl;

       minimum = B[1];
       for (i = 1; i < 7; ++i)
{
       if (B[i]< minimum)
{
		minimum = B[i];
    	minB = i; 
}
        cout <<"\n";
        cout << "The minimum value of the arrays B is "<< minimum << "." << endl;
 
   // to determine the minimum whether in A or B
	    minimum = A[minA];
	    if(B[minB] < minimum)
{
		minimum = B[minB];
		flagB = 1; //the minimum is in B if flagB=1
}
    	cout <<"\n";
        <strong>if(flagB = 1)
		cout<< "Initial solution in A:" <<minimum << "."<<endl;
	    else 
		cout<< "Initial solution in B:" <<minimum << "."<<endl;
	   
        return 0;</strong>}
}
}
nurulshidanoni
Posting Whiz in Training
219 posts since Nov 2007
Reputation Points: 9
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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?

nurulshidanoni
Posting Whiz in Training
219 posts since Nov 2007
Reputation Points: 9
Solved Threads: 0
 
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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Initialize to zero,? Dont you see that the array gives the value. Its a mathematic eqyuation, and i arrange in a matrix array.

nurulshidanoni
Posting Whiz in Training
219 posts since Nov 2007
Reputation Points: 9
Solved Threads: 0
 

Yes I see now its initialized on line 19. But still, why do you need that array. You can just simply do this:

for(i=1;i<7;i++)
{
       A[i]=(2+pow(i, 2.0);
       B[i]=(1+pow(i,3.0);
       C[i]=(2+pow(i,1.0);
       D[i]=(3*pow(i,3.0);
       E[i]=(1+pow(I,2.0);
       F[i]=(3+pow(I,2.0);
}


Also not that line 6 appears to be wrong -- using * instead of + like the other lines

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This is what nagel said...so thats why I do like that.
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(x[i]),static_cast(2) ) );

nurulshidanoni
Posting Whiz in Training
219 posts since Nov 2007
Reputation Points: 9
Solved Threads: 0
 

Since X is never used anywhere else in your program you can remove it like I posted. Its up to you whether you want to use static cast or not. In some cases you might have to, but for literals you can just add .0 to the end, such as 2.0 instead of just 2.

A[i]=( 2+pow( static_cast<double>(i), 2.0 ));

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

If I have get the initial solution in array B which is 2, then how tu move to the next step. Ignore array B, and look the min value in array A, C,D, E, F.

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

nurulshidanoni
Posting Whiz in Training
219 posts since Nov 2007
Reputation Points: 9
Solved Threads: 0
 

why must put a 2.0? what is different if we put 2 only?

nurulshidanoni
Posting Whiz in Training
219 posts since Nov 2007
Reputation Points: 9
Solved Threads: 0
 

2 is an integer, 2.0 is a double, 2.0F is a float

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

To simplify your code you should probably write a function that returns the minimum value of the array that you pass it. Then you can pass it each of the arrays and print out the min for each. You only have to write the algorithm once instead of 6 times.

int min(int array[], int size)
{
   // your code here

}

int main()
{
    int A[7], ...

    cout << "min of array A is " << min(A,7) << "\n";
    cout << "min of array B is " << min(B,7) << "\n";
    ... // etc. etc.
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I try to do like you said, but error.

ompiling...
151.cpp
error C2447: missing function header (old-style formal list?)
Error executing cl.exe.

151.exe - 1 error(s), 0 warning(s)

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <ctime>
using namespace std;

int i;
int x[7];
int A[7], B[7], C[7], D[7], E[7], F[7];
// The members of the array int minimum = numbers[0] ;
int minimum, minA, minB, flagB;
int min(int array[], int size);
int main(void);

{
    for(i=1;i<7;i++)
{
       x[i]=i;
       A[i]=(2+pow(i,2));
       B[i]=(1+pow(i,3));
       C[i]=(2*pow(i,2)-1);
       D[i]=(3*pow(i,3));
       E[i]=(1+pow(i,2));
       F[i]=(3+pow(i,2));
}
   printf("\n");
       printf ("A\tB\tC\tD\tE\tF");
       printf ("\n_\t_\t_\t_\t_\t_\t");
       for(i=1;i<7;i++)
{
       cout<<endl;
       printf("%d\t", A[i]);
       printf("%d\t", B[i]);
       printf("%d\t", C[i]);
       printf("%d\t", D[i]);
       printf("%d\t", E[i]);
       printf("%d\t", F[i]);

       

    // Announce the result
       cout << "Min of array A is "<< min(A,7) << "." <<   endl;

      
}
}
nurulshidanoni
Posting Whiz in Training
219 posts since Nov 2007
Reputation Points: 9
Solved Threads: 0
 

i A B C D E F
1
2
3
4
5
6

Giben above, how to get the summation of A, B, C, D, E,F is equal to 10?
From each array, must chose only.

nurulshidanoni
Posting Whiz in Training
219 posts since Nov 2007
Reputation Points: 9
Solved Threads: 0
 
error C2447: missing function header (old-style formal list?) Error executing cl.exe.



Well where is your min() function ? I see the defenition but where's the rest? Did you ever use functions before?

Niek

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

ont udesrtand, min function()?

nurulshidanoni
Posting Whiz in Training
219 posts since Nov 2007
Reputation Points: 9
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You