In: Computer Science
Objective:
The objective of this assignment is to allow students to understand the basic principles of arrays
Program Requirements:
The assignment will require the students use arrays to perform mathematical calculations.
Program Setup: Functions to include in the program
The program will have three functions
Function 1: Responsible for setting values in the arrays
Function 2: Responsible for printing values in arrays
Function 3: Responsible for doing calculations to arrays
Function 1 Details: Setting array values
Parameters: 3
Array of integers
Int value representing the size of array
String value representing the “array type”
Return Type: None
This function will be responsible for filling in the values of the array
The function will in the array using the following steps
Step 1: Checking the value of the array type (3rd parameter)
The array type will determine how we are going to fill in the arrays
The array type is going to be checked for the following
Value 1
Value 2
Step 1 Part A: Filling in the array for “value 1”
The program will ask the user to enter an integer value
The integer value will be stored in the array
The user will have to keep entering values until the entire array is filled (using the size of the array)
Step 1 Part B: Filling in the array for “value 2”
The program will randomly select a number between 1 and 200
The random number will be stored in the array
The program will store a random number until the entire array is filled (using the size of the array)
Function 2 Details: Printing arrays
Parameters: 2
Array of integers
Int value representing the size of array
Return Type: None
This function will be responsible for printing out the contents of the array
Contents will be print in the following format
Index # : value of the array (at that index)
Function 3 Details: Performing mathematical calculations on the arrays
Parameters: 5
Array of integers for value 1
Array of integers for value 2
Array of integers for the sum
Int value representing the size of array
String value representing the math operation
Return Type: None
This function will be responsible for performing the mathematical calculations on the two arrays
Step 1: Checking the value of the math operation (5th parameter)
The math operation will determine what mathematical calculation will be done on the two arrays
The math operation will be one of the following choices
Add
Sub
Mul
Div
Step 2: Perform the selected mathematical calculation
The function is then going to perform the mathematical on the two arrays
For Example, lets say “Add” was selected
The function will then go through every element in both arrays and add them together
The results will be stored in the sum array
Array1[i] + array2[i] = sum[i]
The program will then print the results to the screen in the following format
Array1[i] <mathematical operator> array2[i] = sumArray[i]
Flow of the program
Now that the functions have been defined, we are now going to describe how the program is going to flow from the main
Program Setup
There should be three arrays declared at the beginning of the main
All three arrays should have the same size
Step 1: Filling in the values for array 1
Use Function 1 to set the values for array 1
Array 1 will be passed as the first parameter
Array size will be passed as the second parameter
“Value 1” will be passed as the third parameter
Step 2: Printing the contents of array 1
Use Function 2 to print array contents
Array 1 will be passed as the first parameter
Array size will be passed as the second parameter
Step 3: Filling in the values for array 2
Use Function 1 to set the values for array 2
Array 2 will be passed as the first parameter
Array size will be passed as the second parameter
“Value 2” will be passed as the third parameter
Step 4: Printing the contents of array 2
Use Function 2 to print array contents
Array 2 will be passed as the first parameter
Array size will be passed as the second parameter
Step 5: Performing mathematical calculations
Use Function 3 to perform the following mathematical calculations
Call function 3 four times
The first four parameters are going to stay the same for each call
Array 1
Array 2
Sum Array
Array Size
The fifth parameter is going to change base between each call
Add: first call
Sub: second call
Mul: third call
Div: fourth call
Done in C++ and visual studios 2017
Program:
#include <bits/stdc++.h>
using namespace std;
void setValues(int *array, int n, int typ)
{
if(typ==1)
{
cout<<"Enter "<<n<<" array elements\n";
for(int i=0; i<n; i++)
{
int a;
cin>>a;
array[i]=a;
}
}
else
{
cout<<"Randomly choosing array elements......\n";
for(int i=0; i<n; i++)
{
array[i]= rand() % 200;
}
}
}
void printArray(const int *array, int n)
{
for(int i=0; i<n; i++)
{
cout<<i+1<<": "<<array[i]<<endl;
}
}
void arrayOper(const int *arr, const int *brr, int *res, int n, char oper)
{
if(oper == '+')
{
for(int i=0; i<n; i++)
{
res[i] = arr[i] + brr[i];
cout<<arr[i]<<" + "<<brr[i]<<" = "<<res[i]<<endl;
}
}
if(oper == '-')
{
for(int i=0; i<n; i++)
{
res[i] = arr[i] - brr[i];
cout<<arr[i]<<" - "<<brr[i]<<" = "<<res[i]<<endl;
}
}
if(oper == '*')
{
for(int i=0; i<n; i++)
{
res[i] = arr[i] * brr[i];
cout<<arr[i]<<" * "<<brr[i]<<" = "<<res[i]<<endl;
}
}
if (oper == '/')
{
for(int i=0; i<n; i++)
{
res[i] = arr[i] / brr[i];
cout<<arr[i]<<" / "<<brr[i]<<" = "<<res[i]<<endl;
}
}
}
int main()
{
int n, typ1, typ2;
cout<<"enter the length of array: ";
cin>>n;
int arr[n], brr[n], res_arr[n];
cout<<"How you want to fill the array 1?\n1. Manually\n2. Randomly\n";
cin>>typ1;
setValues(arr, n, typ1);
printArray(arr, n);
cout<<"How you want to fill the array 2?\n1. Manually\n2. Randomly\n";
cin>>typ2;
setValues(brr, n, typ2);
printArray(brr, n);
cout<<"Operations: \n";
arrayOper(arr, brr, res_arr, n, '+');
cout<<endl;
arrayOper(arr, brr, res_arr, n, '-');
cout<<endl;
arrayOper(arr, brr, res_arr, n, '*');
cout<<endl;
arrayOper(arr, brr, res_arr, n, '/');
return 0;
}
Output :
Hope this will help!
Good Luck :)