Introduction to C++, Assignment Two

   This is the second of two articles about my foray into C++ programming through online college. As with the first article, I'll present a few of the code samples I came up with for my C++ class. As with the last article, I ask that if you're an ACCIS student to please not plagiarize my code. Feel free to look over it, understand it, even use bits and pieces of it.

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];

#include <iostream.h>

#include "stdafx.h"

using namespace std;

int arrayCount(int, int);

const int MAX = 10;

int arrayCount(int b[])

{

      int oddCount;

     

      for(int x = 0; x < MAX; x++)

      {

            cout << b[x] << endl;

            if (b[x] % 2 != 0)

                  oddCount++;

      }

      cout << oddCount << " odd numbers in this array" << endl;

      return oddCount;

}

int _tmain()

{

int myArray[MAX] = {22, 10, 18, 3, 45, 14, 12, 66, 44, 21};

arrayCount(myArray);

      }

This is a good first exampleof using one-dimensional arrays in C++.

2. 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.

#include <iostream.h>

#include "stdafx.h"

using namespace std;

int arrayCount(int, int);

const int MAX = 10;

int arrayCount(int b[], int seekNum)

{

      int diff;

      int lowestNum = 1000000000;

      int closestMatch;

     

      for(int x = 0; x < MAX; x++)

      {

            cout << b[x] << endl;

            diff = (seekNum - b[x]);

            if (diff < 0)

                  diff = (diff * -1);

            if (diff < lowestNum)

            {

                  lowestNum = diff;

                  closestMatch = b[x];

            }

      }

      cout << closestMatch << " is the closest match for " << seekNum << endl;

      return diff;

}

int _tmain()

{

int myArray[MAX] = {22, 10, 18, 3, 45, 14, 12, 66, 44, 21};

arrayCount(myArray, 13);

}

3. 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

 

#include <iostream.h>

#include "stdafx.h"

using namespace std;

int arrayCount(int, int);

const int MAX = 3;

int arrayCount()

{

      int myArray[MAX][MAX];

      int diagposition = 0;

      for(int x = 0; x < (MAX); x++)

      {

            for(int y = 0; y < (MAX); y++)

            {

                  if (y == diagposition)

                        myArray[x][y] = 0;

                  if (y > diagposition)

                        myArray[x][y] = 1;

                  if (y < diagposition)

                        myArray[x][y] = -1;

                  if (y == (MAX-1))

                        cout << myArray[x][y] << endl;

                  else

                        cout << myArray[x][y] << " ";

            }

            diagposition++;

      }

      return diagposition;

}

int _tmain()

{

arrayCount();

}