0

I'm attempting to construct an array of user defined information. For the life of me, I cannot figure out how to place the user's input into the array or how to disallow more than 5 values to be placed into the array?

4
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by DdoubleD
0

I would use a List<> instead of an array, because you can create a List<> and then add new elements to it.

string[] someArray = new[] { "Alpha", "Bacta", "Cata", "Dooku", "Ephem" };
List<string> xs = new List<string>();
foreach (string s in someArray) {
  if (xs.Length < 3) {
    xs.Add(s);
  }
}

There you can see one example of building a list with the first three elements of some other source of information. (That's a pretty bad example -- instead you'd want to end the for loop somehow immediately after you've reached three elements.)

0

I figured out the issues I was initially having. Now, for some reason that I cannot figure out, the call that I'm attempting to make from ArrayWork.cs to ArrayInformation.cs is not working. I've sat here for another hour this morning messing around with different things trying to figure out my problem...

//ArrayWork.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WorkingWith2Arrays
{
    class ArrayWork
    {
        static void Main(string[] args)
        {
            ArrayInformation arrayInfo = new ArrayInformation();

            arrayInfo.SetUpTheArrays();
        }
    }
}
//ArrayInformation.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WorkingWith2Arrays
{
    public class ArrayInformation
    {
        public void SetUptheArrays()
        {
            //declare arrays 
            int[] integerArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            float[] floatArray = new float[5];

            DisplayOddNumbers(integerArray);
            DisplayNegativeNumbers(floatArray);
        }


        public void DisplayOddNumbers(int [] integerArray)
        {
            foreach (int value in integerArray)
            {
                if (value % 2 == 1)
                {
                    Console.WriteLine("Even numbers from integerArray:");
                    Console.Write("   " + value);
                    Console.Write("  {0}\n", value);
                }
            }
        }


        public void DisplayNegativeNumbers(float [] floatArray)
        {
            //copy floatArray
            float[] floatArrayCopy = floatArray;
            int floatCounter = 0;

            //prompt user for numbers
            do
            {
                Console.WriteLine("Please input 5 numbers.  (Press Enter to input each number.)");
                floatArray[0] = Convert.ToSingle(Console.ReadLine());

                //increment floatCounter
                floatCounter = floatCounter + 1;
            }
            while (floatCounter < 5);

            //check for negative numbers
            foreach (float value in floatArrayCopy)
            {
                if (value < 0)
                {
                    Console.WriteLine("The number of " + value +
                        " is a negative number.");
                }
                else
                {
                    Console.WriteLine("No negative numbers were entered in the floatArray");
                }
            }
        }
    }
}
0

You did not initialize your array:

public void SetUptheArrays()
        {
            //declare arrays 
            int[] integerArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            float[] floatArray = new float[5];

// initialize
floatArray[0] = 100.99;
floatArray[1] = 324.333354;
//...            
floatArray[4] = 0932409.3; // last element of this array
 
            DisplayOddNumbers(integerArray);
            DisplayNegativeNumbers(floatArray);
        }

Edited by DdoubleD: syntax issue

1

Set as last statement in your Main method something like Console.ReadKey(); otherwise your Console window will vanish into oblivion...
Might be a typo but in Main you are calling SetUpTheArrays in your class this method is defined as SetUptheArrays.
Find your coding style a bit weird, but fine with me we are luckily still rocking I a free world;)

Votes + Comments
lol
0

free world?

LOL

You also need to probably use your floatCounter as your index. Then, you can probably disregard intializing as I suggested earlier ;):

floatArray[floatCounter] = Convert.ToSingle(Console.ReadLine());

Also, you copy the floatArray, but I don't see that you use it... EDIT: Nevermind this statement

Edited by DdoubleD: misread the statement

0

Set as last statement in your Main method something like Console.ReadKey(); otherwise your Console window will vanish into oblivion...
Might be a typo but in Main you are calling SetUpTheArrays in your class this method is defined as SetUptheArrays.
Find your coding style a bit weird, but fine with me we are luckily still rocking I a free world;)

Man, I was hit-n-miss all over this one. Glad you spotted that ddanbe. ;)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.