locked
I need to store 4 integers in 1 Array row for each inputdata number RRS feed

  • Question

  • If you can give me a hint please for this problem.

    So, if the user enter, in concole mode,  2 for example, I need to store 4 double integers in each line like this.

     int array 1 [] = new int[] (store the 4 integers in a single line from console input)

    int array 2 [] = new int[] (store the 4 integers in a single line from sonsoleinput).

    And so on for N numbers N rows. 

    Thank you! 


     

    Sunday, August 30, 2020 7:56 PM

Answers

  • The numbers are like this : 100.00  150.00 10000.00 10000.00 in a single row with 4 numbers a row.

    Is there a way to make a double dimension array with N rows that mach for user input number.

    Here's an example of creating a 2-dim array with a variable number of rows
    and a fixed number of columns:

    static void Main(string[] args)
    {
        int rows = 0;
        do
        {
            Console.Write("Enter the number of rows: ");
            int.TryParse(Console.ReadLine(), out rows);
        } while (!(rows > 0));
    
        double[,] matrix = new double[rows, 4];
    }
    

    - Wayne

    • Marked as answer by keeponfalling Monday, August 31, 2020 11:42 AM
    • Unmarked as answer by keeponfalling Monday, August 31, 2020 8:26 PM
    • Marked as answer by keeponfalling Tuesday, September 1, 2020 6:29 AM
    Monday, August 31, 2020 10:38 AM

All replies

  • Have the user enter value separated by comma or your choice

    Add this class to your project

    public static class NumericArray
    {
        /// <summary>
        /// Convert all values in array to int array where non int
        /// values will be set to the default value.
        /// </summary>
        /// <param name="sender">string array</param>
        /// <returns>All elements in array as int</returns>
        public static int[] ToIntegerPreserveArray(this string[] sender)
        {
    
            var intArray = Array.ConvertAll(sender, (input) =>
            {
                int.TryParse(input, out var integerValue);
                return integerValue;
            });
    
            return intArray;
    
        }
    }

    Code

    using System;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var input = Console.ReadLine();
                if (input.Contains(","))
                {
                    var results = input.Split(',').ToIntegerPreserveArray();
                    Console.WriteLine();
                }
            }
        }
    }
    

    If non-int value are entered they result to 0.

    See also https://github.com/karenpayneoregon/cs-language-extension-basics/tree/master/ExtensionsLibrary


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, August 30, 2020 10:42 PM
  • If you can give me a hint please for this problem.

    So, if the user enter, in concole mode,  2 for example, I need to store 4 double integers in each line like this.

     int array 1 [] = new int[] (store the 4 integers in a single line from console input)

    int array 2 [] = new int[] (store the 4 integers in a single line from sonsoleinput).

    And so on for N numbers N rows.  

    What do you mean by "double integer"? A double is not an integer. It is a
    floating point non-integral number. Are you inputting integers or doubles?

    You stipulate 4 integers in your subject and text, but then you say:

    "And so on for N numbers N rows."

    Is the number of numbers for each row fixed at 4 or is it variable?

    You have asked for "a hint". I assume that by this you are NOT looking for 
    code solutions. Is this a course assignment? If it is, we should not be
    providing complete code for the task but we can provide guidance. For that
    you need to describe exactly what part of the task you need help with, and
    post the code that YOU have written so far.

    - Wayne

    Monday, August 31, 2020 12:57 AM
  • The numbers are like this : 100.00  150.00 10000.00 10000.00 in a single row with 4 numbers a row.

    Is there a way to make a double dimension array with N rows that mach for user input number.

    Yes, it is a course assignment, and I just receive this problem. I din't manage to write any viable code because I didn't know exactly how to add array rows that are equal with input user number. That's why I'm looking for a hint in order to start and solve the problem.

    Thank you! 



    Monday, August 31, 2020 9:50 AM
  • The numbers are like this : 100.00  150.00 10000.00 10000.00 in a single row with 4 numbers a row.

    Is there a way to make a double dimension array with N rows that mach for user input number.

    Here's an example of creating a 2-dim array with a variable number of rows
    and a fixed number of columns:

    static void Main(string[] args)
    {
        int rows = 0;
        do
        {
            Console.Write("Enter the number of rows: ");
            int.TryParse(Console.ReadLine(), out rows);
        } while (!(rows > 0));
    
        double[,] matrix = new double[rows, 4];
    }
    

    - Wayne

    • Marked as answer by keeponfalling Monday, August 31, 2020 11:42 AM
    • Unmarked as answer by keeponfalling Monday, August 31, 2020 8:26 PM
    • Marked as answer by keeponfalling Tuesday, September 1, 2020 6:29 AM
    Monday, August 31, 2020 10:38 AM
  • Thank you again, it makes sense for me now  :) 
    Monday, August 31, 2020 11:42 AM
  • I also manage to use this code to add lines 

    int rows = int.Parse(Console.ReadLine());

    int[,] newRows = new int[rows,4];

    The next problem is about how to add my long double values to lines : 10.00 10.00 10.00 10.00

    i try with a for loop but I have a out of range exception.

    for(int i=0; i<matrix.Length;i++)
                {
                    matrix[i, 4] = Console.ReadLine();
                }

    Or I need to use 2 for loops in the same time?

    Thanks




    Monday, August 31, 2020 8:29 PM
  • I also manage to use this code to add lines 

    int rows = int.Parse(Console.ReadLine());

    int[,] newRows = new int[rows,4];

    The next problem is about how to add my long double values to lines : 10.00 10.00 10.00 10.00

    i try with a for loop but I have a out of range exception.

    for(int i=0; i<matrix.Length;i++)
                {
                    matrix[i, 4] = Console.ReadLine();
                }


    The problem with that code is that if a non-integer is entered the Parse will
    throw an exception and your program will abort. No opportunity is given to
    re-enter a value.

    Also, you have created an array of integers. What good is that? How were
    you planning on using an array of integers to hold multiple floating point
    numbers for each row?

    Your for loop is using a 2-dim array named matrix. How does that relate to
    the array called newRows? Show *all* of the code in your program, not just
    snippets. Also, when posting code use the "Insert Code Block" feature of
    the forum editor.

    Note as well that array indexing starts at zero. So an array with a dimension
    of 4 will have a max index of 3. Valid indexes are 0,1,2,3.

    Additionally, the input from ReadLine is a *string* which has to be
    converted to a numeric value or values.

    I'm trying to avoid giving you all of the code for this assignment. You need
    to work through it yourself - with some (limited) guidance.

    Why did you unmark as Answer my earlier reply? It showed you how to create
    an array that will hold 4 numbers in each row, with the number of rows being
    input by the user. That answers the original question as described in the
    thread Subject.

    Each thread should be limited to ONE specific issue, not a series of issues
    encountered during the creation of a program. Threads should not become
    excessively long as each and every new problem is encountered and addressed.

    - Wayne

    Monday, August 31, 2020 10:58 PM

  • i try with a for loop but I have a out of range exception.

    for(int i=0; i<matrix.Length;i++)
                {
                    matrix[i, 4] = Console.ReadLine();
                }


    Note also that the Length property of a 2-dim array gives the *total* number
    of elements in *all* the dimensions of the Array. So for an array

    int[,] matrix = new int[2, 4];

    Length will be 8.

    To get the length of one dimension you need to use GetUpperBound, which
    returns the highest valid index for that dimension of the array.

    matrix.GetUpperBound(0) // = 3

    matrix.GetUpperBound(0)+1  // = 4, the length of row 0

    - Wayne

    Monday, August 31, 2020 11:18 PM

  • The next problem is about how to add my long double values to lines : 10.00 10.00 10.00 10.00

    ...

                    matrix[i, 4] = Console.ReadLine();
    ...


    Do you plan on entering all 4 values on one line of input?

    Or one value at a time?

    Your program logic will be different depending on which way you accept input.

    If entering all 4 values on one line, then you need to extract all of the
    values from the string retrieved from GetLine(). You can use the String Split
    method to do that.

    Then the strings for each value must be converted to floats or doubles.

    - Wayne

    Monday, August 31, 2020 11:31 PM

  • Why did you unmark as Answer my earlier reply? It showed you how to create
    an array that will hold 4 numbers in each row, with the number of rows being
    input by the user. That answers the original question as described in the
    thread Subject.


    I was thinking that it more easy for you to take a look at my thread.Thank you for your directions and I will open a new one with my code that I have so far.
    Tuesday, September 1, 2020 6:32 AM
  • I try with string.Split(' '); without succes, I will open a new thread about this problem.

    Thank you for your time.

    I open a new thread for this problem.



    Tuesday, September 1, 2020 6:34 AM