The main goal of the program is to identify the lowest number inside the array(which is size 10 or list[10] inside the declarations), get the sum of all 10 numbers, remove the lowest number and get the average of the 9 remaining elements inside the array. Inside the code, I have already identified the lowest number inside the array. I tried to assign min to 0 which clearly did not work and got stuck after that.
Watched many youtube videos and searched this up online, but many videos and tutorials only offer to remove the lowest number with the array given by the programmer and not by user input (cin>>). Help would be much appreciated by a beginner computer science student, thank you!
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int dim=10;
int list[dim];
int i;
int min = list[0];
int sum = 0;
int ave;
int temp;
cout<<"enter number: "<<endl; // takes input
for(int i = 0; i<dim; i )
{
cout<<"loc["<<i<<"] ";
cin>>list[i];
}
for(int i=1; i<dim; i ) // gets the lowest num
{
if (list[i] < min)
{
min = list[i];
}
}
cout<<"lowest numb is: "<<min<<endl; // displays the lowest num
min = 0; // tries to replace the element from the list[] array with the value of 0
cout<<"sum is: "<<sum<<endl;
ave = sum/9; // tries to compute the average of 9 numbers, without the lowest element in the array
cout<<"average is: "<<ave<<endl;
}
CodePudding user response:
I want to show how using standard libraries help to get the things much easier, even if it is harder to read for a novice programmer. But knowing STL makes a lot things easier. And using "modern" for loops and lambdas makes the live much easier. I was forced to write it, as I got the impression the code can be written a bit more "modern" :-)
int main()
{
constexpr int dim = 10;
// we use set as container as it sorts internally its elements which
// makes it very easy to get lowest element simply on pos 0.
std::set<int> values;
for(int i = 0; i<dim; i )
{
int v;
std::cout<<"loc["<<i<<"] ";
std::cin>>v;
values.insert( v );
}
// identify the lowest number
// as std::set is sorted, simply print the first element in the set we see lowest number
std::cout << "Lowest Number is " << *values.begin() << std::endl;
// get the sum of all 10 numbers
// as we later on need to calculate the sum again, create a functor which we can reuse on the fly
auto sumFunc = [&values]() { int sum = 0; for ( auto el: values ) { sum = el; } return sum;};
std::cout << "Sum of all elements " << sumFunc() << std::endl;
// remove the lowest number
// as we know the lowest element is at the first position, simply remove begin()
values.erase( values.begin() );
// get the average of the 9 remaining elements
// again take sum and divide by number of elements
// as we expect that average is not really an int, we convert to double
std::cout << "Average of remaining elements " << static_cast<double>(sumFunc()) / values.size() << std::endl;
}
CodePudding user response:
Here is a proper solution written with modern C features:
#include <iostream>
#include <vector>
#include <iterator>
int main( )
{
constexpr std::size_t initialSizeOfArray { 10 };
std::vector<int> numbers( initialSizeOfArray );
std::cout << "Enter " << initialSizeOfArray << " numbers:" << '\n';
for ( std::size_t idx { }; idx < initialSizeOfArray; idx )
{
std::cout << "loc[" << idx << "] : ";
std::cin >> numbers[ idx ];
}
int min { numbers[ 0 ] };
std::size_t idxOfMin { };
for ( auto it = numbers.cbegin( ); it != numbers.cend( ); it )
{
if ( *it < min )
{
min = *it;
idxOfMin = std::distance( numbers.cbegin( ), it );
}
}
std::cout << '\n';
std::cout << "The lowest number is: " << min << '\n';
numbers.erase( numbers.cbegin( ) idxOfMin );
int sum { };
for ( const auto& num : numbers )
{
sum = num;
}
std::cout << "The sum is: " << sum << '\n';
double avg { static_cast<double>( sum ) / static_cast<double>( numbers.size( ) ) };
std::cout << "The average is: " << avg << '\n';
return 0;
}
Use a std::vector
and its erase
member function to remove an element from any position in the vector.
Sample input/output:
Enter 10 numbers:
loc[0] : 10
loc[1] : 9
loc[2] : 8
loc[3] : 7
loc[4] : 6
loc[5] : 5
loc[6] : 4
loc[7] : 3
loc[8] : 2
loc[9] : 1
The lowest number is: 1
The sum is: 54
The average is: 6