Question

In: Computer Science

In C# using a Console App, create an array that stores 20 integer values. Use the...

In C# using a Console App, create an array that stores 20 integer values. Use the random number generator to select an integer value between 0 and 10. Store the 20 random integers in the array.

Once the array has been created:

  1. Print the values in the array.
  2. Print the values in the array in reverse order.
  3. Sort and print the values in the array in ascending order.
  4. Sort and print the values in the array in descending order.
  5. Count the number of duplicate values in the array.
  6. Display the average of the values in the array.
  7. Display the average of the odd values in the array.
  8. Create a second array that stores 2 integer values.
    a) Use a random number generator to store an integer value between 1 and 5.
    b) Multiply the odd positioned values in the first array with the value in position 0 of the second array.
    c) Multiply the even positioned values in the first array with the value in position 1 of the second array.
    d) Display the new values of the first array.

Solutions

Expert Solution

using System;  

public class Exercise1  

{  

public static void Main()  

{  

public class RandomGenerator  

{  

private readonly Random _random = new Random();  

public int RandomNumber(int min, int max)  

{  

  return _random.Next(min, max);  

}  

}

int[] arr = new int[10];

var generator = new RandomGenerator();

int i;  

int count=0;

int average =0;

int sum=0;

int even=0;

int odd=0;

int evenaverage=0;

int oddaverage=0;

Console.Write("\n\nRead and Print elements of an array:\n");

Console.Write("Input 10 elements in the array :\n");  

for(i=0; i<10; i++)  

{  

Console.Write("element - {0} : ",i);

arr[i] = generator.RandomNumber(1, 20);

}

Console.Write("\nElements in array are: ");  

for(i=0; i<10; i++)  

{  

Console.Write("{0} ", arr[i]);  

}   

Console.Write("\nElements in array in reverse order are: ");  

for(i=10; i>0; i--)  

{  

Console.Write("{10} ", arr[i]);  

}

//Sorting the values  

        for (i = 0; i <=10; i++)  

        {  

            for (int j = 0; j <=9; j++)  

            {  

                if (a[j] > a[j + 1])  

                {  

                    int temp = a[j];  

                    a[j] = a[j + 1];  

                    a[j + 1] = temp;  

                }  

            }  

        }  

        Console.Write("Ascending Sort : ");  

        for (i = 0; i <10; i++)  

        {

            Console.Write(a[i]+" ");

}

Console.Write("Decending Sort : ");  

        for (i = 10; i >0; i--)  

        {

            Console.Write(a[i]+" ");

}

// Duplicate value count

for (i = 0; i < 10; i++)

{

for (j = i + 1; j < 10; j++)

{

// If duplicate element found then increment count by 1

if (arr[i] == arr[j])

{

count++;

break;

}

}

}

Console.WriteLine("\n Total number of duplicate elements found in array:"+count);

// Find Average of array

    for (int i = 0; i < 10; i++)
        {
            sum += arr[i];          

if (i % 2 == 0)

                even += arr[i];

  else

                odd += arr[i];

        }
    average = sum / 10; 
    Console.WriteLine("Average Of Array is : " + average);
    Console.ReadLine();
    evenaverage = even / 10;
    Console.WriteLine("Even Average Of Array is : " + evenaverage);      
    Console.ReadLine();
    oddaverage = odd / 10;
    Console.WriteLine("Odd Average Of Array is : " + oddaverage); 
    Console.ReadLine();

}

}


Related Solutions

IN C# lang Write a console app (review Module 5 for console app) that stores 3...
IN C# lang Write a console app (review Module 5 for console app) that stores 3 students test stores in 3 separate arrays as integers. Some did not not take all the tests, so they each have a their own length and we use separate arrays. Declare and initialize each array. Use meaningful names. Print each array as follows: Morgan 98, 72, 65 Bowie 15, 47, 35, 92, 94 Ananya 91, 68, 87, 75 Extra credit: Compute the average for...
Create a console app c#. Using a List, ask the user to enter all of their...
Create a console app c#. Using a List, ask the user to enter all of their favorite things. Once they are done, randomly pick a value from the list and display it.
In C++ Create a dynamic array of 100 integer values named myNums. Use a pointer variable...
In C++ Create a dynamic array of 100 integer values named myNums. Use a pointer variable (like ptr) which points to this array. Use this pointer variable to initialize the myNums array from 2 to 200 and then display the array elements. Delete the dynamic array myNums at the end. You just need to write part of the program.
Q#2 Write a C++ program that reads 10 integer values and stores them in an array....
Q#2 Write a C++ program that reads 10 integer values and stores them in an array. The program should find and display the average of the array elements and how many elements are below the average.
Q#1 Write a C++ program that reads n integer values and stores them in an array...
Q#1 Write a C++ program that reads n integer values and stores them in an array of maximum 20 integers. Read from the user a valid value of n (n should not exceed 20). After reading the n array elements find the maximum and minimum elements. Q#2 Write a C++ program that reads 10 integer values and stores them in an array. The program should find and display the average of the array elements and how many elements are below...
Create a console app with C# that uses a while loop to calculate the average of...
Create a console app with C# that uses a while loop to calculate the average of 3 test scores. Input integers, but use the appropriate average data type. Generally, you use a while to do something while a condition is true. You can use a while loop to execute a certain number of times. *While* this is not the best use of the while loop and another loop is generally more appropriate, it follows the pattern below set a counter...
In C++ Declare an integer array of size 20 and assign the array with 20 randomly...
In C++ Declare an integer array of size 20 and assign the array with 20 randomly generated integers in the range [1, 100]. Shuffle the data in the array so that all numbers smaller than 50 are put before the numbers greater or equal to 50. Note you are not allowed to create any other arrays in the program. Finally, print out the shuffled array. (25 points) #include<iostream> using namespace std; const int NUM = 20; int main() {      ...
using c language: create an array of the values of a sine wave. Include the math.h...
using c language: create an array of the values of a sine wave. Include the math.h header to use the sin floating point function. The function sin takes an argument of radians (not degrees). Make your array721 elements and initialize each element with 10 * sin(2*3.1416* (i/360.0) where i is the array index from 0 - 720. When done properly, the array should contain approximately 2 complete sine wave cycles. Similarly, create an array of 721 elements only this time...
Needs to be written in C# Console App! Create a base class BankAccount. Decide what characteristics...
Needs to be written in C# Console App! Create a base class BankAccount. Decide what characteristics are common for checking and saving accounts and include these characteristics in the base class. Define derived classes for checking and savings. In your design , do not allow the banking base account to be instantiated --only the checking and saving classes. Use a Program class to test your design.
Shopping Cart App (C#) Please create a C# Console (.NET Framework) application which reads from the...
Shopping Cart App (C#) Please create a C# Console (.NET Framework) application which reads from the user, the price of items in their shopping "cart". Be sure to tell the user how to stop (or ask them if they want to stop) entering prices. Items can be priced any positive number greater than zero. When the user has entered the prices for all items in their "cart", calculate the subtotal (the sum of all item prices). Next, check if the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT