?
Solved

Create random number and modify them in array

Posted on 2010-11-08
2
Medium Priority
?
288 Views
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.
0
Comment
Question by:rcham68
[X]
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
2 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 2000 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)
            {
                Console.WriteLine(item);
            }

            //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;
                            break;
                        case 2:
                            arr[i + 1] = 3;
                            break;
                        case 3:
                            arr[i + 1] = 1;
                            break;
                    }
                }
            } 
            Console.WriteLine();

            foreach (int item in arr)
            {
                Console.WriteLine(item);
            }

Open in new window

0
 

Author Closing Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

718 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