Question

In: Computer Science

Objective: The objective of this assignment is to allow students to understand the basic principles of...

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

Solutions

Expert Solution

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 :)


Related Solutions

Purpose of Assignment  The purpose of this assignment is to allow the students to understand and...
Purpose of Assignment  The purpose of this assignment is to allow the students to understand and practice the measurement of present value, future value, and interest rate using Microsoft® Excel®.  Assignment Steps  Resources: Microsoft® Office® 2013 Accessibility Tutorials, Microsoft® Excel®, Time Value of Money Calculations Template Calculate the following time value of money problems using Microsoft® Excel®: If we place $8,592.00 in a savings account paying 7.5 percent interest compounded annually, how much will our account accrue to in 9.5 years? What is the present value of...
Purpose of Assignment The purpose of this assignment is to allow the students to become familiar...
Purpose of Assignment The purpose of this assignment is to allow the students to become familiar with and practice the measurement of Net Present Value (NPV), payback, and Weighted Average Cost of Capital (WACC) using Microsoft® Excel®. Assignment Steps Resources: Microsoft® Excel®, Capital Budgeting Decision Models Template Calculate the following problems using Microsoft® Excel®: Calculate the NPV for each project and determine which project should be accepted. Project A Project B Project C Project D Inital Outlay (105,000.000) (99,000.00) (110,000.00)...
Purpose of Assignment The purpose of this assignment is to allow students the opportunity to research...
Purpose of Assignment The purpose of this assignment is to allow students the opportunity to research a Fortune 500 company stock using the popular online research tool, Yahoo Finance. The tool allows the student to review analyst reports and other key financial information necessary to evaluate the stock value and make an educated decision on whether to invest. Assignment Steps Select a Fortune 500 company from one of the following industries: Pharmaceutical Energy Retail Automotive Computer Hardware Manufacturing Mining Review...
The communication principles impact our communication. It is important to recognize and understand the eight basic...
The communication principles impact our communication. It is important to recognize and understand the eight basic principles for effective interpersonal communication. Select on the principles and describe a situation where this principle impacted you in a significant manner. Describe an I–It, I–You, and I–Thou relationship in your life (one each). Analyze differences in communication and personal knowledge in the three relationships.
Objective: Work with basic input commands. Work with decision making commands. Assignment This will be done...
Objective: Work with basic input commands. Work with decision making commands. Assignment This will be done using Python. Part 1 Create a program that asks the user to input a number between 20 and 99. The program must then print out the number in English words. If the user does not enter a value between 20 and 99 display an error message stating that the input is not within a valid range. No credit will be given for programs that...
Basic Unix Commands Objective: The objective of this lab is to work with files of UNIX...
Basic Unix Commands Objective: The objective of this lab is to work with files of UNIX file system. Procedure: 1. OpenyourUnixshellandtrythesecommands: Ø Create a new file and add some text in it vcat > filename Ø View a file vcat /etc/passwd vmore /etc/passwd vmore filename Ø Copy file, making file2 vcp file1 file2 Ø Move/rename file1 as file2 vmv file1 file2 Ø Delete file1 as file2 vrm file //Deletefile //Double-checkfirst vrm -i file Ø Counts the lines, words, characters in...
Discuss the basic principles of cash management.
Discuss the basic principles of cash management.
Explain 3 of the basic principles of lending. What are the consequences of violating these principles...
Explain 3 of the basic principles of lending. What are the consequences of violating these principles of lending?
List the basic principles of the distillation tower at least 4 principles and why there are...
List the basic principles of the distillation tower at least 4 principles and why there are several design techniques? I need the answer in obvious and clear script not by handwriting please.
Analysis of five basic principles of finance: Define the five basic principles finance and justify your analysis by illustrating examples:
Analysis of five basic principles of finance: Define the five basic principles finance and justify your analysis by illustrating examples: Choose one or more events described by media (CNN Business, Financial Times, Dow Jones financial news etc.) about companies and financial market. Analyse that event (s) applying the five basis principles of finance. Note: Each principle is to be illustrated by at least one event. The events should be in 2020
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT