Jump to content

C# insert elements in a specific place

MisterWhite

The problem with your code is that it doesn't make a correct correlation between the two arrays after the new element is inserted.

Let's take your own example.

Once i reaches 2, b = 100, so b[2]=100, then i gets incremented and b = a, so b[3] = a[3] = 4

We obviously missed a[2].

Didn`t think of that *facepalm*. I should really test my code in the future before I post it.

GPU: Gigabyte GTX 970 G1 Gaming CPU: i5-4570 RAM: 2x4gb Crucial Ballistix Sport 1600Mhz Motherboard: ASRock Z87 Extreme3 PSU: EVGA GS 650 CPU cooler: Be quiet! Shadow Rock 2 Case: Define R5 Storage: Crucial MX100 512GB
Link to comment
Share on other sites

Link to post
Share on other sites

I hate it when teachers tell me that my solution is bad. Today I had to do this exersize in school: http://codingbat.com/prob/p130788

Yeah, it might be a correct solution , but the teacher might have wanted you to do it in a different way. In your case , it was to experiment with conditional statements.

 

In OP's case, the teacher obviously wants to get the students involved with the algorithm behind inserting an element into an array, which implies shifting the array to the right only from a certain position.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

I hate it when teachers tell me that my solution is bad. Today I had to do this exersize in school: http://codingbat.com/prob/p130788

Exactly. I hate to do overcomplicated stuff when I can do it easier. I wish I started earlier with programming though - got interested half a year ago and I'm a noob in writing algorithms like the one @Nineshadow did. 

From salty to bath salty in 2.9 seconds

 

Link to comment
Share on other sites

Link to post
Share on other sites

Exactly. I hate to do overcomplicated stuff when I can do it easier. I wish I started earlier with programming though - got interested half a year ago and I'm a noob in writing algorithms like the one @Nineshadow did. 

I started 3 years ago, but I haven`t done much in the past year. I think I`ll try to make a simple program to download all images in the anime thread soon though, just so I always have a fitting image when I post something in there.

GPU: Gigabyte GTX 970 G1 Gaming CPU: i5-4570 RAM: 2x4gb Crucial Ballistix Sport 1600Mhz Motherboard: ASRock Z87 Extreme3 PSU: EVGA GS 650 CPU cooler: Be quiet! Shadow Rock 2 Case: Define R5 Storage: Crucial MX100 512GB
Link to comment
Share on other sites

Link to post
Share on other sites

Exactly. I hate to do overcomplicated stuff when I can do it easier. I wish I started earlier with programming though - got interested half a year ago and I'm a noob in writing algorithms like the one @Nineshadow did. 

This is the easy stuff broski.

Lol, broski. brah? brochan?

AU9d6uQ.jpg

 

Ok, enough.

 

Programming is quite interesting that way, but problem solving can be even more challenging. Getting the algorithm right is one thing, but optimizing it is as important. You might even need to implement another algorithm for a desired optimization.

 

Here's a simple one for you. Given a natural number P , find the smallest number N such as N! (factorial) has exactly P digits of 0 at the end.

Hint : binary search.

 

And don't worry, I only started programming last year.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

We should stop derailing this thread, xD.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

 

Check Nineshadow's pseudocode - it can give you a general understanding and you can build the program around it. :)

 

The list way is much simpler, but your teacher won't be happy with it.

Example:

int[] arr = {1,2,3,4,5};List <int> list = new List<int>(arr);list.Insert(2, 7); //insert "7" at the second positionforeach (var item in list){    Console.Write(item + " ");}

So here's the solution beneath the exercise, though i dont understand how that works, maybe explain? :)

                                        Console.Write("Enter the number of the city to modify: ");					int position = Convert.ToInt32(Console.ReadLine());					Console.WriteLine("Insert a new data at {0} position: ",						position);					amount++; // general amount of data in array					for (int i = (int)amount; i > position - 1; i--) // no idea whats here and down					{						cities[i] = cities[i - 1];					}					Console.Write("City name: ");					cities[position - 1] = Console.ReadLine();					Console.Write("Inhabitants: ");					peopleCount[position - 1] = Convert.ToInt32(Console.ReadLine());

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

 

So here's the solution beneath the exercise, though i dont understand how that works, maybe explain? :)

//ask for the postion at which the object should be inserted     Console.Write("Enter the number of the city to modify: ");     int position = Convert.ToInt32(Console.ReadLine());     Console.WriteLine("Insert a new data at {0} position: ",      position);     amount++; // general amount of data in array     //iterate backwards from the end of the array until you reach the wanted position     for (int i = (int)amount; i > position - 1; i--) // no idea whats here and down     {      //replace the object at i with the object before i      //this is to move all of the objects to the right      cities[i] = cities[i - 1];     }     //ask for the city name     Console.Write("City name: ");     //insert city name at the given position     cities[position - 1] = Console.ReadLine();         //ask for the inhabitants of the city     Console.Write("Inhabitants: ");     peopleCount[position - 1] = Convert.ToInt32(Console.ReadLine());

added some comments

GPU: Gigabyte GTX 970 G1 Gaming CPU: i5-4570 RAM: 2x4gb Crucial Ballistix Sport 1600Mhz Motherboard: ASRock Z87 Extreme3 PSU: EVGA GS 650 CPU cooler: Be quiet! Shadow Rock 2 Case: Define R5 Storage: Crucial MX100 512GB
Link to comment
Share on other sites

Link to post
Share on other sites

added some comments

This one is used to make the first letters(and every first letter after spaces) of the city's name into Capital letters, but isn't there a more simple way of doing so?

for(int i = 0;i < amount;i++)  {// First, the whole name to lower casestring lowerCaseName = cities[i].ToLower();// then, first letter to uppercasestring correctedName = lowerCaseName.Substring(0, 1).ToUpper()+ lowerCaseName.Substring(1);// and then the letters after a spacefor (int j = 1; j < correctedName.Length - 2; j++){if (correctedName[j] == ' ')correctedName = correctedName.Substring(0, j) + " " +correctedName.Substring(j + 1, 1).ToUpper() +correctedName.Substring(j + 2);} cities[i] = correctedName;}

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×