Question

For this program you create a program with multiple functions using C++ 1) You will need...

For this program you create a program with multiple functions using C++

1) You will need a function that creates an array of 100 random numbers that are generated between and including 20 and including 40. This must be a two dimensional array of 10 numbers in a row with a total of 10 rows.

2) A function that will store the array of random numbers in a data file called "rannum".

3) A function must then read the data file "rannum" and display the array.

4) A function that will take that array of random numbers and order the array from smallest to largest, repeated numbers are grouped together. The orered array is to be displayed as well.

5) A function that counts how many times each number was chosen and then displayed. As an example:

Number Qty

22 4

23 1

24 6

6) Display a graphical representation of the number of occurrences for each value. Should look like:

Number

20 xxx

21 xxxxxx

23 xxxx

24 xxxxxx

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

#include<bits/stdc++.h>
using namespace std;


void func1(int arr[10][10]){
   for(int i = 0; i < 10; ++i){
       for(int j = 0; j < 10; ++j){
           arr[i][j] = rand()%21 + 20;
       }  
   }
}

void func2(int arr[10][10]){
   ofstream myfile("rannum.txt");
   myfile.clear(); //clearing old content in file
   for(int i = 0; i < 10; ++i){
       for(int j = 0; j < 10; ++j){
           myfile << to_string(arr[i][j]);
           myfile << " ";
       }
       myfile << "\n";  
   }
   myfile.close();
}

void func3(){
   ifstream myfile("rannum.txt");
   string line;
   cout << "rannum array\n";
   if(myfile.is_open()){
           while(getline(myfile,line)){
                   cout << line << "\n";
           }
   }
   myfile.close();
}

void func4(int arr[10][10]){
   //Bubble sort to sort the 2D array
   for(int i = 0;i < 10 ;++i){
       for(int j = 0; j < 10 ;++j){
          
           int smallest = arr[i][j];
           int initial = j + 1;
           pair<int,int> p({i,j});
           for(int l = i; l < 10 ;++l){
               for(int m = initial; m < 10 ;++m){
                   if(arr[l][m] < smallest){
                       smallest = arr[l][m];
                       p = make_pair(l,m);
                   }
               }
               initial = 0;
           }
           swap(arr[i][j], arr[p.first][p.second]);
       }
   }
  
   //display
   cout << "Ordered array\n";
   for(int i = 0; i < 10; ++i){
       for(int j = 0; j < 10; ++j){
           cout << arr[i][j] << " ";
       }  
       cout << "\n";
   }
}

void func5(int arr[10][10], int freq[21]){
   //frequency of a random number calculation
   for(int i = 0; i < 10; ++i){
       for(int j = 0; j < 10; ++j){
           freq[arr[i][j] - 20] += 1;
       }  
   }
   //display
   cout << "Number Qty\n";
   for(int i = 0; i < 21; ++i){
       cout << i + 20 << " " << freq[i] << endl;
   }
}
void func6(int freq[21]){
   cout << "Number\n";
   for(int i = 0; i < 21; ++i){
       cout << i + 20 << " ";
       for(int j = 0; j < freq[i]; ++j){
           cout << "x";
       }
       cout << "\n";
   }
}

int main(){
   int arr[10][10];
   func1(arr); //array in cpp are passes by reference
   func2(arr); //write file
   func3(); // display file
   func4(arr);
   int frequency_array[21];
   func5(arr, frequency_array);
   func6(frequency_array);
   return 0;
  
}

SAMPLE OUTPUT:

Add a comment
Know the answer?
Add Answer to:
For this program you create a program with multiple functions using C++ 1) You will need...
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
  • 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...

  • *Using C++* You will create a program that uses a Critter class to move around a...

    *Using C++* You will create a program that uses a Critter class to move around a Grid, which is also a class. The Critter and the Grid classes will be in separate files. The Critter class will have a data member to count the number of moves made. It will also need data members to hold the current x and y coordinates. It will have a member function that randomly moves it one space in one of 4 directions. You...

  • Write a C program Design a program that uses an array to store 10 randomly generated...

    Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...

  • For this c++ assignment, Overview write a program that will process two sets of numeric information....

    For this c++ assignment, Overview write a program that will process two sets of numeric information. The information will be needed for later processing, so it will be stored in two arrays that will be displayed, sorted, and displayed (again). One set of numeric information will be read from a file while the other will be randomly generated. The arrays that will be used in the assignment should be declared to hold a maximum of 50 double or float elements....

  • Write a program in C that creates an array of 100 random numbers from 0-99. The...

    Write a program in C that creates an array of 100 random numbers from 0-99. The program sums the random numbers and prints the sum.  It then writes the numbers to a new file using open, close and write. Then looks in the current directory for files that match the pattern “numbers.XXXX”. For each file, open the file and read the file. You can assume that the file will contain 100 integers. Sum the integers. Print the filename and the sum...

  • 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...

  • Write a C++ program using function that takes a Two Dimensional array, number of rows and...

    Write a C++ program using function that takes a Two Dimensional array, number of rows and number of columns in that array as argument, and returns another 2D-array containing binary representation of those numbers.

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • C++ programming language Write a program that asks the user for a file name. The file...

    C++ programming language Write a program that asks the user for a file name. The file contains a series of scores(integers), each written on a separate line. The program should read the contents of the file into an array and then display the following content: 1) The scores in rows of 10 scores and in sorted in descending order. 2) The lowest score in the array 3) The highest score in the array 4) The total number of scores in...

  • with C++ You will create a program that uses a Critter class to move around a...

    with C++ You will create a program that uses a Critter class to move around a Grid, which is also a class. The Critter and the Grid classes will be in separate files. The Critter class will have a data member to count the number of moves made. It will also need data members to hold the current x and y coordinates. It will have a member function that randomly moves it one space in one of 4 directions. You...

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