Free Essay

Cs205

In:

Submitted By nm9287
Words 472
Pages 2
Assignment 2
1. Write a function that takes an array of int as a parameter and returns a count of odd numbers in the array. Assume the array has MAX elements where MAX is a global constant int. That means that before all of the functions there is a declaration like: const int MAX = 10;
And arrays are declared like: int myArray[MAX];
Note: if you are having trouble with the array questions, be sure to work through the “Building Block” problems in this module first.
2. Write a function that takes an array of int as a parameter and returns the sum of odd numbers in the array. Assume the array has MAX elements where MAX is a global constant int.
3. Rewrite your answer to the previous question as a recursive function.
4. Write a function that is passed two parameters: a one-dimensional array of int values, and an int value. The function finds the value in the array that is closest in value to the second parameter.
For example, if the array had the values {5, -3, 18, 9, 4} and the second parameter was 11, then the function would return 9, because 9 is the closest value in the array to 11.
If two values are equally distant, return either. In the previous example, if the second parameter was 7, either 5 or 9 could be returned.
Assume the array has MAX elements where MAX is a global constant int.
5. Write a function that initializes the components of a two-dimensional array in the following manner: components above the upper-left to lower-right diagonal should be set to 1. Those below the diagonal should get -1 and those on the diagonal should be initialized to 0. Assume the array has width and height equal to MAX, where MAX is a global constant. Write a short test program that calls your function and displays the resulting array. For example, a 3x3 array would be initialized to: 0 1 1
-1 0 1
-1 -1 0
1. Write a function that takes an array of int as a parameter and returns a count of odd numbers in the array. Assume the array has MAX elements where MAX is a global constant int.
# include using namespace std;

#include using std::setw;

const int MAX = 10; //setting array size to 10 int myArrayFunction( int [], int );//function prototype

int main ()
{
int myArray[MAX]; //Initialize myArray

cout = 90) grade = 'A'; else if(studentScore >= 80) grade = 'B'; else if(studentScore >= 70) grade = 'C'; else if(studentScore >= 60) grade = 'D'; else grade = 'F'; return grade;
}

void StudentLetterGrade::displayRecord()
// --------------------------------------------------------------
// Preconditions : student's ID number, student's
// score and student's letter grade are already set now we want
// to retrieve them.
//
// Postconditions: Will display the student's ID number along
// with the students score and letter grade
// --------------------------------------------------------------
{
cout

Similar Documents