Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4617118
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:04:46+00:00 2026-05-22T02:04:46+00:00

Hey there, I have a probably straight-forward question about the following problem: I have

  • 0

Hey there,
I have a probably straight-forward question about the following problem:

I have a function

double afunction(double *myarray)
{   
    double ret = 1.0;
    for(int i = 0; i < 4; i++)
        ret *= myarray[i]*myarray[i];
    return ret;
}

Now I want to modify it so that I pass two new parameters: int index describing which index of myarray to change like the following: myarray[ index ] = *add;. This would be the following:

double afunction(double *myarray, int index, double *add)
{   
    myarray[ index ] += *add;
    double ret = 1.0;
    for(int i = 0; i < 4; i++)
        ret *= myarray[i]*myarray[i];
    return ret;
}

The problem is: I don’t want to modify the array myrray and I don’t want to create a new array for this because of memory-regards (this will later be computed on the GPU and there I couldn’t allocate a whole new array in a kernel function anyway.
Easy solution to this?
Thanks!

EDIT
Sorry, I mistyped something. Instead myarray[ index ] = *add; I meant to say myarray[ index ] += *add;

EDIT2
Example of a bigger function which could later on easily be extended to about 50 different return-cases. So to have the if-statement to modify the certain value myarray[index] with adding *add in each return-case is pretty ugly 🙁

double afunction(double *myarray, int funcIndex, int indexAdd, double *add)
{   
    myarray[ indexAdd ] += *add;

    if(funcIndex >= 1 && funcIndex <= 4)
        return myarray[1]*myarray[1]*myarray[2];

    switch(funcIndex)
    {
        case 5:
            return sin(myarray[3]) * cos(myarray[1]);
        case 6:
            double ret = exp(myarray[1]);
            for(int i = 1; i < 5; i++)
                ret *= (myarray[ i ]-myarray[ 5-i ]);
            return ret;
        case 7:
            double ret = 0.0;
            for(int i = 1; i < 10; i++)
                ret += myarray[ i ];
            return ret;
    }

    return 0.0;
}
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:04:47+00:00Added an answer on May 22, 2026 at 2:04 am
    double afunction(double *myarray, int index, double *add)
    {   
        double ret = (*add) * (*add);
        for(int i = 0; i < 4; i++)
            if( i != index )
                ret *= myarray[i]*myarray[i];
        return ret;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey there, I'm trying to make a site which have following: News, Products, About
Hey just a quick question for any experts out there. I have a site
Hey guys, one more quick question for any experts out there. I have a
Hey there, assuming I have the following code in a mex routine: mxArray *newPoint
hey there i am having problem i have List<List<memoryCard>> that i want to show
Hey there, I have a small problem with compiling and linking with the command
Hey there I have this function: void vdCleanTheArray(int (*Gen_Clean)[25]) { } I wanted to
Hey there, I have dynamic question marks on my landing page that scroll with
Hey there programming peps! I have a question re: jQuery. We have a software
Hey there- I'm testing jQuery Mobile and have a question. I wrote a simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.