0

I'm fairly new to c++ and I am trying and have searched for how to take an input [Integer] that is looped 6 times and find the average, highest, and lowest input [Integer].So simply put, the program will ask for a score from 6 separate people one after another. Any instructions on how to use a loop to generate 6 different outputs would be greatly appreciated. Sorry if I sound simple in how I’m explaining this. The c++ lingo is a slow learning process for me. This is the for loop I am using.

for(double score = 0.0; score < 6; score++)
2
  • First and foremost you should do your own homework. Copying a solution from this site will not teach you programming. Commented Feb 19, 2015 at 7:25
  • I'm not looking to copy a solution more than studying how a solution became my solution in the first place. Commented Feb 19, 2015 at 19:16

3 Answers 3

1

You'll want to use an index for your loop, and then keep separate variables for your findings, as such:

int sum = 0;
int highest = 0;    // this should be set to the minumum score.
int lowest = 1000;  // this should be set to the maximum score.

for (int i = 0; i < 6; i++) {
  // Read the next number
  int number;
  cin >> number;

  // Add it to the sum
  sum += number;
  // Check to see if it's higher than our current highest.
  if (number > highest) {
    highest = number;
  }
  // Check to see if it's lower than our current lowest.
  if (number < lowest) {
    lowest = number;
  }
}

// Now that we've read our six values, print out the results.
cout << "Average: " << (sum / 6) << endl;
cout << "Highest: " << highest << endl;
cout << "Lowest: " << lowest << endl;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply I'm trying to take an input from 6 people that manually enter a score 1-5 and find the average between all of them. I would like to make it when they enter the information they do it one after another how do I take all that data and use it/access it.
So, what I wrote can be easily adapted for your uses. Look at the top, where highest and lowest are pre-set. Highest should be set to one lower than the minimum valid value, that way it gets set for sure the first time someone enters a number. The opposite for the lowest variable. If it's max + 1 (in your case, 6), then it will definitely get set the first time someone enters a number. cin reads a number from the console, and you can use cout to print messages to the user. Look them up. Also, you'll want to do some validation that they don't enter anything other than a 1-5.
1

I think this is what you want

double score[6];
cout << "Enter 6 different scores \n";
for(int i = 0; i < 6; i++)
cin >> score[i];

With this loop, you will have to input 6 values for score.

With this next loop, you can output those 6 values you inputed earlier

for( i = 0; i < 6; i++)
cout << score[i] << "\n";

1 Comment

Okay I was thinking an array could help. Let me try this brb.
1

Use the rand() or srand() function within the for loop and you should get your random values.

Your other option would be to ask the user to manually enter 6 integers using cin>>number;

Hope this helps :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.