multiply and add to vector of integer

Write a program which prompts the user to enter data into a vector of integers and assigns to another vector of integers the values of the first vector (modified by multiplying by 8 and incremented by 100) BUT IN REVERSE ORDER. Example: We enter into the first vector the values:

3   8   2   9   11  5  40 100 203 49

Then the second vector has the values:

(49*8 +100) (203*8 +100) (100*8 +100) (40*8 +100) (5*8 +100) (11*8 +100) (9*8 +100)
(2*8 +100) (8*8 +100) (3*8 +100) .

So this is the question being asked but yet all I can do in my code so far is input the integers and reverse order them but I can't figure out how to add the math into the coding.(adding 100 and times 8)

#include <iostream>
using namespace std;

int main(){
    int input[500], output[500], count, i;

    cout << "Enter number of elements in array\n";
    cin >> count;

    cout << "Enter " << count << " numbers \n";

    for(i = 0; i < count; i++){
        cin >> input[i];
    }

    // Copy numbers from inputArray to outputArray in 
    // reverse order 
    for(i = 0; i < count; i++){
        output[i] = input[count-i-1];
    }    

    cout << "Reversed Array\n";
    for(i = 0; i < count; i++){
        cout << output[i] << " ";
    }
    return 0;
}

3 answers

  • answered 2018-11-08 06:08 JeJo

    Well, you have not used the std::vector<int> anywhere in your code, rather, you have normal arrays with both of them with size 500. This was not the requirement.

    Read about std::vector<> and use them instead of those arrays, by allocating the size only what the user wants to have.(i.e, accordingly the user input count).

    Secondly, you could have done the calculations to each of the elements in input vector while you copying/ filling to output vector array.

    Here is an example code: DEMO HERE

    #include <iostream>
    #include <vector>
    
    int main()
    {
        std::cout << "Enter number of elements in array\n";
        std::size_t count; std::cin >> count;
    
        std::cout << "Enter " << count << " numbers \n";
        std::vector<int> input(count, 0);
        // input element
        for (int& element : input) std::cin >> element;
    
        std::vector<int> output; output.reserve(count); // reserve memory for elements
    
        // apply to the output vector: for that    
        // simply reverse iterate input array using the while loop, 
        // emplace back the calculated result to the output array
        while (count--)
        {
            output.emplace_back((input[count] * 8) + 100);
        }
    
        // print out the output
        std::cout << "Reversed Array\n";
        for (const int element : output)    std::cout << element << " ";
        return 0;
    }
    

    Output:

    Enter number of elements in array
    10
    Enter 10 numbers
    1 2 3 4 5 6 7 8 9 10
    Reversed Array
    180 172 164 156 148 140 132 124 116 108 
    

    That being said, you could have also done it by std algorithm function std::trasform. For that, iterate reversely the input vector in std::transform and using a lambda function(which calculate the necessary), and insert back to the vector output.

    SEE LIVE

    #include <algorithm> // std::transform
    
    auto doMath = [](int element) { return (element * 8) + 100; }; // lambda which does the calculation
    std::transform(input.crbegin(), input.crend(), std::back_inserter(output), doMath);
    

    or you can even trick the user by having only one vector array throughout the program where you will store user inputs directly after making the calculation like follows:

    SEE LIVE

    #include <iostream>
    #include <vector>
    
    int main()
    {
        std::size_t count; std::cin >> count;
        std::vector<int> vec; vec.reserve(count);
        // take the input and store the calculated value directly in the vec array
        while (count--)
        {
            int element; std::cin >> element;
            vec.emplace_back((element * 8) + 100);
        }
        return 0;
    }
    

  • answered 2018-11-08 06:20 evele

    When you copy the numbers from inputArray to outputArray in reverse order, you basically access to each and every element of inputArray and then put them in to the new array, so you can also change them in this step too:

    //take the element index (count - 1 - i) from inputArray, 
    //multiply by 8, then increment by 100, 
    //then assign output element index i equal to the result 
    
    cout << "Reversed Array\n";
    for(i = 0; i < count; i++){
        output[i] = ((input[count-i-1]) * 8) + 100;
    }
    

  • answered 2018-11-08 14:56 Pete Becker

    The actual calculation is a one-liner:

    std::vector<int> input(count);
    // populate input vector here...
    std::vector<int> result(count);
    
    // one-liner, formatted for easier readability
    std::transform(std::cbegin(input), std::cend(input),
        std::rbegin(result),
        [](int incoming) { return 8 * incoming + 100; } );
    

    The calls to std::cbegin and std::cend return iterators that point at the first element of input and past-the-end of input, respectively.

    The call to std::rbegin returns a reverse iterator into result; it runs from the end of the vector back toward the beginning.

    That last line is a lambda function -- it gets called with an int argument named incoming and returns the calculated value.

    The standard algorithm std::transform runs through the input range (std::cbegin(input) .. std::cend(input)) and for each element, applies the lambda function and stores the result in the output range (std::rbegin(output); no end iterator, because the algorithm terminates when the input range has been exhausted).

    That looks more complicated than the one-off solutions in other answers. But once you get used to using iterators and algorithms, this becomes a natural formulation, with the advantage of greater clarity and flexibility. For example, if your requirements changed and you needed to write the values in order instead of reverse order, you'd just change std::rbegin(result) to std::begin(result).