Home > Enterprise >  How do I change the odd numbers to even in an array? C
How do I change the odd numbers to even in an array? C

Time:11-20

For this assignment, I have to write a program that removes the odd numbers from an array and replaces them with even numbers. The array must have 10 elements and be initialized with the following numbers: 42, 9, 23, 101, 99, 22, 13, 5, 77, 28.

These are the requirements:

  1. Must use the values provided in my array.
  2. Print the original array to the console.
  3. Identify any odd values in the array and replace them with even values.
  4. Display the updated array to the console.

This is the output I am going for:

The original array is: 42 9 23 101 99 22 13 5 77 28
Your even number array is: 42 18 46 202 198 22 26 10 154 28

I'm super new to programming, so this concept is difficult for me to grasp, so if someone could give guidance that would mean the world.

This is what I have so far

#include <iostream>
using namespace std;


int main()
{
    int const size = 10;
    int values[size] = { 42, 9, 23, 101, 99, 22, 13, 5, 77, 28 };

    for (int i = 0; i < size; i  )
    {
        if (values[i] % 2 != 0)
        {
            cout << (values[i] * 2) << endl;
    
        }


    }
    

    return 0;
}

output

It's multiplying the odd numbers, which is want I want, but not each one in their own line. Also the new even numbers need to be along with the original even numbers.

CodePudding user response:

The transformation itself seems pretty simple. There are a number of ways to do the job. Here's one possibility:

std::transform(b, e, b, [](auto v) { return v * ((v & 1)   1); });

Just as a fair warning: if you turn that in for homework, whoever's teaching the class is probably going to ask some deep and searching questions about how it works, and start to get really suspicious if you don't have answers showing equally deep understanding.

CodePudding user response:

You mentioned that you are somehow new to programming. So, we need to adapt our answer to that fact. You will not yet know all the existing functions in the standard library. So, let us come up with a very easy solution.

Obviously we need to frist print the original unmodified date from the array. So, we will output the initial text and then iterate over all the slements in the array and output them as is.

Next, we show the second label string and then iterate again over all elements in the array. We the check for each value, if it is odd. If it is, then we print the double of this value, else, we show to unmodified value.

Please have a look at the below for one potential solution approach:

#include <iostream>
using namespace std;

int main() {
    // Size of our array
    int const size = 10;

    // Arry with some random sample data
    int values[size] = { 42, 9, 23, 101, 99, 22, 13, 5, 77, 28 };

    // Show the original array
    cout << "\nThe original array is: ";
    for (int i = 0; i < size;   i) cout << values[i] << ' ';

    // Now show check for odd numbers
    cout << "\nYour even number array is: ";

    // Show result
    for (int i = 0; i < size;   i)
        if (values[i] % 2 != 0)
            cout << (values[i] * 2) << ' ';
        else 
            cout << values[i] << ' ';
    cout << '\n';
}
  • Related