Click here to Skip to main content
15,908,166 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Write a program that read an array of size 4 (4 elements). and
1- Compute the sum of these four elements using the array.
2- Compute the average of these four elements using the array.
3- Find out the minimum element.


I Tried but it says Error .

What I have tried:

int item[4];
float sum=0, aver,minElement=0;
cout <<"enter four elements"<<endl;
for(int i =0,i<4;i++){
cin>>item[i];
sum =sum+item[i];
}
aver=sum/4;
minElement=item[0]
for(int i =1;i<4;i++){
if (item[i]
Posted
Updated 5-Dec-16 2:57am
v2

In your first for-loop is a comma instead of a semicolon.
 
Share this answer
 
The compiler does not only say "error" but explains also what is wrong and indicates the line number.

So read the error message and inspect the code at the mentioned line number and the lines before that (errors are sometimes detected later while sourced in a previous line).

If you don't understand the error message look it up in the compiler documentation or the web.

If you still got stuck asking here is fine. But you should include the full error message(s) and indicate the line(s) in your code snippet where they occurred. This would make it much easier to help you.

However, the first solution showed you already the problem:
//for(int i =0,i<4;i++)
// Should be:
for(int i =0;i<4;i++)
 
Share this answer
 
You might use just one loop:
C++
#include <array>
#include <climits>
#include <iostream>

using namespace std;

int main()
{

  const size_t ITEMS = 4;

  std::array<int, ITEMS>  a;

  double avg = 0.0;
  int min = INT_MAX;

  cout << "enter " << ITEMS << " integers" << endl;

  for ( auto & x : a)
  {
    cin >> x;
    avg += x;
    min = min < x ? min : x;
  }
  avg /= ITEMS;

  cout << "average " << avg << ", minimum " << min << endl;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900