I've tried doing it but it doesnt run
The array of real numbers is set.
a) To delete from the array positive elements.
b) To calculate the sum of negative elements in the array.
c) To calculate minimum of positive elements in the array.
solve using functions
ddanbe 2,724 Professional Procrastinator Featured Poster
I've tried doing it but it doesnt run
Why doesn't it run?
Show us code?
Ese 0 Newbie Poster
#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;
}
Ese 0 Newbie Poster
#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;
}
Ese 0 Newbie Poster
#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;
}
StuXYZ 731 Practically a Master Poster
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.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.