Sie sind auf Seite 1von 3

Programming Assignment

Assignment
Language: C++.

Question #1
Simple introduction question (5 points)
Using a for loop, write a function to compute the k-th power of a number :
double power(double x, int k)

Question #2

Using two-dimentional arrays, write a function (and a corresponding program to test it)
which multiplies an mxn matrix of integers by an nxr matrix of integers. Use global
constant declarations before the main program to give test values for m, n and r. Example
input/output might be:

INPUT FIRST (2x2) MATRIX:


Type in 2 values for row 1 separated by spaces: 3 4
Type in 2 values for row 2 separated by spaces: 5 7
INPUT SECOND (2x2) MATRIX:
Type in 2 values for row 1 separated by spaces: 1 1
Type in 2 values for row 2 separated by spaces: 2 2

3 4
5 7
TIMES
1 1
2 2
EQUALS
11 11
19 19
Question #3
Greek mathematicians took a special interest in numbers that are equal to the sum of their
proper divisors (a proper divisor of N is any divisor less than N itself). They called such
numbers perfect numbers. For example, 6 is a perfect number because it is the sum of
1, 2, and 3, which are the integers less than 6 that divide evenly into 6. Similarly, 28 is a
perfect number because it is the sum of 1, 2, 4, 7, and 14.
Write a predicate function IsPerfect that takes an integer n and returns true if n is
perfect, and false otherwise. Test your implementation by writing a main program that
uses the IsPerfect function to check for perfect numbers in the range 1 to 9999 by
testing each number in turn. When a perfect number is found, your program should
display it on the screen. The first two lines of output should be 6 and 28. Your program
should find two other perfect numbers in the range as well.
Question #2
Class and Data Abstraction

Some of the characteristics of a book are the title, author(s), publisher, ISBN, price, and
year of publication. Design a class bookType that defines the book as an ADT.

1. Each object of the class bookType can hold the following information about a
book: title, up to four authors, publisher, ISBN, price, and number of copies in
stock. To keep track of the number of authors, add another data member.
2. Include the member functions to perform the various operations on objects of the
type bookType. For example, the usual operations that can be performed on the
title are to show the title, and check whether a title is the same as the actual title of
the book. Similarly, the typical operations that can be performed on the number of
copies in stock are to show the number of copies in stock, set the number of
copies in stock, update the number of copies in stock, and return the number of
copies in stock. Add similar operations for the publisher, ISBN, book price, and
author. Add the appropriate constructors and a destructor ( if one is needed).
3. Write the definitions of the member functions of the class bookType.

Write a program that uses the class bookType and test the various operations on the
objects of class bookType. Declare an array of 100 components of the type
bookType. Set to 5 components. Display the output.

Das könnte Ihnen auch gefallen