C# help
SebiDragu (1)

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

namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
math1();
Console.WriteLine("Hello, world!");
}
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();
    }

//First I wanna type "5" for example to set 5 elements and then add them and in final I want them all to show reversed.. Can you help me please?
}
}

You are viewing a single comment. View All
heyitsmarcus (287)

Hi SebiDragu,

I made a repl with your code and then edited it and added comments into where I edited. https://repl.it/@heyitsmarcus/C-Numere-Problem

Please check out the repl link and look at the comments as to what I changed and why I changed it. If this doesn't solve your problem, please provide more clarification into what you're doing.

If this solves your problem, please mark my answer as the accepted answer.

SebiDragu (1)

@heyitsmarcus thank you! I also wanted to add manually numbers. For example (31, 23, 44, 54) and the reverse this like (54, 44, 23, 31).. that's something I couldn't understand

heyitsmarcus (287)

@SebiDragu So, you can add those numbers at any specified index that is within the realm of the integer array. Let me explain: let's say I choose the number 5 in the program. That means that numere is created with 5 indices that always start at 0. You just need to specify the index to which to assign them.

So, if you want the first one to be 31 then you would put:

numere[0] = 31;
//Set the next one to be 23
numere[1] = 23;
//And so on...up until index 4 since in this example we only created 5 indices

Or you can do this when you first create the array but you'll either want to leave off the number of indices to create or add in default values to fill the array up:

//creates numere with 4 specified indices
int[] numere = new int[] { 31, 23, 44, 54 };
//or let's create numere with n=5 (but we have to specify an additional value this time to make it 5)...the number of values specified HAS to match the length of the array
//This would generally not be good since we don't know what n would be
int[] numere = new int[n] { 31, 23, 44, 54, 0 };

I hope that makes more sense.

SebiDragu (1)

@heyitsmarcus Thank you so much, Marcus! That's exactly what I needed. I gotta do a lot of practice now.. thanks again!!

heyitsmarcus (287)

@SebiDragu You are very welcome! Good luck, and please if you have more troubles, just post on this Ask forum. I tend to check it a few times a day, and I'm sure there are other C# developers on here somewhere if I miss it. Good luck!