TeSteR
TeSteR

Reputation: 31

C++ integer array transforms and manipulation

So I have an assignment to

so here's how I tried it, but it works up to the 5th element after which it starts outputting some odd looking #s. What do i have to do???

#include <iostream>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <locale>
#include <algorithm>

using namespace std;

int main()
{
    srand((unsigned)time(NULL));
    int n, i, k = 0;
    int sum;
    int S = 0;
    cout << "What size array would you like:" << endl;
    cout << "n = ";
    cin >> n;
    int* A = new int[n];
    sum = 0;
    cout << "Input elements: ";
    for (i = 0; i < n; i++) cin >> A[i];
    for (int i = 0; i < n; i++)
    {
        cout << A[i] << " ";
    }
    cout << endl;
    cout << endl;
    for (int i = 0; i < n; i++)
    {
        if (A[i] % 2 == 0) {
            k = k + 1;
            sum += A[i];
        }
    }
    cout << "Ampount of even numbs: " << k << endl << endl;
    cout << "Sum of even numbs: " << sum << endl << endl;
    cout << "New array: " << endl;
    for (int i = 0; i < n; i++)
    {
        if (abs(A[i]) % 2 == 0)
        {
            for (int j = 0; j < i; j++)
            {
                S += A[j];
            }
            A[i] = S;
        }
    }

    for (int i = 0; i < n; i++)
    {
        cout << A[i] << " ";
    }
    cout << endl;
    cout << endl;
}

Upvotes: 0

Views: 278

Answers (3)

learnvst
learnvst

Reputation: 16195

Well, you said, C++ and "fun", so here is a more C++ way to skin this cat . .

#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
using namespace std;    

int main() {
    vector<int> v = {1,2,3,4,5,6};
    const auto isEven = [](int x){return x%2==0;};
    const auto print = [](const int& n) { cout << " " << n; };

    cout << "Before: "; for_each(v.begin(), v.end(), print); cout << endl;
    cout << "Num Even: " << count_if(v.begin(), v.end(), isEven) << endl;
    cout << "Sum Even: " << accumulate(v.begin(), v.end(), 0, [isEven](auto acc, int e) { return acc + (isEven(e) ? e : 0); }) << endl;
    // I'm not guru enough for Jarod42s c++20 voodoo 🤯nor is IDEONE 😉
    int b=0;
    vector<int> v2;
    for (auto& x : v) {
        isEven(x) ? v2.push_back(b) : v2.push_back(x);
        b+=x;
    }
    cout << "Transformed: "; for_each(v2.begin(), v2.end(), print); cout << endl;

    return 0;
}

Try here https://ideone.com/jDjoeB

Upvotes: 1

mfnx
mfnx

Reputation: 3018

You don't need a second loop to get S, you can do it in the first one. Also, don't forget deleting your array.

Design with pencil and paper before coding.

A solution to your problem could be something like this.

#include <iostream>

constexpr static size_t N = 10;

int main()
{  
    int* A = new int[N];

    /* fill array A */

    int sum(0), k(0), S(0);
    for (size_t i = 0; i < N; ++i)
    {
        S += A[i];
        if (A[i] % 2 == 0)
        {
            ++k;
            sum += A[i];
            if (i == 0) // nothing before index 0
                A[i] = 0;
            else
                A[i] = S - A[i]; // here's the trick
        } 
    }

    delete[] A; // don't forget this!

    return 0;
}

Upvotes: 0

AlexGeorg
AlexGeorg

Reputation: 1037

In your second-last loop you are changing every even number to their predecessors sum and then still iterate again over all numbers in the array. That means you are summing the sums! Either temporarily write the output to a new array, or better be smart and separately sum all numbers to the current point instead of re-looping once for every even number.

Upvotes: 0

Related Questions