Title: CS211 Lab1 Program3 in Racket
Date: 2017-01-30T00:00:00
Tags: CS211, Racket, C++
Authors: Henry Brooks

Program 3

Write a program with several functions that perform the following tasks:

a. Read the following 10 integer numbers from the file data.txt into array A. 10 15 27 89 90 95 27 13 99 33 Call function ReadArray that reads numbers from the file data.txt into an array.

b. Copy array A into array B in reverse order. Call function ReverseArray that copies an array into another array in reverse order

c. Print the elements of Array A and B. Call function PrintArray that prints the elements of an array of any size

d. Find the number of elements in array A that are >= 80 and <=100. Call function FindRange that finds and returns the number of elements in an array of any size that are within a certain range (min, max)

e. Find the index of the elements in array A that are divisible by 5. Call function FindDiv that finds and returns the number of the elements in array that are divisible by 5

f. Find average in array A. Call function ArrayAvg that finds and returns the average of elements in an array of any size

g. Find the minimum number in array A. Call function ArrayMin that finds and returns the minimum number in an array of any size

h. Ask the user to input a key. Then search for the key in array A and inform the user about the existence (true / false) of the key in array. Call function KeySearch that searches for a key in an array of any size and returns true/false as result

NOTES:

  • If a function is supposed to return a value, print that value in main not inside the function.
  • Any parameter that is not supposed to be changed inside a function must be declared as a constant parameter.

data.txt

10 15 27 89 90 95 27 13 99 33

output

#:filename "./a.out > output.txt"]{
10 15 27 89 90 95 27 13 99 33 

33 99 13 27 95 90 89 27 15 10 

There are 4 values in arrayA between 80 and 100.

There are elements divisible by 5 at positions 1, 2, 5, 6, 4

The average of the array is 49.8

The minimum element of the array is 10

Enter an integer value to search for: True

Lab1_Program3.cpp

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// Function prototypes *************************************************
int fileSize();                        // aux
void ReadArray(int [], int);            // a
void ReverseArray(int [], int [], int); // b
void PrintArray(int [], int);           // c
int FindRange(int, int, int []);        // d
int FindDiv(int, int []);               // e
double ArrayAvg(int []);                // f
int ArrayMin(int []);                   // g
bool KeySearch(int, int []);            // h

int main () {
  // find length of the input
  int SIZE = fileSize();

  // initialize array A and B
  int arrayA[SIZE];
  int arrayB[SIZE];

  // a. Read data from data2.txt to arrayA
  ReadArray(arrayA, SIZE);
  
  // b. Copy arrayA into arrayB in reverse order
  ReverseArray(arrayA, arrayB, SIZE);
  
  // c. Print arrayA and arrayB
  PrintArray(arrayA, SIZE);
  PrintArray(arrayB, SIZE);
  
  // d. Find the number of elements in array between 80 and 100
  cout << "There are " 
       << FindRange(80, 100, arrayA)
       << " values in arrayA between " 
       << 80
       << " and " 
       << 100 << "." 
       << endl <<endl;
  
  // e. Find the number of elements divisible by 5
  cout << "There are elements divisible by 5 at positions ";
  cout << FindDiv(5, arrayA)
       << endl << endl;
  
  // f. Find the average of the array
  cout << "The average of the array is "
       << ArrayAvg(arrayA)
       << endl << endl;
  
  // g. Find the minimum element of the array
  cout << "The minimum element of the array is " 
       << ArrayMin(arrayA) 
       << endl << endl;
  
  // h. Search for an element in an array
  int num;
  cout << "Enter an integer value to search for: ";
  cin >> num;
  cout << (KeySearch(num, arrayA) ? "True" : "False") 
       << endl << endl;
  
  return 0;
}

//Function definitions *************************************************

int fileSize() {
  int count = 0;
  int temp = 0;
  ifstream inputFile;
  inputFile.open("data2.txt");
  while (inputFile >> temp)
    count++;
  inputFile.close();
  return count;
}

void ReadArray(int arr[], int size) {
  // initialize ifstream to read file
  ifstream inputFile;
  inputFile.open("data2.txt");
  
  if (inputFile.is_open()) { // Only do these if file is open
    // Pull data off data2.txt and place in arr
    for (int i = 0; i < size; i++) {
      inputFile >> arr[i];
    }
  }
  
  // Close the file
  inputFile.close();
}

void ReverseArray(int arrayA[], int arrayB[], int size) {
  for(int i = 0; i < size; i++) {
    arrayB[i] = arrayA[9 - i];
  }
}

void PrintArray(int arr[], int size) {
  for (int i = 0; i < size; i++) {
    cout << arr[i] << " ";
  }
  cout << endl << endl;
}

int FindRange(int first, int second, int arr[10]) {
  int count = 0;
  for(int i = 0; i < 10; i++) {
    if(arr[i] > first) {
      if(arr[i] < second) {
	count += 1;
      }
    }
  }
  return count;
}

int FindDiv(int num, int arr[10]) {
  int count = 0;
  for (int i = 0; i < 10; i++) {
    if (arr[i] % num == 0) {
      cout << i+1 << ", ";
      count += 1;
    }
  }
  return count;
}

double ArrayAvg(int arr[10]) {
  int sum = 0;
  for(int i = 0; i < 10; i++) {
    sum += arr[i];
  }
  return sum / 10.0;
}

int ArrayMin(int arr[10]) {
  int min = arr[0];
  for(int i = 1; i < 10; i++) {
    if(arr[i] < min) {
      min = arr[i];
    } 
  }
  return min;
}

bool KeySearch(int num, int arr[10]) {	
  for(int i = 0; i < 10; i++) {
    if(arr[i] == num) {
      return true;
    } 
  }
  return false;
}

Lab1_Program2.cpp

#lang racket