Question

C++ Look the source code and, 1. the function findMaxVisits is empty and one of its...

C++

Look the source code and,

1. the function findMaxVisits is empty and one of its parameters does not behave as it should. supply the missing statements and repair the damaged parameter. do not use any non-local constants or variables.

2. the fill function has some errors. repair the function without changing its parameter list. do not use any non-local constants or variables.

3. the show function has some errors. repair the function without changing its parameter list. One error is a compiler error and one error is a subtle logic error that affects the appearance of the function's output. repair the function without changing its parameter list. do not use any non-local constants or variables.

4. the main function has one and only one logic error. find the error and fix the error.

#include <ctime>

#include <fstream>

#include <iomanip>

#include <iostream>

#include <random>

#include <string>

//standard library declarations

using namespace std;

int findMaxVisits(int a[], int size, int maxrow)

{

/*

the function returns the maximum element in the array and the position of the maximum elements in the array

use a return statement to return the maximum element in the array

use maxrow to return the position of the maximum element in the arrray.

*/

}

void show(int a[], int size, int fieldwidth)

{

   for (int row = 0; row < M; row++)

       cout << a[row];

   cout << endl;

}

void fill(int a[], int size, int fillvalue)

{

   for (int row = 0; row < M; row++)

       a[row] = fillvalue;

}

const int M = 16;

default_random_engine e(static_cast<unsigned>(time(NULL)));

uniform_int_distribution<int> uM(0, M - 1);

int main()

{

   int visits[M];

   fill(visits, M, 0);

   int nvisits = 0;

   int maxrow;

   int nunvisited = M;

   int oldrow = uM(e);

  

   visits[oldrow]++;

   nvisits++;

   nunvisited--;

   cout << "number of visits: " << nvisits << " number of unvisited " << nunvisited << endl;

   int maxvisits = findMaxVisits(visits, M, maxrow);

   cout << "max visits: " << maxvisits << endl;

   show(visits, M, to_string(maxvisits).length() + 1);

   system("pause");

   system("cls");

   while(nunvisited > 0)

   {

       int newrow = uM(e);

       visits[newrow]++;

       nvisits++;

       cout << "number of visits: " << nvisits << " number of unvisited " << nunvisited << endl;

       maxvisits = findMaxVisits(visits, M, maxrow);

       cout << "max visits: " << maxvisits << endl;

       show(visits, M, to_string(maxvisits).length() + 1);

       system("pause");

       system("cls");

       nunvisited--;

       oldrow = newrow;

   }

   cout << "number of visits: " << nvisits << " number of unvisited " << nunvisited << endl;

   maxvisits = findMaxVisits(visits, M, maxrow);

   cout << "max visits: " << maxvisits << endl;

   show(visits, M, to_string(maxvisits).length() + 1);

   system("pause");

   return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <random>
#include <string>

//standard library declarations
using namespace std;

int findMaxVisits(int a[], int size, int &maxrow) {

    int max = a[0];
    maxrow = 0;
    for(int i=1; i<size; i++) {
        if(a[i] > max) {
            max = a[i];
            maxrow = i;
        }
    }

    return max;
}

void show(int a[], int size, int fieldwidth) {
   for (int row = 0; row < size; row++)
       cout << setw(fieldwidth) << a[row];
   cout << endl;
}

void fill(int a[], int size, int fillvalue) {
   for (int row = 0; row < size; row++)
       a[row] = fillvalue;
}

const int M = 16;

default_random_engine e(static_cast<unsigned>(time(NULL)));
uniform_int_distribution<int> uM(0, M - 1);

int main() {

   int visits[M];
   fill(visits, M, 0);

   int nvisits = 0;
   int maxrow;
   int nunvisited = M;
   int oldrow = uM(e);

   visits[oldrow]++;
   nvisits++;
   nunvisited--;

   cout << "number of visits: " << nvisits << " number of unvisited " << nunvisited << endl;

   int maxvisits = findMaxVisits(visits, M, maxrow);
   cout << "max visits: " << maxvisits << endl;
   show(visits, M, to_string(maxvisits).length() + 1);

   //system("pause");
   //system("cls");

   while(nunvisited > 0)  {
       int newrow = uM(e);

       visits[newrow]++;
       nvisits++;
       nunvisited--;

       cout << "number of visits: " << nvisits << " number of unvisited " << nunvisited << endl;

       maxvisits = findMaxVisits(visits, M, maxrow);
       cout << "max visits: " << maxvisits << endl;

       show(visits, M, to_string(maxvisits).length() + 1);

    //system("pause");
    //system("cls");

       oldrow = newrow;
   }

   cout << "number of visits: " << nvisits << " number of unvisited " << nunvisited << endl;

   maxvisits = findMaxVisits(visits, M, maxrow);

   cout << "max visits: " << maxvisits << endl;

   show(visits, M, to_string(maxvisits).length() + 1);

   //system("pause");

   return 0;

}

cout <<max visits: << maxvisits << endl; show(visits, M, to string(maxvisits).length) 1); //system(pause) number of visits: 7 nuber of unvisited 9 number of visits: 11 nurber of unvisited 5 maxvisits findMaxVisits(visits, M, maxrow); cout << max visits: くくmaxvisits くく endl; show(visits, M, to string(maxvisits).length) 1); θθ11121111θ!1261 001112111|θ112θ3 maxvisits findMaxvisits(visits, M, maxrow); cout << max visits: << maxvisits << endl; θθ11121111θ!1263 -ft-ft-ft-ft-ft-ft-ft-ft-ft-ft-ft-ft-ft-ft- 1 n+t tii in 1 ( n ㄑㄧ ou 3 8 5

Please upvote, as i have given the exact answer as asked in question. Still in case of any concerns in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
C++ Look the source code and, 1. the function findMaxVisits is empty and one of its...
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
  • a. Write a function named findmax() that finds and displays the maximum values in a two...

    a. Write a function named findmax() that finds and displays the maximum values in a two dimensional array of integers. The array should be declared as a 10 row by 20 column array of integers in main() and populated with random numbers between 0 and 100. b. Modify the function written above so that it also displays the row and column numbers of the element with the maximum value. I have this so far, and but it's only finding the...

  • Using C++, fix the following code so that (1) Change the student array storage part so...

    Using C++, fix the following code so that (1) Change the student array storage part so that the data is loaded from a file "students.txt". Use an appropriate data    termination flag. (2) Sort the student array in ascending order of GPA using insertion sort. Print the sorted array (3) Sort the student array in ascending order of ID using insertion sort. Print the sorted array (4) Illustrate Binary search for a particular ID that would be found and one...

  • Consider the following C++code snippet and what is the output of this program? # include<iostream> using...

    Consider the following C++code snippet and what is the output of this program? # include<iostream> using namespace std; void arraySome (int[), int, int); int main () const int arraysize = 10; int a[arraysize]-1,2,3,4,5, 6,7,8,9,10 cout << "The values in the array are:" << endl; arraySome (a, 0, arraySize) cout<< endl; system ("pause") return 0; void arraySome (int b[], int current, int size) if (current< size) arraySome (b, current+1, size); cout << b[current] <<""; a) Print the array values b) Double...

  • please help me fill out the blanks to the prewritten code for c++ 1. OBLJ UI...

    please help me fill out the blanks to the prewritten code for c++ 1. OBLJ UI SuuenLD, JUN, UML function find MultiMax that finds both the value ane handle ties, the find MultiMax function she are and assumed to be initialized alues should be as the returned values show 4. (30 pts) Use the space provided to write the function find Multima location of the maximum element of an array. To handle ties, the store the index locations in an...

  • In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to...

    In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to make the displayAnimal function anim parameter as an object variable, not a reference variable. Run the code and examine the output. What has changed? Polymorphic behavior is not possible when an object is passed by value. Even though printClassName is declared virtual, static binding still takes place because anim is not a reference variable or a pointer. Alternatively we could have used an Animal...

  • Hello, I have written a code that I now need to make changes so that arrays...

    Hello, I have written a code that I now need to make changes so that arrays are transferred to the functions as pointer variable. Below is my code I already have. #include<iostream> #include<string> #include<iomanip> using namespace std; //functions prototypes void getData(string id[], int correct[], int NUM_STUDENT); void calculate(int correct[], int incorrect[], int score[], int NUM_STUDENT); double average(int score[], int NUM_STUDENT); void Display(string id[], int correct[], int incorrect[], int score[], double average, int NUM_STUDENT); int findHigh(string id[], int score[], int NUM_STUDENT);...

  • C++ Code Pass By Reference Practice Write the following functions for basic array operations based on...

    C++ Code Pass By Reference Practice Write the following functions for basic array operations based on the function descriptions given below. Write the following functions for basic array operations based on the function descriptions given below. FindMinArray: This function will search an array of integers (passed to the function as a parameter) for its minimum value, then return that value. SumArray: This function will sum an array of integers (passed to the function through a parameter) then return that sum....

  • my code deosn't run and it doesn't show any error. what's wrong about it !! it...

    my code deosn't run and it doesn't show any error. what's wrong about it !! it should loads data from a text file into an array of structs and prints the array to the screen here is the code: #include <iostream> #include <string> #include <fstream> using namespace std; struct Company {    string Departmentname;    int Departmentnum; }; const int MAX = 20; int main() { ifstream File;    Company arrayofdepartment[MAX];    int Departmentcount ;      File.open("comapny.txt");       if...

  • In c++ programming, can you please edit this program to meet these requirements: The program that...

    In c++ programming, can you please edit this program to meet these requirements: The program that needs editing: #include <iostream> #include <fstream> #include <iomanip> using namespace std; int cal_avg(char* filenumbers, int n){ int a = 0; int number[100]; ifstream filein(filenumbers); if (!filein) { cout << "Please enter a a correct file to read in the numbers from"; }    while (!filein.eof()) { filein >> number[a]; a++; } int total = number[0]; for (a = 0; a < n; a++) {...

  • using the code above my instructor just want me to delete name from a list of...

    using the code above my instructor just want me to delete name from a list of students name. the function needs to be called delete_name. It's in c++. please no changing the code above just adding. this is pointer and dynamic array. // This is chapter 9 Pointers and Dynamic array #include< iostream> #include<string> using namespace std; //Gv //function declaration string* add_name(string*,int&); void display_names (string*,int); //main int main() //local var int size-3; string *students_names-new string[size]; //code cout<<"Enter "<<size< students names:"<<endl;...

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