ProgrammingNoob
ProgrammingNoob

Reputation: 3

Using arithmetic with arrays

  1. Two quantities u and v are said to be at right angles if

    nuv = u1v1 + u2v2 + u3v3 + u4v4 +………… + unvn = 0

Write a function that computes whether u and v are at right angles. You may use arrays if you wish. The function can assume that the vectors have the same dimension (n, say), but this number should be passed as a parameter to the function.

I have a few errors in my program. I'd appreciate some help, as I'm a beginner. The errors are telling me:

In function 'void function(int*,int*)'
cpp 26: error expected ';' before '}' token
cpp 29: error ;value required as left operand of assignment

#include <iostream> 

using namespace std; 

const int n = 5;

void function(int array[n],int array2[n]);

int main(){

    int array[n] = {5, 3 , -4, 2, 8};
    int array2[n] ={-7, -9, 5, 2, 9};

    function(array, array2);

    return 0;
}

void function(int array[n], int array2[n]){

    int multiple;

    for(int i=0; i <=5, i++)
    {
        (array[i]*array2[i]) + (array[i+1]*array2[i+1]) = multiple;
    }

    cout << multiple << endl;
}

Upvotes: 0

Views: 4302

Answers (4)

user2672107
user2672107

Reputation:

Alternative: try the C++ way. Use std::array that knows it's length. Use algorithms provided by the standard library like std::inner_product.

#include <iostream>
#include <algorithm>
#include <array>
#include <numeric>

int main()
{
  using arr_t = std::array<int,5>;

  arr_t arr1 = {5, 3 , -4, 2, 8};
  arr_t arr2 = {-7, -9, 5, 2, 9};

  int mult = std::inner_product( begin(arr1), end(arr1), begin(arr2), 0,
                 std::plus<>(), std::multiplies<>() );

  std::cerr << mult << "\n";
}

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 310960

The function can assume that the vectors have the same dimension (n, say), but this number should be passed as a parameter to the function.

This function declaration

void function(int array[n],int array2[n]);

does not include a parameter that specifies the dimension of the arrays.

The above declaration is equivalent to

void function(int *array,int *array2);

because arrays passed by value are implicitly converted to pointers to their first elements.

There are a typo and a bug in this for statement

for(int i=0; i <=5, i++)
             ^^^^^^

There shall be

for ( int i=0; i < n; i++)

The variable multiple

int multiple;

is not initialized and this assignment

(array[i]*array2[i]) + (array[i+1]*array2[i+1]) = multiple;

does not have sense and has nothing common with the condition

nuv = u1v1 + u2v2 + u3v3 + u4v4 +………… + unvn = 0

It seems what you mean is the following

#include <iostream>

bool function( const int array[], const int array2[], size_t n ) 
{

    long long int product = 0;

    for ( size_t i = 0; i < n; i++)
    {
        product += array[i] * array2[i];
    }

    return product == 0;
}

int main()
{
    const size_t N = 5;
    int array[N]  = { 5, 3 , -4, 2, 8 };
    int array2[N] = { -7, -9, 5, 2, 9 };

    std::cout << "array and array2 are "
        << (function(array, array2, N) ? "" : "not ")
        << "at right angles"
        << std::endl;

    return 0;
}

These arrays

    int array[N]  = { 5, 3 , -4, 2, 8 };
    int array2[N] = { -7, -9, 5, 2, 9 };

are not a right angles,

But these arrays

    int array[N]  = { 5, 3 , -4, 1, 9 };
    int array2[N] = { -7, -9, 5, 1, 9 };

are at right angles. Try them.

Upvotes: 1

user4880283
user4880283

Reputation:

Syntax error is where you are using the for loop in this fashion:

for(int i=0;i<=5,i++)

Use this instead:

for(int i=0; i <= 5; i++)

Upvotes: 1

Remy Lebeau
Remy Lebeau

Reputation: 595971

Your for loop is malformed. You need to use < instead of <=, use n instead of 5, and use ; instead of ,.

Your assignment of multiple is backwards. The value on the right-side of the = operator is assigned to the variable on the left-side of =. You are trying to assign the value of multiple (which is uninitialized) to a dynamically computed value that has no explicit variable of its own. You should be assigning the computed value to multiple instead.

Also, you didn't follow the "this number [array dimensions] should be passed as a parameter to the function" requirement of your instructions.

Try this:

#include <iostream>   
using namespace std; 

const int n = 5;

void function(const int *array1, const int *array2, const int size);

int main()
{
    int array1[n] = { 5,  3, -4, 2, 8};
    int array2[n] = {-7, -9,  5, 2, 9};

    function(array1, array2, n);

    return 0;
}

void function(const int *array1, const int *array2, const int size)
{
    int multiple = 0;

    for(int i = 0; i < size; i++)
    {
        multiple += (array1[i] * array2[i]);
    }

    cout << multiple << endl;
}

Upvotes: 1

Related Questions