Lab 3 - Basic for Loops

The purpose of this lab is to use for() loops to calculate the sum, the minimum and the maximum of a list of numbers. This lab will also use constants.

Review Lab 1 for how to log in to Sleipnir, edit source code and send an email.

You may work in groups of up to 3 students for this lab. Name your source code file lab3.cpp and make sure to put the names of all group members in the comment section at the top of the source code file.

Program Requirements: Take the summation example given in class and modify it to also calculate the minimum and maximum of the numbers entered. Create two new variables for the minimum and maximum. Initialize the minimum to a large number (such as 200000000) and the maximum to a small number (such as -200000000). Within the body of the for() loop, compare the temporary number to the minimum and maximum. If the temporary number is smaller than the minimum, update the minimum to be the temporary number. If the temporary number is larger than the maximum, update the maximum to be the temporary number.

The summation example given in class was:

#include <iostream>
using namespace std;

#define MAX_COUNT  4

int main()
{
  int sum = 0;

  for(int i = 0; i < MAX_COUNT; i++)
  {
    int tmp;
    cout << "Enter integer " << (i + 1) << ": ";
    cin >> tmp;
    sum = sum + tmp;
  }

  cout << "The sum is " << sum << endl;

  return 0;
}
To cut-and-paste with vi, before going into insert mode give the command
:set paste
Then go in to insert mode and paste the code. Then hit escape to exit insert mode and give the command
:set nopaste
to turn off pasting mode. Then enter insert mode and add the new code for the lab assignment.

Try changing the value for MAX_COUNT (recompile your source code every time you do this) and see how the code behaves.

Lab Submission: Email me and attach your lab3.cpp file (the source code) to the email. Do NOT email me your lab3 file (the executable). You should also CC your lab partner(s) on the email so they have a copy of the source code.