Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Solutions to Assignment 4 - Introduction to Computer Program II | CS 1713, Assignments of Computer Science

Material Type: Assignment; Class: Intro to Computer Program II; Subject: Computer Science; University: University of Texas - San Antonio; Term: Spring 1998;

Typology: Assignments

Pre 2010

Uploaded on 07/30/2009

koofers-user-71p-1
koofers-user-71p-1 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 1713, Solution to Assign 4, Mon Feb 16 1998, Page 1 of 1
/* Name: Neal R. Wagner, Course Instructor
* Date: Due Feb 14, 1997
* Course: CS 1713 Section 02
* Subject: Print the equation of a line through
* two points on the graph of an equation.
* Algorithm: Read x-coordinates x1 and x2.
* Calculate the corresp. y-coords y1 and y2
* on the graph of equation y = x^2 - 3x - 2.
* Calculate the slope m and the y-intercept b.
* Use m and b to print the line’s equation.
* Print integers without a decimal point.
* Input: Keep reading pairs of x-coords up to zeros.
* Output: Unless the x-coords are the same, print the
* line in as in a calculus book.
*/
#include <stdio.h>
#include <math.h>
double f(double x);
void printnum(double z);
void printline(double x1, double y1, double x2, double y2);
void main(void)
{
double x1, y1, x2, y2; /* coords of two points */
printf("Lines through x^2 - 3x - 2.\n");
for (;;) {
scanf("%lf %lf", &x1, &x2);
if (x1 == 0.0 && x2 == 0.0) break;
y1 = f(x1); y2 = f(x2);
printline(x1, y1, x2, y2);
printf("\n\n");
}
printf("Other lines:\n");
printline(2.0, 3.0, 2.0, 4.0); printf("\n");
printline(1.0, 0.0, 3.0, 0.0); printf("\n");
}
/* f: function on which the points occur. */
double f(double x)
{
return x*x - 3.0*x -2;
}
/* print num: Print a double with two decimals unless */
/* it is an exact integer,*/
void printnum(double z)
{
if ( ((int) z) == z)
printf("%1.0f", z);
else
printf("%3.2f", z);
}
/* printline: Print a line as in a calculus book. */
/* x1 and x2 are input x-coordinates of two points, */
/* with y1 and y2 the corresponding y-coordinates. */
void printline(double x1, double y1, double x2, double y2)
{
double m, b;
printf("Line through points: ");
printf("(%3.2f,%3.2f),(%3.2f,%3.2f)\n", x1, y1, x2, y2);
if (x1 == x2) {
if (y1 == y2)
printf("Identical points. There is no line.");
else {
printf("Equation of line: X = ");
printnum(x1);
}
return;
}
m = (y1 - y2)/(x1 - x2); b = -m*x1 + y1;
printf("Equation of line: Y = ");
/* handle case of no X term here, i.e., m == 0. */
/* This includes the case m == 0 && b == 0. */
if (m == 0.0) {
printnum(b);
return;
}
/* Print the X term, assuming m != 0. */
if (m == 1.0)
printf("X ");
else if (m == -1.0)
printf("-X ");
else {
printnum(m); printf("X ");
}
/* Print constant term. Note: if b == 0, print */
/* nothing (works because m != 0 here). */
if (b < 0.0) {
printf("- "); printnum(fabs(b));
}
else if (b > 0.0) {
printf("+ "); printnum(b);
}
}
Lines through x^2 - 3x - 2.
Line through points: (2.10,-3.89),(-2.10,8.71)
Equation of line: Y = -3X + 2.41
Line through points: (0.50,-3.25),(3.75,0.81)
Equation of line: Y = 1.25X - 3.88
Line through points: (1.00,-4.00),(3.00,-2.00)
Equation of line: Y = X - 5
Line through points: (1.00,-4.00),(-2.00,8.00)
Equation of line: Y = -4X
Line through points: (-2.00,8.00),(4.00,2.00)
Equation of line: Y = -X + 6
Line through points: (2.00,-4.00),(2.00,-4.00)
Identical points. There is no line.
Line through points: (0.00,-2.00),(4.00,2.00)
Equation of line: Y = X - 2
Line through points: (-1.00,2.00),(2.00,-4.00)
Equation of line: Y = -2X
Line through points: (-1.00,2.00),(4.00,2.00)
Equation of line: Y = 2
Line through points: (-0.50,-0.25),(3.50,-0.25)
Equation of line: Y = -0.25
Other lines:
Line through points: (2.00,3.00),(2.00,4.00)
Equation of line: X = 2
Line through points: (1.00,0.00),(3.00,0.00)
Equation of line: Y = 0

Partial preview of the text

Download Solutions to Assignment 4 - Introduction to Computer Program II | CS 1713 and more Assignments Computer Science in PDF only on Docsity!

CS 1713, Solution to Assign 4, Mon Feb 16 1998, Page 1 of 1

/* Name:

Neal R. Wagner, Course Instructor

  • Date:

Due Feb 14, 1997

  • Course:

CS 1713 Section 02

  • Subject:

Print the equation of a line through

two points on the graph of an equation.

  • Algorithm:

Read x-coordinates x1 and x2.

Calculate the corresp. y-coords y1 and y

on the graph of equation y = x^2 - 3x - 2.

Calculate the slope m and the y-intercept b.

Use m and b to print the line’s equation.

Print integers without a decimal point.

  • Input:

Keep reading pairs of x-coords up to zeros.

  • Output:

Unless the x-coords are the same, print the

line in as in a calculus book.

{ void main(void)void printline(double x1, double y1, double x2, double y2);void printnum(double z);double f(double x);#include <math.h>#include <stdio.h>/ for (;;) {printf("Lines through x^2 - 3x - 2.\n");double x1, y1, x2, y2; / coords of two points */ printf("\n\n");printline(x1, y1, x2, y2);y1 = f(x1); y2 = f(x2);if (x1 == 0.0 && x2 == 0.0) break;scanf("%lf %lf", &x1, &x2);

printline(1.0, 0.0, 3.0, 0.0); printf("\n");printline(2.0, 3.0, 2.0, 4.0); printf("\n");printf("Other lines:\n");}

{ double f(double x)/* f: function on which the points occur. /} return xx - 3.0*x -2;

{ void printnum(double z)/* it is an exact integer,// print num: Print a double with two decimals unless */} if ( ((int) z) == z) printf("%1.0f", z);

else printf("%3.2f", z);

/* with y1 and y2 the corresponding y-coordinates./* x1 and x2 are input x-coordinates of two points, // printline: Print a line as in a calculus book. */}

{void printline(double x1, double y1, double x2, double y2) if (x1 == x2) {printf("(%3.2f,%3.2f),(%3.2f,%3.2f)\n", x1, y1, x2, y2);printf("Line through points: ");double m, b;

if (y1 == y2) printf("Identical

(^) points.

There

(^) is (^) no (^) line.");

else { printf("Equation

(^) of (^) line:

X

printnum(x1);

return;}

m = (y1 - y2)/(x1}

(^) x2);

(^) b (^) = (^) -m*x

(^) y1;

printf("Equation

(^) of (^) line:

(^) Y (^) = (^) ");

/* handle case of

(^) no (^) X (^) term

(^) here,

(^) i.e.,

(^) m (^) == (^) 0. (^) */

/* This includes

(^) the (^) case

(^) m (^) == (^0) && (^) b (^) == (^) 0. (^) */

if (m == 0.0) { return;printnum(b);

/* Print the X term,}

(^) assuming

(^) m (^) != (^) 0. (^) */

if (m == 1.0) printf("X ");

else if (m == -1.0) printf("-X ");

else { printnum(m); printf("X

/* Print constant}

(^) term.

Note:

(^) if (^) b (^) == (^) 0, (^) print

nothing (works

(^) because

(^) m (^) != (^0) (^) here).

if (b < 0.0) { printf("- ");

(^) printnum(fabs(b));

else if (b > 0.0)}

printf("+ ");

(^) printnum(b);

Lines through x^2 -}

(^) 3x (^) - (^) 2.

Line through points:

Equation of line: Y

-3X

Line through points:

Equation of line: Y

1.25X

Line through points:

Equation of line: Y

X

Line through points:

Equation of line: Y

-4X

Line through points:

Equation of line: Y

-X

Line through points:

Identical points.

There

(^) is (^) no (^) line.

Line through points:

Equation of line: Y

X

Line through points:

Equation of line: Y

-2X

Line through points:

Equation of line: Y

Line through points:

Equation of line: Y

Line through points:Other lines:

Equation of line: X

Line through points:

Equation of line: Y