Question

In: Computer Science

Write a program that asks the user to enter an array of random numbers, then sort...

Write a program that asks the user to enter an array of random numbers, then sort the numbers (ascending order), then print the new array, after that asks the user for a new two numbers and add them to the same array and keep the array organization. (c++ ) (using while and do while loops)

Solutions

Expert Solution

#include<iostream>
#include<stdlib.h>
using namespace std;
//method for sort the array using selection sort
void sort(int arr[],int size)
{
    int i,j,t;
    //loop for selection sort
    i=0;
   while(i<size)
    {
       j=i+1;
       do //do...while loop for inner loop
       {
          if(arr[i]>arr[j]) //condition for swap
          {
             t=arr[i]; //logic for swap
             arr[i]=arr[j];
             arr[j]=t;
               }
               j++;
       }while(j<size);
       i++;
   }
}
//method to print the array
void print(int arr[],int size)
{
   int i;
   i=0; //initialize i to 0
   //loop to print the array elements
   while(i<size)
   {
       cout<<" "<<arr[i]; //print the array elements
       i++;
   }
}
//driver program
int main()
{
   int arr[1000],n,i,a,b;//declare the array
   //ask user for number of elements
   cout<<endl<<"Enter the number of elements to input";
   cin>>n;
   i=0; //initialize i to 0
   while(i<n) //loop will continue for n times
   {
      arr[i]= rand() % 100 +1; //generate a number between 1 to 100 and assign it to array
      i++;
   }
   //display the array elements before sort
cout<<endl<<"Before sort the array elements are : \n";
print(arr,n);
sort(arr,n); //call to sort() method.
   //display the array elements after sort
cout<<endl<<"After sort the array elements are : \n";
print(arr,n);
//ask user to input two more elements
cout<<endl<<"Enter two number s to add into the array";
cin>>a>>b;
//assign the frst number to array
arr[n]=a;
//assign second number to array
arr[n+1]=b;
n=n+2; //update the number of elements

cout<<endl<<"\n*******AFTER ADDING OF TWO ELEMENTS*****\n";
//display the array elements before sort
cout<<endl<<"Before sort the array elements are : \n";
print(arr,n);
sort(arr,n);//call to sort() method.
   //display the array elements after sort
cout<<endl<<"After sort the array elements are : \n";
print(arr,n);


  
  
}

output


Related Solutions

in C++, Write a program that asks the user to enter 6 numbers. Use an array...
in C++, Write a program that asks the user to enter 6 numbers. Use an array to store these numbers. Your program should then count the number of odd numbers, the number of even numbers, the negative, and positive numbers. At the end, your program should display all of these counts. Remember that 0 is neither negative or positive, so if a zero is entered it should not be counted as positive or negative. However, 0 is an even number....
Write a C program that asks the user to enter 15 integer numbers and then store them in the array.
Write a C program that asks the user to enter 15 integer numbers and then store them in the array. Then, the program will find the second largest element in array and its index without sorting the array. For example, In this array {-55,-2,1, 2, -3, 0, 5, 9, 13, 1, 4, 3, 2, 1, 0}, the second largest element is 9 [found at index 7].
Write a program that asks the user to enter an array of 8 characters then you...
Write a program that asks the user to enter an array of 8 characters then you have to check if characters ‘b’ or ‘a’ appears within the characters and replace them with ‘B’ or ‘A’. For example you enter “a m a l a a b d” The output should be “A m A l A A B d”
Write a C++ program that asks the user to enter a series of single-digit numbers with...
Write a C++ program that asks the user to enter a series of single-digit numbers with nothing separating them. Read the input as a C-string or a string object. The program should display the sum of all the single-digit numbers in the string. For example, if the user enters 2514, the program should display 12, which is the sum of 2, 5, 1, and 4. The program should also display the highest and lowest digits in the string. It is...
Write a python program that asks the user to enter a string containing numbers separated by...
Write a python program that asks the user to enter a string containing numbers separated by commas, e.g., s = '1.23,2.4,3.123', Your program should then calculate and print the sum of the numbers entered. Hint: you need to iterate over the string searching for the commas, i.e. their index. The first number is obtained by slicing between the start of the string and the index of the first comma. The second number is between the last comma and the next...
C++ Bubble Sort Write a program that ask user to enter 7 numbers and store that...
C++ Bubble Sort Write a program that ask user to enter 7 numbers and store that in array. Display that all numbers before and after performing Bubble sort. You must have to create new function with required parameter to perform Bubble sort. Sample Run :- Enter 1 number :- 1 Enter 2 number :- 5 Enter 3 number :- 7 Enter 4 number :- 45 Enter 5 number :- 90 Enter 6 number :- 6 Enter 7 number :- 55...
IN JAVA Write a complete program that asks the user to enter two real numbers from...
IN JAVA Write a complete program that asks the user to enter two real numbers from the console. If both numbers are positive print the product, if both numbers are negative print the quotient, otherwise print INVALID INPUT. Use a nested if; output should be to a dialog and console; use printf to format the console output (for real numbers specify the width and number of digits after the decimal). The output must be labeled. Follow Java conventions, indent your...
Design a modular program which asks the user to enter a list of numbers. The numbers...
Design a modular program which asks the user to enter a list of numbers. The numbers must be stored in an array. The program then finds the index of the first occurrence of the smallest element in the array and the last occurrence of the largest element in the array. The program displays the position and value of each of these items.
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
Design a complete program that asks the user to enter a series of 20 numbers. The...
Design a complete program that asks the user to enter a series of 20 numbers. The program should store the numbers in an array and then display each of the following data: I. The lowest number in the array II. The highest number in the array III. The total of the numbers in the array IV. The average of the numbers in the array *PYTHON NOT PSUEDOCODE AND FLOW CHART!!!!*
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT