Beginner asking for code-idea help!

droelth

New member
Joined
Mar 20, 2020
Messages
1
Programming Experience
1-3
Hey guys. So I am learning c# now, I have some experience in c++. But what I am gonna ask is more theory based question. So think of 6 numbers ( like an array), for example a0,a1,a2,a3,a4,a5. I wanna use last 4(doesnt matter the number here, has to be bigger than one tho :D) of them ( a2,a3,a4,a5). I will use these in some complicated equations and at last I will find a number lets say it is 32. I will put this 32 as a6. Then I will start again(for 5 time, I will use "for") with using my new a6 in last 4 of them which are (a6,a5,a4,a3). and it will go on.

So I firstly thought of arrays but I m not good at arrays and couldnt do it, one of my friend suggest me to use linked list which I have no information about it.

I secondly thought of making a "for" loop which will work 4 times, and it will call array's last digit from array.length, then loop will turn and it will call one less digit of array(if array.length is 4 it will call [b3]) and it will go 4 times. But then there was a problem in putting these array numbers into datatypes.. uhm let me say
[a0] = "phantom"
[a1]="raven"
[a2]="car"
[a3]="bird"
these will be called from array, I wanna put "phantom" to string x, and put raven to string y etc. to use them in my equations. (dont mind the string value, it will be numeric double value)

Conclution I couldnt pull my head from this jungle, please I need your help. Thanks.
 
Hello, welcome to the forums.

Please take a course in C# programming.

Read some simple updated material on C# basics before delving into areas of the language which you clearly don't understand. I honestly stopped reading here :
I secondly thought of making a "for" loop which will work 4 times, and it will call array's last digit from array.length, then loop will turn and it will call one less digit of array(if array.length is 4 it will call [b3]) and it will go 4 times.
call array's last digit
[a0] Uhhu really? No. That won't work.

But then there was a problem in putting these array numbers into datatypes.. uhm let me say
[a0] = "phantom"
[a1]="raven"
[a2]="car"
[a3]="bird"
Your approach is all wrong. What you're looking for here are Enums.... dear god, I'm not even going to explain. And I am seriously astonished at the mod whoever approved this post with such poor quality, with not one question asked. It's wrote in extremely poor calibre. And there is not even an ethical question being asked here.

What is your question?

I wanna put "phantom" to string x, and put raven to string y etc.
They are already an array; well of the broken kind.

and put raven to string y etc. to use them in my equations.

To use equations, generally implies maths will be involved, so just about what in this jargon will be calculable?
 
Here's the sort of thing that I would do:
C#:
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create an array to process.
            var array = new[] {1, 2, 3, 4, 5, 6};

            // Process the array multiple times.
            for (var i = 1; i < 5; i++)
            {
                // Process the last 4 elements of the array.
                ProcessArray(array, 4);
            }
        }

        static void ProcessArray(int[] array, int elementsToProcess)
        {
            // Get a subset of the elements of the specified length from the end of the array.
            var subset = array.Skip(array.Length - elementsToProcess).ToArray();

            // Perform the calculation on the subset and replace the last element with the result.
            array[array.GetUpperBound(0)] = PerformCalculation(subset);
        }

        static int PerformCalculation(int[] data)
        {
            // Perform the calculation.
            return data.Sum();
        }
    }
}
The Skip extension method allows you to skip over a certain number of items in any IEnumerable<T> and then process the rest. The complement of Skip is Take. The Main method here includes the loop and passes the array in for processing multiple times. You might choose to have the loop inside the ProcessArray method if you preferred. The processing consists of creating the subset, performing the desired calculation on that subset and then assigning the result back to the last element of the array. Depending on what your calculation looks like, it might be necessary to create a new array for the subset, as I have here. In my example though, that's not actually necessary, as my calculation would work just as well on an IEnumerable<int>:
C#:
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create an array to process.
            var array = new[] {1, 2, 3, 4, 5, 6};

            // Process the array multiple times.
            for (var i = 1; i < 5; i++)
            {
                // Process the last 4 elements of the array.
                ProcessArray(array, 4);
            }
        }

        static void ProcessArray(int[] array, int elementsToProcess)
        {
            // Get a subset of the elements of the specified length from the end of the array.
            var subset = array.Skip(array.Length - elementsToProcess);

            // Perform the calculation on the subset and replace the last element with the result.
            array[array.GetUpperBound(0)] = PerformCalculation(subset);
        }

        static int PerformCalculation(IEnumerable<int> data)
        {
            // Perform the calculation.
            return data.Sum();
        }
    }
}
 
John, I am all for helping people as you know, including beginners. But one of the reasons I left a majority of my previous websites such as Stack Overflow was because of the quality of the questions that were allowed to be asked were simply illogical, and or where not even questions at all.

The OP's topic doesn't have one question. And while I know english is not everyone's first language, this is also no excuse to write a poor topic with not one question asked. I'm happy to discuss this in PM if you prefer, but I'd like to see the standard of good ethical questions not degrade on this forum, and become just like one of those other terrible boards.

Your reply is rather courteous given the quality of the question, so I'm surprised you answered it with a code example at all, while it appears to me that our OP is a little mixed up regarding how things work in C# and it also appears they don't fully grasp how to use arrays properly or what would probably be a more appropriate structure to start building this project with, and it really wouldn't have hurt our OP to do some light research on the differences between the two languages.
 
Unfortunately post #3 missing something. My reading of what the OP is looking for is to do something like:
Given a list of items:
a0, a1, a2, a3, a4, a5

Take the last 4 items to get:
a2, a3, a4, a5

And do some operation on them and append it to the list to get:
a0, a1, a2, a3, a4, a5, a6

then repeat the above the above 4 times.

At the end, end up with a list:
a0, a1, a2, a3, a4, a5, a6, a7, a8, a9

So it is not a matter of replacing the last item of an array, but rather appending to it. For such a case, a List is a better data structure rather than an array.

I still don't quite understand the second part of the question about assignment of values. Maybe if I try reading it again on a full stomach it may make more sense.
 
Last edited:
Unfortunately post #3 missing something. My reading of what the OP is looking for is to do something like:
Given a list of items:
a0, a1, a2, a3, a4, a5

Take the last 4 items to get:
a2, a3, a4, a5

And do some operation on them and append it to the list to get:
a0, a1, a2, a3, a4, a5, a6

then repeat the above the above 4 times.

At the end, end up with a list:
a0, a1, a2, a3, a4, a5, a6, a7, a8, a9

So it is not a matter of replacing the last item of an array, but rather appending to it. For such a case, a List is a better data structure rather than an array.

I still don't quite understand the second part of the question about assignment of values. Maybe if I try reading it again on a full stomach it may make more sense.
You're correct. I missed that, when the OP referred to a6, that wasn't part of the original list. With that in mind, the second code snippet from my previous post would become this:
C#:
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a list to process.
            var list = new List<int> {1, 2, 3, 4, 5, 6};

            // Process the list multiple times.
            for (var i = 1; i < 5; i++)
            {
                // Process the last 4 items in the list.
                ProcessArray(list, 4);
            }
        }

        static void ProcessArray(List<int> list, int itemsToProcess)
        {
            // Get a subset of the items of the specified length from the end of the list.
            var subset = list.Skip(list.Count - itemsToProcess);

            // Perform the calculation on the subset and add the result as a new item.
            list.Add(PerformCalculation(subset));
        }

        static int PerformCalculation(IEnumerable<int> data)
        {
            // Perform the calculation.
            return data.Sum();
        }
    }
}
 

Latest posts

Back
Top Bottom