none
Hello! I need help with a simple problem in C# RRS feed

  • Question

  • using System;

    namespace learning_platform
    {
        class Program
        {
            static void Main(string[] args)
            {
                math1();
            }
            public static void math1()
            {
                Console.WriteLine("Enter a number: ");
                int n = Int32.Parse(Console.ReadLine());
                int[] numere = new int [n];

                for(int i = 0; i <= numere.Length - 1; i++)
                {
                    n = numere[i];
                    Console.WriteLine("Number of elements is " + i.ToString());
                    for(int j = 0; j < i; j++)
                    {
                        Console.WriteLine("Elements are: " + j);
                    }
                }
                Console.ReadLine();
            }

    //I want it to ask me for a number at first. Let's say I pass "5" and then type more numbers so I can add them as elements. And in final to make it show reversed elements. Thank u so much!


    • Edited by Sebi996 Wednesday, May 29, 2019 2:29 PM
    Wednesday, May 29, 2019 2:01 PM

All replies

  • You're post is unreadable. Please edit it. Embed code using the <> button in the tool besides the HTML button. Remove any other custom color/font styles. When using copy'n'paste use a text editor like notepad/notepad++ to paste it first there, then copy it from there, this will remove HTML or other style attributes from text.

    The only thing I see, depending on your level, you would have separate procedures for collecting the input, processing the problem and creating the output.

    Wednesday, May 29, 2019 2:19 PM
  • it's done
    Wednesday, May 29, 2019 2:33 PM
  • Structure your approach.. this leads to better understandable code and program flows.

    namespace ConsoleCS
    {
        using System;
    
        public class Program
        {
            public static void Main(string[] args)
            {
                int[] numbers = ReadNumbers();
                int[] processedNumbers = ProcessNumbers(numbers);
                OutputNumbers("Numbers", numbers);
                OutputNumbers("Processed", processedNumbers);
    
                Console.WriteLine("\nDone.");
                Console.ReadLine();
            }
    
            private static int[] ReadNumbers()
            {
                return new int[] { 1, 9, 2, 8, 3, 7, 4, 6, 5 };
            }
    
            private static int[] ProcessNumbers(int[] numbers)
            {
                return numbers;
            }
    
            private static void OutputNumbers(string message, int[] numbers)
            {
                Console.WriteLine(message);
                for (int index = 0; index < numbers.Length; index++)
                {
                    Console.WriteLine($"{index} => {numbers[index]}");
                }
    
                Console.WriteLine(string.Empty);
            }
        }
    }

    Use Console.ReadLine() to read values entered by the user. The input is string. Test the input whether it is numeric or not. Then convert it to int by using int.TryParse(). Populate the result array in the ReadNumbers() function.

    In the ProcessNumbers() create your reversed array.

    Wednesday, May 29, 2019 2:45 PM
  • Try to write your program in plain text first.  Then I think you'll see where you went wrong.  You start off well enough, by asking for the number of elements and then creating an array with that many elements.  But then you never read anything else.  Your first loop should look something like:

        Console.WriteLine( "Enter " +n.ToString() + " numbers:" );
        for( int i = 0; i < numere.Length; i++ )
        {
            numere[i] = Int32.Parse(Console.ReadLine());
        }

    Now you have your array.  To print them out in reverse order, you can either use Array.Reverse and print them in order, or you can run a for loop in reverse order.  Note that the second loop should not be inside the first loop.  You just need one loop to get the numbers, then one separate loop to print the numbers.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Wednesday, May 29, 2019 5:45 PM
  • Hi Sebi996,

    Thank you for posting here.

    For your question, you want to make it show reversed elements in your code.

    I have modified it, you could try it.

     static void Main(string[] args)
            {
                math1();
    
            }
            public static void math1()
            {
                Console.WriteLine("Enter a number: ");
                int n = Int32.Parse(Console.ReadLine());
                int[] numere = new int[n];
                List<int> list = new List<int>();
                for (int i = 0; i <= numere.Length - 1; i++)
                {
                    n = numere[i];
                    Console.WriteLine("Number of elements is " + i.ToString());
                    for (int j = 0; j < i; j++)
                    {
                       
                        list.Add(j);
                      
                    }
                    list.Reverse();
                    foreach (var item in list)
                    {
                        Console.WriteLine("Elements are: " + item);
                    }
                    list.Clear();
    
                }
               
                Console.ReadLine();
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 30, 2019 2:00 AM
    Moderator