I wrote this code and I want it to find if all number were positive or negative or mixture? + I want to find the largest and smallest, finding the largest number works fine but finding the smallest is not working. Could you help me using if statement?
#include <iostream>
usingnamespace std;
void main()
{
float num,
average,
sum,
largest,
smallest,
positive,
negative;
int count;
sum = 0;
count = 1;
negative = 0;
largest= 0;
smallest = 0;
while ( count < 11)
{
cout << "Number #" << count << ": ";
cin >> num;
if ( num < 0 )negative++;
sum += num;
if ( count != 0 )
{
if ( num > largest )
largest = num;
elseif ( num < smallest )
smallest = num;
}
count++;
}
average = ( sum / 10 );
cout << endl << "The smallest number is " << smallest << endl;
cout << "The largest number is " << largest << endl;
cout << "The average of all numbers is " << average << endl;
cout << "The sum of all numbers is " << sum << endl;
cout << endl;
cout << "There are " << negative << " negative numbers." << endl;
if ( negative == 0 )
cout << "All numbers are positive.";
elseif ( negative == 10 )
cout << "All numbers are negative.";
else
{
cout << "There is a mixture of numbers.";
}
}
1. Your code doesn't compile
2. You default small to 0. So you have to enter a number smaller than zero before it considers your number the smallest. Default it to something high like 1e20
#include <iostream>
usingnamespace std;
void main()
{
float num,
average,
sum,
largest,
smallest,
positive,
negative;
int count;
sum = 0;
count = 1;
negative = 0;
largest = 0;
smallest = 0;
while ( count < 11)
{
cout << "Number #" << count << ": ";
cin >> num;
if ( num < 0 )negative++;
sum += num;
if ( count == 1 )
{
if ( num > largest )
largest = num;
if ( num < smallest )
smallest = num;
}
count++;
}
average = ( sum / 10 );
cout << endl << "The smallest number is " << smallest << endl;
cout << "The largest number is " << largest << endl;
cout << "The average of all numbers is " << average << endl;
cout << "The sum of all numbers is " << sum << endl;
cout << endl;
cout << "There are " << negative << " negative numbers." << endl;
if ( negative == 0 )
cout << "All numbers are positive.";
elseif ( negative == 10 )
cout << "All numbers are negative.";
else
{
cout << "There is a mixture of numbers.";
}
}
#include<iostream>
using std::cout;
using std::cin;
using std::fixed;
using std::endl;
#include<iomanip>
using std::setprecision;
int main(){
int
countNumbers=1,
number,
smallest,
largest,
negative=0,
positive=0;
double
average,
sum=0;
bool
allPositives=false,
allNegatives=false,
mixedNumbers=false;
while(countNumbers<=10){
cout<<"Enter number #"<<countNumbers<<" ";
cin>>number;
if(countNumbers==1){
smallest=number;
largest=number;
}else{
if(number>largest)
largest=number;
if(number<smallest)
smallest=number;
}//end if...else
if(number<0)
negative++;
else
positive++;
sum+=number;
countNumbers++;
}//end while
average=sum/10;
if(negative>=1 && positive>=1)
mixedNumbers=true;
if(negative>=1 && positive==0)
allNegatives=true;
if(negative==0 && positive>=1)
allPositives=true;
cout<<fixed<<setprecision(2)<<"The largest number is: "<<largest
<<"\nThe smallest number is: "<<smallest
<<"\nThe sum is: "<<sum
<<"\nThe average is: "<<average;
if(mixedNumbers==true)
cout<<"\nThere are "<<negative<<" negative numbers and "
<<positive<<" positive numbers"<<endl;
else{
if(allNegatives==true)
cout<<"\nThere are "<<negative<<" negative numbers"
<<endl;
else
cout<<"\nThere are "<<positive<<" positive numbers"<<endl;
}//end if...else
return 0; //indicates successful termination
}//end main
Juan-Enrique-Hernandez-Perezs-MacBook-Pro:test Enrique$ ./hw_testNumbers
Enter number #1 -1
Enter number #2 -2
Enter number #3 -3
Enter number #4 -4
Enter number #5 -5
Enter number #6 -6
Enter number #7 -7
Enter number #8 -8
Enter number #9 -9
Enter number #10 -10
The largest number is: -1
The smallest number is: -10
The sum is: -55.00
The average is: -5.50
There are 10 negative numbers
Enter number #1 1
Enter number #2 2
Enter number #3 3
Enter number #4 4
Enter number #5 5
Enter number #6 6
Enter number #7 7
Enter number #8 8
Enter number #9 9
Enter number #10 10
The largest number is: 10
The smallest number is: 1
The sum is: 55.00
The average is: 5.50
There are 10 positive numbers
Enter number #1 -2
Enter number #2 -1
Enter number #3 3
Enter number #4 4
Enter number #5 5
Enter number #6 6
Enter number #7 7
Enter number #8 8
Enter number #9 9
Enter number #10 10
The largest number is: 10
The smallest number is: -2
The sum is: 49.00
The average is: 4.90
There are 2 negative numbers and 8 positive numbers