0

i'm new to programming , this code gives me syntax error in line => int *result = apply_all(array1,5,array2,3) this is the error: expected primary-expression before '}' token|
i'm trying to write function called apply_all expects 2 arrays of integers and their sizes and dynamically allocates a new array of integers whose size is the product of 2 array sizes. the function should loop through the 2nd array and multiple each element accross each element of array 1 and store the product in newly created array. the function is returning a pointer of to the newly allocated array. also i wrote a function which is print to display the 1st & 2nd & newly array.

#include <iostream>

using namespace std;
//function prototype
int *apply_all(int *array1 ,int size1,int *array2,int size2);
void print(int *array,int size);



int main()
{
    int array1[] {1,2,3,4,5};
    int array2[] {10,20,30};

    cout << "Array 1:";
    print(array1,5);

    cout << "Array 2:";
    print(array2,3);

    int *result = apply_all(array1,5,array2,3);             
    cout << "Result : ";
    print(result,15);
    delete [] result;
    return 0;
}


int *apply_all(int *array1 ,int size1,int *array2,int size2)
{
        int *result {nullptr};
        result = new int[size1 * size2];   
        for (int i{0};i<size2;i++)          
            for(int j{0};j<size1;j++)          
                  *(result[i*5+j]) = *(array1[i])**(array2[j]);           
        return result;
}


void print(int *array,int size)
{
    for(auto num:array)
        cout << num << endl;
}
1
  • also i was thinking to replace this line *(result[i*5+j]) = *(array1[i])**(array2[j]); to be *(result++) = *array1[i]**array2[j]; or create a new variable for ex: k++ in for loop and *(result[k]) = *array1[i]**array2[j]; Commented Jul 3, 2020 at 19:48

2 Answers 2

2

On this line:

*(result[i*5+j]) = *(array1[i])**(array2[j]);

since result[i*5+j] gives you an int, you are trying to dereference an int, which is not possible.

You just need to do:

result[i*5+j] = array1[i] * array2[j];

Also, in print, your range-for loop won't work with a pointer. You need to do:

for(int i = 0; i < size; ++i)
    cout << array[i] << endl;

Also, in apply_all, your loop bounds are incorrect. i needs to go till size1, and j needs to go to size2.

Here's a demo.

Sign up to request clarification or add additional context in comments.

4 Comments

thank you so much but i have 2 questions 1- *(result[i*5+j]) = *(array1[i])**(array2[j]); , so this line i've already accessed the array elements because i use array subscript notation [] so i don't need to deference it?? what i understand result now has the address of array in heap. so to access it i can use [] or * 2- why range-for loop won't work with a pointer?? sorry for those beginner questions
Yes, [] and * are related like you say. For the second question see stackoverflow.com/questions/15904896/…
thank you :) i read that in the article you provided You can't use range-for-loop with dynamically allocated arrays, since compiler can't deduce begin and end of this array can i use vector with pointer in for range based??? void display (vector<string> *v) { for(auto str: *v) cout << str << endl;} int main(){ vector<string> str1 ["joe","logan","Jasmine"] display(&str1);
You could, but just pass in the vector by reference, instead of by pointer.
1

Since you are new, a simple work around would be creating an array with buffer space to store your results in and passing the pointer for this into apply_all. You could then write to this array which (being declared in main) should be very easy to access and cause few errors and use a c-string like ending to know when your results are over and to stop printing from the array (c-strings end with a value of 0 so that programs don't read unrelated memory). eg:

int buf[99];
apply_all(array_1, size1, array_2, size2, buf, size3);
for (int x = 0; buf[x] != end of buf var; x++;)
{
    print(buf[x])
}

and

apply_all()
{
    buf[start-end] = whatever you want;
    buf[end + 1] = some variable that won't appear in buffer; //max int size?
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.