Question

In: Computer Science

2. A vector is an ordered collection of values in mathematics. An array is a very...

2. A vector is an ordered collection of values in mathematics. An array is a very straightforward way to implement a vector on a computer. Two vectors are multiplied on an entry-by-entry basis, e.g. (1, 2, 3) * (4, 5, 6) = (4, 10, 18).
Write a program that include the following functions.
void multi_vec (int v1[], int v2[], int v3[], int n);
int comp_vec(int v1[], int v2[], int n);
The multi_vec function multiplies vectors v1 and v2 and stores the result in v3. n is the length of the vectors.
The comp_vec function compares v1 and v2, return 1 if vectors v1 and v2 are equal (their corresponding components are equal), and 0 otherwise. n is the length of the vectors.
In the main function, ask the user to enter the length of the vectors, declare two arrays with the length, read in the values for two vectors, and call the two functions to compute the multiplication and comparison of them. The main function should display the result.
Enter the length of the vectors: 5
Enter the first vector: 3 4 9 1 4
Enter the second vector: 5 7 2 6 8
Output:
The multiplication of the vectors is: 15 28 18 6 32
The vectors are not the same.

Solutions

Expert Solution

Hi, you can compile this code in Dev C++

#include<iostream>
using namespace std;
void multi_vec(int a[],int b[], int c[],int n)
{
        int i;
        for(i=0;i<n;i++)
        {
                c[i]=a[i]*b[i];
        }
}

int comp_vec(int a[],int b[],int n)
{
        int i;
        for(i=0;i<n;i++)
        {
                if(a[i]!=b[i])
                        return 1;
        }
        return 2;
}
int main()
{
        int n,i;
        cout<<"Enter the size of vector :";
        cin>>n;
        
        int a[n],b[n],c[n];
        cout<<"Enter the first vector :";
        for(i=0;i<n;i++)
                cin>>a[i];
                
        cout<<"Enter the second vector :";
        for(i=0;i<n;i++)
                cin>>b[i];
                
        multi_vec(a,b,c,n);
        
        cout<<"The multiplication of vectors is :";
        for(i=0;i<n;i++)
                cout<<c[i]<<" ";
                
        int same=comp_vec(a,b,n);
        if(same==1)
                cout<<"\nthe vectors are not same";
        
        else cout<<"\nthe vectors are same";      
}

Related Solutions

Finding duplicate values of an array of integer values. Example 1 Input: arr[] = {5, 2,...
Finding duplicate values of an array of integer values. Example 1 Input: arr[] = {5, 2, 7, 7, 5}, N = 5 Output: Duplicate Element: 5 Duplicate Element: 7 Example 2 Input: arr[] = {1, 2, 5, 5, 6, 6, 7, 2}, N = 8 Output: Duplicate Element: 2 Duplicate Element: 5 Duplicate Element: 6 CODE:: class Main {    public static void FindDuplicate(int[] arr){ // write your code to find duplicates here and print those values    }   ...
1- The average is a way to characterize a collection of values. When the values refer...
1- The average is a way to characterize a collection of values. When the values refer to a fixed item (for example the number of oranges per tree) and we want to find the average number of oranges, the _______________ mean is appropriate. 2- The ___________________________ mean is used when we want to preserve the effect on the mean of a percentage change in a value independent of the values magnitude. 3- The word size of the IAS  computer (remember one...
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input #...
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input # of rows: 2 Input Row 1: 9 8 7 6 3 2 1 5 4 Input Row 2: 1 2 4 3 5 6 9 8 7 Output 1,2,3,4,5,6,7,8,9 1,2,3,4,5,6,7,8,9
A. Arrays An array is basically of collection of related variables of the same type. For...
A. Arrays An array is basically of collection of related variables of the same type. For example, a collection of grades of students, a collection of salary of employees of a company, etc. When working with student grades, for example, we can then perform statistical computations on that set of data to obtain more meaningful information. In this part of the lab, you will write a complete C++ program named Lab6A.cpp that allows the user to enter the grades of...
1. Dealing with vector fields is very common in engineering and science applications. Vector fields are...
1. Dealing with vector fields is very common in engineering and science applications. Vector fields are often used to model a moving fluid throughout space, magnetic or gravitational force, and etc. a. Provide two examples of vector field for engineering or science applications in two and three dimensions b. Define a conservative vector field. Verify whether the given examples for vector fields in Part 1(a) are conservative? [15 marks] c. Interpret the fundamental theorem of line integrals for conservative vector...
'The terms numeracy and mathematics are often used interchangeably, but in fact they are very different...
'The terms numeracy and mathematics are often used interchangeably, but in fact they are very different concepts' (Grimley, 2016). Your essay should be informed by your understanding of the two concepts: mathematics and numeracy. Weeks 1–4 of your learning materials will give you an understanding of both concepts and how they are crucial to one’s understanding of the world around us. You are required to support your discussion with examples of everyday (maths) phenomena and identify where mathematical and numeracy...
When k=2 and the initial values of the mean vector are (2,8) and (8,1), perform k-means...
When k=2 and the initial values of the mean vector are (2,8) and (8,1), perform k-means clustering on the following input samples in the 2D feature space. (Each iteration step, find the change of the sample vector. Input sample set={(1,2),(2,3),(4,2),(7,10),(5,7),(9,7)}
Using an array and a function, print the values of an array backwards. Please follow these...
Using an array and a function, print the values of an array backwards. Please follow these guidelines: - Setup your array manually (whichever values you want, as many as you want and whichever datatype you prefer). - Call your function. You should send two parameters to such function: the array’s length and the array. - Inside the function, go ahead and print the array backwards. - Your function shouldn’t return anything.
//   Given an array of size 9, with the values of 1-9, determine if the array...
//   Given an array of size 9, with the values of 1-9, determine if the array //   is valid or not. //   Display a message stating the row is VALId, or state its INVALID and what //   was the index number that determined the data invalid. // //   Use this java code as a start of your code. //   Test the array data by changing the values. //============================================================================= import java.util.*;    public class Soduko_ValidateRow    { public static void main(String...
Inputs: Use 2 inputs: cash flow vector, interest rate. Use values: cash flow = (-10, 2,...
Inputs: Use 2 inputs: cash flow vector, interest rate. Use values: cash flow = (-10, 2, 4, 5, 9, 6), interest rate = 12%. Assume that cash flows start at t=0 and go on year by year. Output: Produce a table containing years, cash flows, PV of cash flows, and a summary with NPV, PI, and Payback. (Rstudio)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT