{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Computer Science I Using C ++ CSC 1253 Louisiana State University Greatest Common Divisor—Programming 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 - today’s 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
Image of page 1

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

View Full Document Right Arrow Icon
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. * For example, 12 is the greatest common divisor of 24 and 2
Image of page 2
* 36 since it is the largest of the integers (1, 2, 4, 6, 8 * and 12) which divide both and the greatest common divisor * of 76 and 0 is undefined since both values must be strictly * positive.
Image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern