Write a C# program to generate 2 Lists of random 5 elements each containing only binary numbers like 1 or 0 only such that there is only one 1 in the list in any random position and the rest four elements are zeroes. Create another list of 3 elements and such that there are random 1s or 0s. And concatenate these 3 Lists together to produce "one List" named "datablock".
Below is the solution;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ListConcate
{
class ListConcate
{
static void
Main(string[] args)
{
Random random = new Random(); //create a random object
List<int> list1 = new List<int>(); //create a
list1
List<int> list2 = new List<int>(); //create a list2
for(int i=0; i<5; i++) //loop 5 times
{
int r = random.Next(0, 2); //genearte random number 0 or 1
list1.Add(r); //add the random number to list1
}
for (int i = 0; i < 3; i++)
{
int r = random.Next(0, 2); //genearte random number 0 or 1
list2.Add(r); //add the random number to list2
}
List<int> datablock = list1.Concat(list2).ToList();
//concatinate list1 and list2
//print datablock
foreach (var x in datablock)
Console.WriteLine(x);
Console.ReadKey();
}
}
}
sample output:
0
1
1
0
1
1
1
1
Type2 solution:
Below is the solution:
using System;
using System.Collections.Generic;
using System.Linq;
namespace Calculator
{
class Calculator
{
static void
Main(string[] args)
{
Random random = new Random(); //create a random object
List<int> list1 = new List<int>(); //create a
list1
List<int> list2 = new List<int>(); //create a
list2
int n = 0;
bool set1 = true;
int setNum = 0;
//do while loop generate the random number 0-1 and only 1 one
time
do
{
int r = random.Next(0, 2);
if (set1 == true && r == 1)
{
set1 = false;
setNum = 1;
list1.Add(r);
n++;
}
else
{
if (r == 0)
{
list1.Add(r);
n++;
}
}
} while (n != 5 || setNum != 1); //loop until 1 time 1 and n is 5
times
Console.Write("\nList1: ");
foreach (var x in list1)
Console.Write(x);
for (int i = 0; i < 3; i++)
{
int r = random.Next(0, 2); //genearte random number 0 or 1
list2.Add(r); //add the random number to list2
}
Console.Write("\nList2: ");
foreach (var x in list2)
Console.Write(x);
List<int> datablock = list1.Concat(list2).ToList();
//concatinate list1 and list2
//print datablock
Console.Write("\ndatablock: ");
foreach (var x in datablock)
Console.Write(x);
Console.ReadKey();
}
}
}
sample output:
List1: 00001
List2: 101
datablock: 00001101
Write a C# program to generate 2 Lists of random 5 elements each containing only binary...
Write a C# program to generate Lists of random 5 elements each containing only binary numbers like 1 or 0 only such that there is only one 1 in the list in any random position and the rest four elements are zeroes. . For eg: (10000) or (01000) or (00100) or (00010) or (00001).
**C++ only, use standard library, no vectors Write a program to generate a list of 5000 random numbers between 1 and 10,000 stored in an array. Sorts: Print out the middle 50 numbers of the original list to show the results. Now sort the original numbers using bubble sort. Print out the number of swaps made. Now sort the original numbers using selection sort. Print out the number of swaps made. Now sort the original numbers using insertion sort. Print...
Given 2 lists, a and b, containing integers, not necessarily with the same length, Write a Python program(Python 3) that returns a list that contains only the elements that are common between the lists a and b (without duplicates).
Write a C++ program to define a class having a 4x4 matrix. Generate 32 random numbers 1-10, in an output.txt file. Fill the generated numbers in two such class objects (first 16 numbers in one matrix and next 16 numbers in another. Create member functions each for adding, subtracting and multiplication of matrices. Write the algorithm defining steps of your program.
In C... Write a program to simulate a pick-5 lottery game. Your program must generate and store 5 distinct random numbers between 1 and 9 (inclusive) in an array. The program prompts the user for: an integer random seed five distinct integers between 1 and 9 (which are stored in another array) The program then compares the two arrays to determine if they are identical. If the two arrays are identical, then the user wins the game. otherwise the program...
Write a Java program named BSTree.java that will: 1) Generate 20 random integer numbers ranging from 1-99. 2) Build a Binary Search Tree using this set of numbers. Write the add method. 3) After building the tree, display the data into three formats: prefix order, infix order, and postfix order. 4) Write a method to delete an element from the Binary Search Tree. First search the item in your TREE and then delete it.
Write a program in C to generate random numbers. The program recieves 4 items on activation through argv: 1. Name of the data file 2. Number of items for program to generate 3. Range of numbers 4. Seed of the random number generator Example run: ./rand datafile.txt 20 1000 3127 Program must convert all numbers to ints using atoi(), must save all parameters into variables, opens the data file to write the random numbers into it, program loops to generate...
Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three functionalities: Covert a binary string to corresponding positive integers Convert a positive integer to its binary representation Add two binary numbers, both numbers are represented as a string of 0s and 1s To reduce student work load, a start file CSCIProjOneHandout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the...
1. Create List 2. Create Random integers 3. Search List Write a program that gets random integers and populates a list, then searches the list 1. Write a program that creates an empty list 2. Populate the list with 100 random integers 3. After creating the list, print the length of elements in the list 4. Select a range of numbers as 10 – 20 5. Find out the number of times ‘15’ appears in the list and print it...
For this assignment, write a program that will generate random numbers in the range of 50-100 and count how many fall into particular ranges. Processing: The program should first seed the random number generator. This is done one time in the program. Use srand(0). Next, generate and display a series of 10 random numbers between 50 and 100. There are several ways to ensure that a random number falls between 50 and 100. One possibility is to use the following...