Create random number and modify them in array

Posted on 2010-11-08
Last Modified: 2012-05-10
Write a program to do the following.  First, create an array of 10 elements of  int type.  Use a random number generator to generate the 10 elements.  Each element is a random integer that is either 1, 2 or 3.  Once these 10 random elements are generated, display them in the console window.

      Next, examine the elements and modify them if necessary.  Our goal is that no two consecutive elements are the same.  In other words, every element must be different from the element right before it.  For example, if both the 4th and the 5th elements are 2, change the 5th element to something else.  The rules we use to modify the elements are as follows.  If there are two consecutive elements of 1, change the second 1 to 2.  If there are two consecutive elements of 2, change the second 2 to 3.  If there are two consecutive elements of 3, change the second 3 to 1.  After all the necessary changes are made, display all the 10 elements of the modified array.
Question by:rcham68
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 42

Accepted Solution

sedgwick earned 500 total points
ID: 34083656
here you go:
 Random rnd = new Random(0);
            int[] arr = new int[10];
            for (int i = 0; i < 10; i++)
                arr[i] = rnd.Next(1, 4);

            foreach (int item in arr)

            //after rules modification
            for (int i = 0; i < 9; i++)
                if (arr[i] == arr[i + 1])
                    switch (arr[i])
                        case 1:
                            arr[i + 1] = 2;
                        case 2:
                            arr[i + 1] = 3;
                        case 3:
                            arr[i + 1] = 1;

            foreach (int item in arr)

Open in new window


Author Closing Comment

ID: 34083930
Thanks for the help Did not remember how to assign a random number to the array

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question