Question

C++ programming please Write a program that will display random numbers in order from low to...

C++ programming please

Write a program that will display random numbers in order from low to high.

1. Declare an integer array of size 100.
   Ask the user how many numbers to work with (n).
   It can be less than 100.

2. Generate random numbers from 10 to 50.

3. Write the random numbers to an output file named random.dat.
   Close the file.

4. Open random.dat for input and read the data values into your array.
   Pass your array to a function named bubbleSort, to sort them.
   Gordon will show you the bubble-sort algorithm.

5. Write the sorted array to an output file named ordered.dat. 

Our Bubble sort algorithm.


thank you 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

In case of any query do comment. Please rate answer. Thanks

Code:

#include<iostream>

#include<fstream>

#include<cstdlib>

#include<ctime>

using namespace std;

//bubble sort program

void bubbleSort(int numberArray[], int count)

{

    int i, j,temp;

    for (i = 0; i < count-1; i++)

    {

        for (j = 0; j < count-i-1; j++)

        {

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

            {

                temp = numberArray[j];

                numberArray[j] = numberArray[j+1];

                numberArray[j+1]= temp;

            }

        }

    }

}

//main driver program

int main(){

   

    int numberArray[100], number, count;

    ofstream outFile;

    ifstream inFile;

   

  //ask the user for number of integers

    std::cout << "Enter how many numbers (less than 100): ";

    cin >> count;

   

    //if count > 100 then return

    if(count > 100)

    {

        std::cout << "Number should not be greater than 100" << std::endl;

        return -1;

    }

  

   //open file for reading

   outFile.open("random.dat");

   //seeding the srand with time

   srand (time(0));

   //generate random numbers between 10 to 50

   for (int i = 0; i < count; i++) {

       

        number = rand() % 40 + 11;

        outFile << number << endl;

   }

   

    //close the file

    outFile.close();

   

    //open the same file for reading into array

    inFile.open("random.dat");

   int index =0;

    while(inFile){

        inFile >> numberArray[index];

        index++;

    }

   

    //close the input file

    inFile.close();

    //call bubble sort function

    bubbleSort(numberArray,count);

   

    outFile.open("ordered.dat");

    //write it to outFile

    for (int i = 0; i < count; i++) {

        outFile << numberArray[i] << endl;

    }

   

    return 0;

}

========screen shot of the code ========

Output:

Add a comment
Know the answer?
Add Answer to:
C++ programming please Write a program that will display random numbers in order from low to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program with a single-dimension array that holds 11 integer numbers and sort the...

    Write a Java program with a single-dimension array that holds 11 integer numbers and sort the array using a bubble sort. Next, identify the median value of the 11 integers. Here are the steps your program must accomplish. algorithm (either flowchart or pseudocode) that you will use to write the program Step 1. Create an Place the algorithm in a Word document. 6. Ste the Step 2. Code the program in Eclipse and ensure the following steps are accomplished. 1....

  • does anyone know how to do this? C++ Your task for this assignment is to use...

    does anyone know how to do this? C++ Your task for this assignment is to use C++ language to implement an insertion sort algorithm. 1. Implement an insertion sort with an array to sort 10 arbitrary numbers using the C++ programming language. The program initializes an array with 10 random 3-digit positive integers. The program then sorts the numbers in the array using the insertion sort algorithm. 2. The program displays the original values in the array before the sort...

  • Chapter 7 Exercise 18, Introduction to Java Programming, Tenth Edition Y. Daniel Liang. Please write your...

    Chapter 7 Exercise 18, Introduction to Java Programming, Tenth Edition Y. Daniel Liang. Please write your own code. 7.18 (Bubble sort) Write a sort method that uses the bubble-sort algorithm. The bubblesort algorithm makes several passes through the array. On each pass, successive neighboring pairs are compared. If a pair is not in order, its values are swapped; otherwise, the values remain unchanged. The technique is called a bubble sort or sinking sort because the smaller values gradually “bubble” their...

  • C programing Write a program to sort numbers in either descending or ascending order. The program...

    C programing Write a program to sort numbers in either descending or ascending order. The program should ask the user to enter positive integer numbers one at a time(hiting the enter key after each one) The last number entered by the user should be -1, to indicate no further numbers will be entered. Store the numbers in an array, and then ask the user how to sort the numbers (either descending or ascending). Call a function void sortnumbers ( int...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program...

    In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides. Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count...

  • C++ Search & Sort

    Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides.  Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array.  Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define...

  • This program is in C++ Write a program that validates charge account numbers read in from...

    This program is in C++ Write a program that validates charge account numbers read in from the file Charges.txt following this assignment (A23 Data). Use a selection sort function on the array to sort the numbers then use a binary search to validate the account numbers. Print out the sorted list of account numbers so you can see the valid ones. Prompt the user to enter an account number and the validate that number. If the account number entered is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT