Extracting the minimum value from a file using a while loop c++ - c++

I am required to set the minimum value (minValue) equal to -1 in my declarations. How do I, using the || (or condition) within my while loop, extract a minimum value from a positive set of numbers considering that a negative value concludes the program? Also, the double average variable (average), is not calculating the intended value. For example, if the value is supposed to be 35.7789, it is returning 35.0000. I am also intending for the average value to return with no decimal point when unnecessary. For example, if the user enters a negative value first, the average value should be 0 as opposed to 0.0000. How do I manipulate the average variable in order to do so?
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int intVal;
int sum = 0;
int maxValue = -1;
int minValue = -1;
double average = static_cast<double>(0);
int count = 0;
int evenCount = 0;
int oddCount = 0;
cout << endl << "Enter an integer (negative value to Quit): ";
cin >> intVal;
cout << endl;
while(intVal >= 0)
{
if(intVal > maxValue)
maxValue = intVal;
if(intVal < minValue)
minValue = intVal;
count ++;
sum += intVal;
if(intVal > 0)
average = sum / count;
if(intVal % 2 == 0)
evenCount ++;
else
oddCount ++;
cout << "Enter an integer (negative value to Quit): ";
cin >> intVal;
cout << endl;
}
cout << fixed << setprecision(4);
cout << "Values entered:" << setw(8) << right << count << endl;
cout << "Sum of numbers:" << setw(8) << right << sum << endl;
cout << " Average value:" << setw(8) << right << average << endl;
cout << " Maximum value:" << setw(8) << right << maxValue << endl;
cout << " Minimum value:" << setw(8) << right << minValue << endl;
cout << " Even numbers:" << setw(8) << right << evenCount << endl;
cout << " Odd numbers:" << setw(8) << right << oddCount << endl;
cout << endl;
}

I am required to set the minimum value (minValue) equal to -1 in my declarations.
With initialization to -1, all positive numbers will be greater than minValue.
You can set/initialize it with max value or first input value, or handling the special case in the loop:
if (minValue == -1 || intVal < minValue)
minValue = intVal;
Also, the double average variable (average), is not calculating the intended value.
Your average does integer arithmetic, you have to use floating point during the computation:
average = static_cast<double>(sum) / count;
the average value should be 0 as opposed to 0.0000
Remove the call to std::setprecision(4);

Related

Why isn't my average working properly? C++ [duplicate]

This question already has an answer here:
Integer division always zero [duplicate]
(1 answer)
Closed 2 years ago.
it seems that I'm stuck at c++ yet again. woohoo.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int amount = 0;
int inputNumber = 0;
int temp = 0;
int n = 0;
int lowest;
int highest;
double averages;
cout << "Welcome to simple calculator. Where you can use for average, highest amd lowest value.";
cout << "Please input all the number you plan to use to calculate. ";
cout << "Finish your input by enter any letter and then press on enter. " << endl;
while(cin >> inputNumber)
{
if (inputNumber < lowest) {
int (lowest = inputNumber);
}
else if(inputNumber > highest){
int (highest = inputNumber);
}
amount = amount += inputNumber;
n++;
}
double averages = amount / n;
cout << "Your lowest value is: " << lowest << endl;
cout << "Your highest value is: " << highest << endl;
cout << "Your average value is: " << averages << endl;
cout << "Amount is " << amount << endl;
cout << n << endl;
return 0;
}
but when I put in numbers it doesn't work properly. like
3
5
d
Your lowest value is: 3
Your Highest value is: 5
Your average value is: 4
2
-2
-3
a
Your lowest value is: -3
Your Highest value is: 0
Your average value is: -2
2
Idk why it do this and I'm still new to C++, I also have to code it in Linux and use G++
Thanks in advance if you could help me out.
try average = double(amount)/double(n);
Because average is double and n and amount
There are few errors in your code:
variable averages is defined twice.
Since for calculating amount you are doing int/int. which in result will yield int.
Thus your answer for -3 2 d will come wrong.
#include<iostream>
using namespace std;
int main()
{
int amount = 0;
int inputNumber = 0;
int temp = 0;
int n = 0;
int lowest=INT_MAX;
int highest=INT_MIN;
cout << "Welcome to simple calculator. Where you can use for average, highest amd lowest value.";
cout << "Please input all the number you plan to use to calculate. ";
cout << "Finish your input by enter any letter and then press on enter. " << endl;
while(cin >> inputNumber)
{
if (inputNumber < lowest) {
int (lowest = inputNumber);
}
else if(inputNumber > highest){
int (highest = inputNumber);
}
amount += inputNumber;
n++;
}
double averages = (double)amount / n;
cout << "Your lowest value is: " << lowest << endl;
cout << "Your highest value is: " << highest << endl;
cout << "Your average value is: " << averages << endl;
cout << "Amount is " << amount << endl;
cout << n << endl;
return 0;
}
This above code is correct code

how to exclude the SENTINEL value from the output in c++?

how do you exclude the SENTINEL value from the output in c++?
For a school project.
The SENTINEL value is skewing the output and adding it to the calculations. My professor was vague on how to exclude the SENTINEL from my calculations, but I am still confused as to how to exclude it.
My output for the largest value keeps equaling 99999 which is the SENTINEL value.
It is also adding the SENTINEL value to all the calculations.
#include "stdafx.h"
#include <iostream>
#include <climits>
using namespace std;
int SENTINEL = 99999;
int main()
{
int min = INT_MAX, max = INT_MIN, num = 0, counter = 0, pos = 0, neg = 0, sum = 0, num2 = 0, tot = 0;
int even = 0;
int odd = 0;
double total = 0;
while (num != SENTINEL)
{
cout << "Enter in a number" << endl;
cout << "Enter 99999 to exit" << endl;
cin >> num;
if (num > max)
max = num;
if (num < min)
min = num;
if ((num % 2) != 0)
{
odd++;
}
else if (num % 2 == 0)
{
even++;
}
if (num < 99999) {
neg++;
}
if (num < 99) {
num2++;
}
total += num;
tot += num;
counter++;
}
total = total / counter;
cout << "The smallest Number of the list is: " << min << endl;
cout << "The largest Number of the list is: " << max << endl;
cout << "The sum of all the numbers is: " << tot << endl;
cout << "The average of all numbers is: " << total << endl;
cout << "There are " << neg << " numbers" << endl;
cout << "There are " << even << " even numbers" << endl;
cout << "There are " << odd << " odd numbers" << endl;
cout << "There are " << num2 << " numbers smaller than 99" << endl;
return 0;
}
There are some changes you will have to make:
Read the number at least once before the while loop to ensure that the while
is not executed if number is 99999.
Move the reading of the number to the end of the while loop so that it can be checked right at the beginning before any further
processing.
Have a check to see if any numbers have to been entered to avoid dividing by zero in the statement total = total / counter;
The various statements must be output using cout only if there are any numbers in the list. Otherwise, it does not make much sense.
After the above changes, the code will look like this:
#include <iostream>
#include <climits>
using namespace std;
int SENTINEL = 99999;
int main()
{
int min = INT_MAX, max = INT_MIN, num = 0, counter = 0, pos = 0, neg = 0, sum = 0, num2 = 0, tot = 0;
int even = 0;
int odd = 0;
double total = 0;
cout << "Enter in a number" << endl;
cout << "Enter 99999 to exit" << endl;
cin >> num;
while (num != SENTINEL)
{
if (num > max)
max = num;
if (num < min)
min = num;
if ((num % 2) != 0)
{
odd++;
}
else if (num % 2 == 0)
{
even++;
}
if (num < 99999) {
neg++;
}
if (num < 99) {
num2++;
}
total += num;
tot += num;
counter++;
cout << "Enter in a number" << endl;
cout << "Enter 99999 to exit" << endl;
cin >> num;
}
if(counter != 0)
{
total = total / counter;
cout << "The smallest Number of the list is: " << min << endl;
cout << "The largest Number of the list is: " << max << endl;
cout << "The sum of all the numbers is: " << tot << endl;
cout << "The average of all numbers is: " << total << endl;
cout << "There are " << neg << " numbers" << endl;
cout << "There are " << even << " even numbers" << endl;
cout << "There are " << odd << " odd numbers" << endl;
cout << "There are " << num2 << " numbers smaller than 99" << endl;
}
else
{
cout << "No numbers were entered" << endl;
}
return 0;
}
Sample Output:
./a.exe
Enter in a number
Enter 99999 to exit
99999
No numbers were entered
./a.exe
Enter in a number
Enter 99999 to exit
34
Enter in a number
Enter 99999 to exit
23
Enter in a number
Enter 99999 to exit
12
Enter in a number
Enter 99999 to exit
99999
The smallest Number of the list is: 12
The largest Number of the list is: 34
The sum of all the numbers is: 69
The average of all numbers is: 23
There are 3 numbers
There are 2 even numbers
There are 1 odd numbers
There are 3 numbers smaller than 99

How do I output the smallest value entered into an array? (c++)

How do I find the smallest value in an array? I think I'm doing it right, but it outputs zero when I run the program.
I did it the same way in another program and it worked. When this runs, the highest element displays, but the lowest displays as zero.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int ARRAY_SIZE = 12;
double rainfall[ARRAY_SIZE];
double total_year, monthly_average;
double highest = rainfall[0];
double lowest = rainfall[0];
cout << " Input rainfall for each month: \n" ;
for(int index = 0; index < ARRAY_SIZE; index++)
{
cout << " Month " << index+1 << ": " ;
cin >> rainfall[index];
total_year += rainfall[index];
if(rainfall[index] < 0)
{
cout << " Rainfall must equal to 0 or higher: " ;
cin >> rainfall[index];
}
}
for(int x = 0; x < ARRAY_SIZE; x++)
{
if(highest < rainfall[x])
{
highest = rainfall[x];
}
if(lowest > rainfall[x])
{
lowest = rainfall[x];
}
}
cout << fixed << setprecision(2) << endl;
cout << " There was " << total_year << " inches" ;
cout << " of rainfall this year. \n" ;
cout << " The monthtly average was " << total_year / 12 ;
cout << " inches of rainfall.\n";
cout << " The highest rainfall was " << highest << " inches" << endl;
cout << " The lowest rainfall was " << lowest << " inches" << endl;
return 0;
}
Try to declare variables where they are used. Otherwise the code will be less readable.
The array rainfall is not yet initialized
double rainfall[ARRAY_SIZE];
//...
double highest = rainfall[0];
double lowest = rainfall[0];
So using its elements with indeterminate values for the variables highest and lowest does not make sense.
Declare and initialize the variables just before the loop where they are calculated.
double highest = rainfall[0];
double lowest = rainfall[0];
for(int x = 0; x < ARRAY_SIZE; x++)
{
if(highest < rainfall[x])
{
highest = rainfall[x];
}
if(lowest > rainfall[x])
{
lowest = rainfall[x];
}
}
In this loop
for(int index = 0; index < ARRAY_SIZE; index++)
{
cout << " Month " << index+1 << ": " ;
cin >> rainfall[index];
total_year += rainfall[index];
if(rainfall[index] < 0)
{
cout << " Rainfall must equal to 0 or higher: " ;
cin >> rainfall[index];
}
}
move the statement
total_year += rainfall[index];
after the if statement.
for(int index = 0; index < ARRAY_SIZE; index++)
{
cout << " Month " << index+1 << ": " ;
cin >> rainfall[index];
if(rainfall[index] < 0)
{
cout << " Rainfall must equal to 0 or higher: " ;
cin >> rainfall[index];
}
total_year += rainfall[index];
}
I would substitute the if statement for a while statement like
while (rainfall[index] < 0)
{
cout << " Rainfall must equal to 0 or higher: " ;
cin >> rainfall[index];
}
but before using the variable total_year you have to initialize it
double total_year = 0.0;
The variable monthly_average is not used in the code. So its declaration can be removed.
Take into account that there are the following algorithms in C++ std::min_element, std::max_element, std::minmax_element that can be used to find minimum and maximum alements in an array or other container.

Highest and Lowest Integer C++

when I run this program is a "Run-Time Check Failure #2 stack around the variable 'numGrades' was corrupted" appears. Also the lowest grade doesn't output the correct answer. Any help would be greatly appreciated!
#include <iostream> // cin, cout
using namespace std;
const int TEN_GRADES = 10; // pre-defined number of grades
int main()
{
int numGrades[10];
double avg, highest = numGrades[0], lowest = numGrades[0], less, greater, grades;
double sum = 0;
// greeting message
cout << "---------------------------------" << endl
<< " Sandro's Statistics Generator " << endl
<< "---------------------------------" << endl << endl;
// requesting number of grades
cout << "Hello Professor, how many grades do I need to analyse this time? ";
cin >> numGrades[10];
if (numGrades[1] == 0)
{
cout << "\nGuess you changed your mind!!!" << endl
<< "Ending program now..." << endl << endl;
return 0;
}
// if user doesn't enter 0 user is ready to begin
cout << "Okay, I am ready. Start..." << endl;
for (int count = 0; count < numGrades[10]; count++)
{
cin >> numGrades[count];
sum += numGrades[10];
}
// to get the average
avg = sum / TEN_GRADES;
// to get the highest and lowest mark
for (int count = 0; count < TEN_GRADES; count++)
{
if (numGrades[count] > highest)
highest = numGrades[count];
}
for (int count = 0; count < TEN_GRADES; count++)
{
if (numGrades[count] < lowest)
lowest = numGrades[count];
}
// output requested statistics
cout << "Here are the requested stats for the " << numGrades << " grades." << endl
<< "The class average is " << avg << endl
<< "The highest grade is " << highest << endl
<< "The lowest grade is " << lowest << endl;
return 0;
}
Oh god, I don't even do c++ but I think one of my eyes bled a little.
Please review (or tell whoever coded this to review) how to create and assign values to them.
Then review simple data structures (like arrays) and loops.
One good way to start is to analyze the following WORKING code of your program:
Please mark as correct if it helps, and if you have any questions... Cheers!
#include <iostream>
#include <string>
using namespace std;
const int TEN_GRADES = 10; // pre-defined number of grades
int main()
{
int numGrades[10];
double avg, highest = 0, lowest = 0, less, greater, grades;
double sum = 0;
// greeting message
cout << "---------------------------------" << endl
<< " Newbie Statistics Generator " << endl
<< "---------------------------------" << endl << endl;
// requesting number of grades
cout << "Hello Professor, please enter 10 grades: "<<endl;
//THIS PART: loops ten times to input the grades
for (int count = 0; count < TEN_GRADES; count ++)
{
cout << "Grade number "<<count<<":";
cin >> numGrades[count];
}
//I get what you want to do here, but consider adding another exit condition here, what if the second grade is really 0 ?
if (numGrades[1] == 0)
{
cout << "\nGuess you changed your mind!!!" << endl
<< "Ending program now..." << endl << endl;
return 0;
}
// if user doesn't enter 0 user is ready to begin
cout << "Okay, I am ready. Start..." << endl;
for (int count = 0; count < TEN_GRADES; count++)
{
sum += numGrades[count];
}
// to get the average
avg = sum / TEN_GRADES;
// to get the highest and lowest mark
for (int count = 0; count < TEN_GRADES; count++)
{
if (numGrades[count] > highest)
highest = numGrades[count];
}
for (int count = 0; count < TEN_GRADES; count++)
{
if (numGrades[count] < lowest)
lowest = numGrades[count];
}
// output requested statistics
cout << "Here are the requested stats for the " << TEN_GRADES << " grades." << endl
<< "The class average is " << avg << endl
<< "The highest grade is " << highest << endl
<< "The lowest grade is " << lowest << endl;
return 0;
}

Maybe a syntax issue here?

Homework problem I am helping one of my mentees out with (check my history, I have previously asked help with Java in more advanced programs. This is something simple I can't help her figure out). We need to use a while loop to read in numbers, keep track of the count, and keep summing up the numbers entered. We keep getting an error in line 24. Even when I comment it out and run it, the programs doesn't do what it is supposed to do. Been forever since I've done a program in C++ and I need the help of you guys!
#include <iostream>
using namespace std;
int main()
{
int num;
int sum = 0;
int count = 0;
float avg;
cout << "Enter numbers, 999 to quit" << endl;
cin >> num; //
while (num != 999)
{
cout << "Number entered is" << num << endl;
cout << "Enter numbers, 999 to quit" << endl;
cin >> num;
sum = sum + num;
count++;
}
cout << "Total numbers entered: " + count << endl;
cout << "Sum of numbers entered is " + sum << endl;
avg = sum/count;
cout << "Average of numbers entered:" + avg << endl;
return 0;
}
cout << "Total numbers entered: " + count << endl;
cout << "Sum of numbers entered is " + sum << endl;
avg = sum/count;
cout << "Average of numbers entered:" + avg << endl;
Change those +'s to <<'s.
cout << "Total numbers entered: " << count << endl;
cout << "Sum of numbers entered is " << sum << endl;
avg = sum/count;
cout << "Average of numbers entered:" << avg << endl;
#include<iostream>
using namespace std;
int main()
{
int num,count;
float sum,average;
cout << "Enter numbers, 999 to quit" << endl;
cin>>num;
count=0;
sum=0;
while (num!=999)
{
cout<<"Number entered is"<<num<<endl;
++count;
sum+=num;
cout << "Enter numbers, 999 to quit" << endl;
cin>>num;
}
if (count==0) {
count=1;
}// if the first number you enter is 999 count should be 1
// , otherwise avg will be (sum/0 ),which doesn't make sense.
cout << "Total numbers entered: " <<count << endl;
cout << "Sum of numbers entered is " <<sum << endl;
average = sum/count;
cout << "Average of numbers entered:"<<average << endl;
// use << not + because "Total..." is string type and count is int type
system("pause");
return 0;
}
You should pay attention to the type of variable when you do something,which often can cause small errors.