Rhombus Q. Student

CIS 280A

Tuesday and Thursday; 9:40 - 11:45 AM; AAR124

Documentation: Programming Assignment One

Assigned: Tuesday, Week One

Due: Tuesday, Week 2

Problem Location: Chapter Two, Problem 9, page 89

Program Name: SumIt

Program Description: The computer application (program) SumIt expects a single integer n from standard input (typically the keyboard), where n is greater than or equal to 1. SumIt, based on the value of n, shall computer the sum of all even integers from 1 to n, the sum of all odd integers from 1 to n and the sum of all integers from 1 to n. The program, after completing these numerical calculations, writes these sums to standard output (typically the monitor

Source Code for SumIt:

// This is the main project file for VC++ application project
// generated using an Application Wizard.
#include "stdafx.h"
#include <iostream>
#using <mscorlib.dll>
using namespace System;
using namespace std; // Needed for cin and cout.
bool EvenNumber( int ); // EvenNumber prototype.
///<Remarks>
///
/// This program, written by Rhombus Q. Student (aka Donald D.
/// Derkacht), is being used to test Visual Studio .NET and the C++
/// compiler. This program also serves an a programming example
/// for the documentation for showing how to document a program.
///
/// Program Name:
/// SumIt
///
/// Inputs:
/// n: an integer greater than or equation to one.
///
/// Outputs:
/// evenSum: The sum of all even integers from 1 to n.
/// oddSum: The sum of all odd integers from 1 to n.
/// sum: The sum of all integers from 1 to n.
///
/// Algorithm:
/// While there are formulas that provide the sum of finite
/// arithmetic series, this program, SumIt, uses brute force
/// iteration to calculate the desired sums. In order to
/// determine when an integer is even or odd, the code used
/// the remainder (modulus) operator and division by 2.
///
///</Remarks>
int _tmain()
{
// TODO: Please replace the sample code below with your own.
int n;
int evenSum;
int oddSum;
int sum;
evenSum = 0;
oddSum = 0;
sum = 0;
cout < "SumIt Invoked" < endl;
cout < "Please enter a value for n." < endl;
cin > n;
for (int ii = 1; ii <= n; ii++ ){
if (EvenNumber(ii)) {
evenSum = evenSum + ii;
} // then
else {
oddSum = oddSum + ii;
} // else
} // for
sum = evenSum + oddSum;
// Console::WriteLine(evenSum);
// Console::WriteLine(oddSum);
// Console::WriteLine(sum);
cout < "The sum of all even integers from 1 to "
< n
< " is = "
< evenSum
< endl;
cout < "The sum of all odd integers from 1 to "
< n
< " is = "
< oddSum
< endl;
cout < "The sum of all integers from 1 to "
< n
< " is = "
< sum
< endl;
cin > n;
return 0;
}
///<Remarks>
///
/// Function Name and Type:
/// bool EvenNumber: Value returning function.
///
/// Inputs:
/// int number: an integer between 1 and maxInt.
///
/// Output:
/// The determine function returns true if number is even;
/// else it returns false.
///
/// Algorithm:
/// The function EvenNumber uses the remainder (modulus)
/// operator to determine if number is even or odd by dividing
/// number by the integer 2 and looking at the remainder. If the
/// remainder is 0, then number is even; if the remainder is
/// odd, then number is odd.
///
///</Remarks>
bool EvenNumber(int number) {
if (number % 2 == 0) {
return true;
} // then
else {
return false;
} // else
}

Output for Execution Run 1 of SumIt:

The following run tests SumIt for robustness for small or special values. The program has not been “bullet proofed” to check for input values less than 1, but that should have been done. I apologize. Please deduct 20 points for my not having completed this task.

SumIt Invoked

Please enter a value for n.

1

The sum of all even integers from 1 to 1 is = 0

The sum of all odd integers from 1 to 1 is = 1

The sum of all integers from 1 to 1 is = 1

The verification for this run is trivial and can be checked mentally.

Output for an Execution Run 2 of SumIt:

SumIt Invoked

Please enter a value for n.

6

The sum of all even integers from 1 to 6 is = 12

The sum of all odd integers from 1 to 6 is = 9

The sum of all integers from 1 to 6 is = 21

Verification of Output for Run 2:

2 + 4 + 6 = 12
1 + 3 + 5 = 9
1 + 2 + 3 + 4 + 5 + 6 = 21

The following checklist is to be used by the instructor only.

Grading Criteria / Weight / Grade
Correctness of program output / 30%
Clarity of the user interface / 20%
Source code, internal commenting, verification and so forth. / 50%
Overall grade for Programming Assignment One:

Instructor Comments:

1