as1-2-3

as1-2-3 - Introduction to Computer Science I Using C ++ CSC...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Computer Science I Using C ++ CSC 1253 Louisiana State University Greatest Common DivisorProgramming Assignment 1 The greatest common divisor of two strictly positive integers m and n is the largest integer which divides both values. For example, 12 is the greatest common divisor of 24 and 36 since it is the largest of the integers which divide both (1, 2, 4, 6, 8 and 12) and the greatest common divisor of 76 and 0 is undefined since both values must be strictly positive ( m > 0 and n > 0). The following program reads an arbitrary number of integer pairs from the input file pairs.dat and writes to an output file called gcd.out the values read and the greatest common divisor of each unless the gcd is undefined for a pair of values, in which case a warning message is printed. /** * Programming Assignment #1 * * @file gcd.cc * @author - your name - , - your logonid - * @date - todays date - * @version 1.0 */ #include <iostream> #include <fstream> #include <cstdlib> using namespace std; void swap(int &val1, int &val2) { /** * Exchanges the values stored in two integer parameters. * * @param val1 the first value * @param val2 the second value */ 1 int temp; temp = val1; val1 = val2; val2 = temp; } bool valid(int val1, int val2) { /** * Determines if two integer values have a valid gcd. * * @param val1 the first value * @param val2 the second value * @return true iff val1 and val2 have a valid gcd, that is, * both values are strictly positive. Otherwise a false * is returned. */ return val1 > 0 && val2 > 0; } int gcd(int val1, int val2) { /** * Returns the greatest common divisor of two strictly positive * integer values. * * @param val1 the first value * @param val2 the second value * @return the greatest common divisor of val1 and val2. * */ int remainder; remainder = val1 % val2; while (remainder != 0) { val1 = val2; val2 = remainder; remainder = val1 % val2; } return val2; } int main() { /** * The greatest common divisor of two strictly positive integers * m and n is the largest integer which divides both values....
View Full Document

This note was uploaded on 10/02/2011 for the course CSC 1253 taught by Professor Staff during the Fall '08 term at LSU.

Page1 / 6

as1-2-3 - Introduction to Computer Science I Using C ++ CSC...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online