Not Yet Answered # Need help with a problem

Discussion Starter Ese Discussion Starter Ese Discussion Starter Ese StuXYZ 731

0

```
#include "stdafx.h"
#include <iostream>
using namespace std;
#define del_array
int _tmain(int argc, _TCHAR* argv[])
{
int z[40];
int i,imin,n;
float k,S,min;
cout<<"n="; cin>>n;
cout<< "please enter "<<n<<" intergers:" <<endl;
for(int i=0;i<n;i++)
{
cin>>z[i];
}
cout<< "\n values in array are now:";
for(i=0;i<n;i++)cout <<" "<<z[i];
cout <<endl;// for inputing of array
for(k=i=0;i<n;i++)
if(z[i]>0)
{k++;
if (k==1)
{min=z[i];imin=i;}
else
if (z[i]<min)
{min=z[i];imin=i;}}
cout<<"min="<<min<<"\n"<<endl;
{
int *z;int n,i;
cout<<"n=";cin>>n;
z=new int[n];
cout<<"Array z/n";
for(i=0;i<n;i++)
cin>>z[i];
for(i=0;i<n;)
if(z[i]>0);del_array(z,i&n)else i++;
if(z[i]>0) n=del_array(z,i,n)else i++;
cout<<"output array z\n";
for(i=0;i<n;i++)
cout<<z[i]<<"t";
}
return 0;
}
```

0

```
#include "stdafx.h"
#include <iostream>
using namespace std;
#define del_array
int _tmain(int argc, _TCHAR* argv[])
{
int z[40];
int i,imin,n;
float k,S,min;
cout<<"n="; cin>>n;
cout<< "please enter "<<n<<" intergers:" <<endl;
for(int i=0;i<n;i++)
{
cin>>z[i];
}
cout<< "\n values in array are now:";
for(i=0;i<n;i++)cout <<" "<<z[i];
cout <<endl;// for inputing of array
for(k=i=0;i<n;i++)
if(z[i]>0)
{k++;
if (k==1)
{min=z[i];imin=i;}
else
if (z[i]<min)
{min=z[i];imin=i;}}
cout<<"min="<<min<<"\n"<<endl;
{
int *z;int n,i;
cout<<"n=";cin>>n;
z=new int[n];
cout<<"Array z/n";
for(i=0;i<n;i++)
cin>>z[i];
for(i=0;i<n;)
if(z[i]>0);del_array(z,i&n)else i++;
if(z[i]>0) n=del_array(z,i,n)else i++;
cout<<"output array z\n";
for(i=0;i<n;i++)
cout<<z[i]<<"t";
}
return 0;
}
```

0

```
#include "stdafx.h"
#include <iostream>
using namespace std;
#define del_array
int _tmain(int argc, _TCHAR* argv[])
{
int z[40];
int i,imin,n;
float k,S,min;
cout<<"n="; cin>>n;
cout<< "please enter "<<n<<" intergers:" <<endl;
for(int i=0;i<n;i++)
{
cin>>z[i];
}
cout<< "\n values in array are now:";
for(i=0;i<n;i++)cout <<" "<<z[i];
cout <<endl;// for inputing of array
for(k=i=0;i<n;i++)
if(z[i]>0)
{k++;
if (k==1)
{min=z[i];imin=i;}
else
if (z[i]<min)
{min=z[i];imin=i;}}
cout<<"min="<<min<<"\n"<<endl;
{
int *z;int n,i;
cout<<"n=";cin>>n;
z=new int[n];
cout<<"Array z/n";
for(i=0;i<n;i++)
cin>>z[i];
for(i=0;i<n;)
if(z[i]>0);del_array(z,i&n)else i++;
if(z[i]>0) n=del_array(z,i,n)else i++;
cout<<"output array z\n";
for(i=0;i<n;i++)
cout<<z[i]<<"t";
}
return 0;
}
```

0

ok -- you start with some input and checking -- kind of ok -- Note that you have not checked if the user inputs that he wants 50 numbers... you program would break.

After than finding the min positive value: That is a bit of a mess of logic/coding etc. What you want to do is establish a start value, and then

continue. You seem to be doing that with a flag so lets keep that idea. But to help lets use variable names that mean something [always do that!]

```
int initialFlag(1); // set the inital flag to true
int minValue; // No need to initialize as min value controlled by flag
for(int i=0;i<n;i++)
{
// This if statement described below -- complex
if (z[i]>0 && (initialFlag==1 || minValue>z[i]))
{
minValue=z[i]; // store new best value
initialFlag=0; // We have a value
}
}
// some checks after the loop.
if (initalFlag==0) // every number was negative
{
std::cout<<"All values negative"<<std::endl;
}
else
{
std::cout<<"Min value = "<<minValue<<std::endl;
}
```

Look at the if statement very carefully! There is a lot going on there.

First off it has three sub-tests. The most important thing to realize with C++ (and many languages like it) is that logical expressions (things that evaluate to true/false) can be combined with boolean operators, the two commonest ones are && which means AND, and || which means OR.

So What happens with that line : The first test is 'is the nubmer positive'

Then there is a bracketed second test. Doesn't matter what is in that test

it can only return true/false. So for the overall condition to be true you need both parts to be true. Thus is z[i] is negative (or zero), no work to do, the condition is false as FALSE AND TRUE is FALSE.

So what if Z[i]>0. If that is the case then we have to get the bracket to be true. That is an OR between initialFlag being 1, and minValue being bigger than z[i]. With an OR, only one side needs to be true for it to be true. So either initialFlag==1 in which case doesn't matter about minvalue

[That nicely dealse with the case we haven't set minValue] or minValue is bigger than z[i], and we need to update it.

Note if we only have one positive number, then it is both the biggest positive number and the smallest!.

Coming to my last point (for now, sorry!) Note the special case check at the end. What if there are not positive values... Porgramming is normally 90% dealting with what are called 'edge cases'. Those unusual things, and this on one of them.

That should give you some ideas about how to do the rest. Cut the program down to do JUST one of the requirments. When you have that working add the next.

p.s. when you are getting a bit more confident -- try to go back and eliminate initialFlag, as the concept can be captured by using a negative minValue for example.

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Hi. so this is actually a continuation from another question of mineHere but i was advised to start a new thread as the original question was already answered.

This is the result of previous question answered :

code for the listbox - datagridview interaction

At the top of the code ...

the function that I created to find the ...

Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.

btn_addline :

`Private Sub btn_addline_Click(ByVal ...`