Home > Net >  Using one array to populate a corresponding second array in C#
Using one array to populate a corresponding second array in C#

Time:05-02

I am taking a class in c# programming and cannot figure out how to do the following. I have spent hours researching and nobody else seems to have the same issues.

The question is:

Write a function named, evenOrOdd, that will accept three parameters. The first parameter is the integer array used in the above function. The second parameter is the string array from step 2 and the third parameter is an integer indicating the size of the two arrays. This function will perform the following tasks:

This function will loop through the first array, checking each value to see if it is even or odd.

For each item in the integer array, the function will then place the appropriate value, “even” or “odd”, in the corresponding position of the string array.

Hint: Using the modulus operator, (also called the modulo), to divide the number by 2 will result in either a remainder of 0 or 1. A remainder of 0 indicates an even number and a remainder of 1 indicates an odd number. The modulus operator for all of the languages in this class is %.

After calling both functions, print the maximum number determined by findMax as well as the array index position of the largest number. Next, the program will loop through the two arrays and print the integer from integer array followed by the corresponding “even” or “odd” value from the string array

I do not understand how to populate the second string array with "even" or "odd".

The two arrays should be something like this:

array1 [1,2,3,4,5,6,7,8,9,10]

then run through a loop to determine if the values are even or odd and then assign the values to the second array so it is something like this:

array2 [odd,even,odd,even,odd,even,odd,even,odd,even]

I am confused how I "link" these two arrays together so that it know index of array 1=index of array 2.

CodePudding user response:

You don't have to "link" the arrays together. You can use a variable which contains the current index and use it for both arrays. Like this:

for (int i = 0; i < array1.Length; i  ){
   //array1[i]...
   //array2[i] = ... 
}

This way, you can check if the number at index i in array 1 is even or odd and then modify the index i of array 2 accordingly.

Instead of array1.Length, you can also use the third argument of the method.

CodePudding user response:

It looks like a code challenge. I highly recommend you find your own way to understand and solve this kind of problem and the fundamental concepts behind it as well.

One way to code the EvenOrOdd method:

public void EvenOrOdd(int[] numbers, 
string[] natures, int size)
{
    for(int i=0; i < size; i  )
        if(numbers[i] % 2 == 0)
            natures[i] = "even";
        else
            natures[i] = "odd";
}

One wat to code FindMax Function:

public static (int MaxValue, int MaxIndex) FindMax(int[] numbers)
{
    int major = int.MinValue;
    int majorIndex = -1;
    
    for(int i=0; i < numbers.Length; i  )
        if(numbers[i] > major)
        {
            major = numbers[i];
            majorIndex = i;
        }
    
    return (major, majorIndex);
}

Check it in dotnetFiddle

  •  Tags:  
  • c#
  • Related