Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

c++ programming arrays?

1). write the definition of the function inputArray that prompts the user to input 20 numbers and stores the numbers into an array called alpha

2).

double highTemp[12];

double lowTemp[12];

double range[12];

assume the code has been written to read in values for the two arrays; highTemp and lowTemp. write the loop to fill the array, range ( of high to low ) for each of the twelve elements in the arrays.

------------ please help - im a math major required to take 1 computer course and i really dont understand this stuff....

thanks

1 Answer

Relevance
  • Dan
    Lv 4
    7 years ago
    Favorite Answer

    Here's one way to do that. There are others.

    /*********************************************************************************

    * 1). write the definition of the function inputArray that prompts the user to

    * input 20 numbers and stores the numbers into an array called alpha.

    *

    ***********************************************/

    #include <iostream>

    /*!< Preprocessor expansion macro for MaxSize controls array elements, for loop.*/

    #define MaxSize 5

    using namespace std;

    /*!< Function declaration of inputArray.*/

    void inputArray(double *target);

    int main()

    {

    double alpha[MaxSize];

    double *alpha_ptr = &alpha[0];

    cout << "Please Enter Numbers as directed below." << endl;

    cout << endl;

    inputArray(alpha_ptr);

    return 0;

    }

    void inputArray(double *target)

    {

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

    {

    cout << "Please enter a number for alpha array element " << i << ": ";

    cin >> *(target+i);

    }

    }

    You're a math student, right? Well, an array in computer programming is analogous to a set in math. It is called an array because the numbers are stored in contiguous memory allocation spaces in the computer's RAM, so it forms an array of elements. Not a big deal to declare in C++. Here's how:

    '(data type) (variable name) [number of elements]; You address any specific element after that as (variable name) [element n]; for direct access. I used a pointer access to do the for loop in this little example. You'll get to those, but take my advice and start reading on pointers, pass by reference and pass by value right now. Perhaps the best thing I can do is to say that programs model problems and solutions in a psuedo-mathematical world, the computer. Think of it as an advanced electronic abacus. That's all it really does, for real, we just 'taught it a lot of tricks and how to send the answers to a screen'.

    OOPS! Almost posted the wrong thing to do for question 2. You'll need to access all three arrays, but I'd recommend the first two in a different way. I wangled some stuff around so I could compile and test, and you'll need to change both programs to run with a different number of elements, but the basic principles are here.

    #include <iostream>

    /*!< Preprocessor expansion macro for MaxSize controls array elements, for loop.*/

    #define MaxSize 5

    using namespace std;

    /*!< Function declaration of inputArray.*/

    void calcArray(double ar1[], double ar2[], double *store);

    int main()

    {

    //double highTemp[MaxSize], lowTemp[MaxSize];

    double highTemp[] = {69,72,66,45,71};

    double lowTemp[] = {31,46,29,12,34};

    double tempRange[MaxSize];

    double *tR_ptr = &tempRange[0];

    calcArray(highTemp, lowTemp, tR_ptr);

    return 0;

    }

    void calcArray(double ar1[], double ar2[], double *store)

    {

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

    {

    *(store + i) = (ar1[i] - ar2[i]);

    /*!< This line just puts what has just been stored on screen as a check.*/

    cout << *(store + i) << endl;

    }

    }

    Source(s): Me, myself and I.
Still have questions? Get your answers by asking now.