-1

so I was wondering if there was a way to have an equation in C++, and use that equation to make an array that has the results of the equation. For example, an equation like n^(2)-1 would create an array of [1,3,7,12,31].

I am a beginner in C++, so I have no clue where I would start in doing this.

I can provide more details if needed, thank you in advance!

Toska
  • 1
  • 3
  • When you say that you have the function n^2-1 you mean a string that represents the function or a function i can call like f(n) = return n^2-1 (the first one would be extremely hard to handle :p), what would be the range of that array, it goes from 0 to n? – Jordi Bustos Mar 31 '23 at 00:53
  • the function like F(n)=n^2-1, sorry i shouldve put proper notation. and the range would preferably be adjustable when needed. – Toska Mar 31 '23 at 00:56
  • You could use std::generate_n – lastchance Mar 31 '23 at 04:12
  • You mean function, not equation. – molbdnilo Mar 31 '23 at 06:36

3 Answers3

0

The code to do what you are asking for is the following:

First we declare a function func that receives an integer range

Second we declare the array and inside the for loop that goes from 0 to range we put at the position i of the array the integer i**2 - 1 (or whatever the function you want does)

Third we return the array

Feel free to ask and play with the code :)


std::vector<int> func(int range) {
    std::vector<int> f_array; //array declared
    
    for(int i = 0; i < range; i++) {
        f_array[i] = i * i - 1
    }

    return f_array;
}

Jordi Bustos
  • 108
  • 7
  • 1
    `pow(i, 2)` -- [Please read this about using pow()](https://stackoverflow.com/questions/25678481/why-does-pown-2-return-24-when-n-5-with-my-compiler-and-os). Instead of `pow`, use `i * i`. – PaulMcKenzie Mar 31 '23 at 04:20
  • I wanted to do it generic as OP asked, but i didn't knew about rounding errors. Thanks for sharing :) – Jordi Bustos Mar 31 '23 at 13:22
0

RephraseThe process can be accomplished by allocating a response for each iteration in an array. The function generates an array containing the answers, resulting in an output that can be accessed by the user.

int arr[10];
for(int i=0; i<n; i++){
    arr[i]=(i*i-1);
}
AayushLad
  • 11
  • 2
0

I don't think there is any inbuilt functionality for doing that best i can think of is using for loop and storing value in array for each i value of n can be passed as argument. function is returning an array which contains of value of equation arr[i]=(i^2)-1.

int *equation_n2(int n){
    int *arr=new int[n+1];
    
    for (int i = 0; i <=n; i++)
    {
        arr[i]=pow(i,2)-1;
        /* code */
    }
    
    for (int i = 0; i <=n; i++)
    {
        cout<<arr[i]<<" ";
        /* code */
    }
    return arr;
    
}
  • 1
    Do not use `pow` to solve integer-based problems. The `pow` function is a floating point function, and with floating point, you get all of the issues associated with it (such as non-exact results). – PaulMcKenzie Mar 31 '23 at 05:29