Question

In: Computer Science

c++: Exercise 1: Study the tutorial from Unit 6 on Sorting Arrays and Vectors. Write a...

c++:

Exercise 1:

Study the tutorial from Unit 6 on Sorting Arrays and Vectors.

Write a program that generates a sequence of 20 random values between 0 and 99 in an array, prints the sequence, sorts it, and prints the sorted sequence. Use the sort method from the C++ standard library. Do not add duplicate values to your array.  

Hint:

#include <algorithm>
#include "math.h"
using namespace std;
const int SIZE = 100;

while (numAdded < 100) {
val = floor(rand() % 100);  // Will generate values between 0 and 99
//  if unique add to array otherwise skip

}

//  Your unsorted array will look something like this. Use a loop, not a declaration to // add your values

{13, 7, 6, 45, 21, 9, 87, 99,...};
sort(arr);

// Sorted will be:

{0,1,2,3,....,99}

Exercise 2:

  Write a program that stores a list of countries: "Egypt", "Switzerland", "Argentina", "Spain", "Portugal", "Luxemburg", etc.

Initialize your array with a single statement. Then print out the array.

Use the sort function as before to sort the countries in alphabetical order.

Reprint your array.

Exercise 3:

Study the tutorial about vectors, if you haven't already. Implement exercises 1

and 2 using vectors. Then append an additional element to each list, print your

new lists and print and resort again.  

Solutions

Expert Solution

PART A -

CODE -

#include<iostream>

#include <algorithm>

#include "math.h"

using namespace std;

const int SIZE = 100;

int main()

{

    int numAdded = 0;

    int arr[100];

    while (numAdded < 100)

    {

        int val = floor(rand() % 100);  // Will generate values between 0 and 99

        bool found = false;

        // Checking if number generated is already in the array

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

        {

            if (arr[j] == val)

                found = true;

        }

        // if unique add to array otherwise skip

        if (!found)

        {

            arr[numAdded] = val;

            numAdded++;

        }

    }

    // Displaying the array before sorting

    cout << "\nArray before sorting:" << endl;

    for(int i=0; i<numAdded; i++)

        cout << arr[i] << " ";

    // Sorting the array

    sort(arr, arr+numAdded);

    // Displaying the array after sorting

    cout << "\nArray after sorting:" << endl;

    for(int i=0; i<numAdded; i++)

        cout << arr[i] << " ";

}

SCREENSHOTS -

CODE -

OUTPUT -

PART B -

CODE -

#include<iostream>

#include <algorithm>

using namespace std;

const int SIZE = 100;

int main()

{

    string countries[] = {"Egypt", "Switzerland", "Argentina", "Spain", "Portugal", "Luxemburg"};

    // Displaying the array before sorting

    int size = sizeof(countries) / sizeof(countries[0]);

    cout << "\nName of Countries before sorting:" << endl;

    for(int i=0; i<size; i++)

        cout << countries[i] << "   ";

    // Sorting the array

    sort(countries, countries + size);

    // Displaying the array after sorting

    cout << "\n\nName of Countries after sorting:" << endl;

    for(int i=0; i<size; i++)

        cout << countries[i] << "   ";

}

SCREENSHOT -

PART C -

1ST PART -

CODE -

#include<iostream>

#include <algorithm>

#include "math.h"

#include<vector>

using namespace std;

const int SIZE = 100;

int main()

{

    int numAdded = 0;

    vector<int> vec;

    while (numAdded < 100)

    {

        int val = floor(rand() % 100);  // Will generate values between 0 and 99

        bool found = false;

        // Checking if number generated is already in the vector

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

        {

            if (vec[j] == val)

                found = true;

        }

        // if unique add to vector otherwise skip

        if (!found)

        {

            vec.push_back(val);

            numAdded++;

        }

    }

    // Displaying the vector before sorting

    cout << "\nVector before sorting:" << endl;

    for(int i=0; i<numAdded; i++)

        cout << vec[i] << " ";

    // Sorting the vector

    sort(vec.begin(), vec.end());

    // Displaying the vector after sorting

    cout << "\n\nVector after sorting:" << endl;

    for(int i=0; i<numAdded; i++)

        cout << vec[i] << " ";

    

    // Appending a random integer between 0 to 99 to the list

    vec.push_back(floor(rand() % 100));

    cout << "\n\nVector after adding an element: " << endl;

    for(int i=0; i<vec.size(); i++)

        cout << vec[i] << " ";

    // Sorting the vector

    sort(vec.begin(), vec.end());

    // Displaying the final vector after sorting

    cout << "\n\nFinal Vector after sorting:" << endl;

    for(int i=0; i<vec.size(); i++)

        cout << vec[i] << " ";

}

SCREENSHOTS -

CODE -

OUTPUT -

2ND PART -

CODE -

#include<iostream>

#include <algorithm>

#include<vector>

using namespace std;

const int SIZE = 100;

int main()

{

    // INITIALIZING THE VECTOR.

    // REMEMBER THIS METHOD OF INITIALIZING VECTORS WORKS ONLY ON C++11 OR LATER VERSIONS.

    // FOR OTHER VERSIONS BEFORE C++11, YOU HAVE TO USE THE push_back() FUNCTION OF VECTORS TO ADD ELEMENTS TO THE VECTOR

    vector<std::string> countries = {"Egypt", "Switzerland", "Argentina", "Spain", "Portugal", "Luxemburg"};

    // Displaying the vector before sorting

    cout << "\nName of Countries before sorting:" << endl;

    for(int i=0; i<countries.size(); i++)

        cout << countries[i] << "   ";

    // Sorting the vector

    sort(countries.begin(), countries.end());

    // Displaying the vector after sorting

    cout << "\n\nName of Countries after sorting:" << endl;

    for(int i=0; i<countries.size(); i++)

        cout << countries[i] << "   ";

    // Adding a country name to the vector

    countries.push_back("Syria");

    // Displaying the vector after adding a country name

    cout << "\nName of Countries after adding a country name:" << endl;

    for(int i=0; i<countries.size(); i++)

        cout << countries[i] << "   ";

    // Sorting the vector

    sort(countries.begin(), countries.end());

    // Displaying the final vector after sorting

    cout << "\n\nFinal name of Countries after sorting:" << endl;

    for(int i=0; i<countries.size(); i++)

        cout << countries[i] << "   ";

}

SCREENSHOT -

If you have any doubt regarding the solution, then do comment.
Do upvote.


Related Solutions

Exercise 1: Study the tutorial from Unit 6 on Sorting Arrays and Vectors. Write a program...
Exercise 1: Study the tutorial from Unit 6 on Sorting Arrays and Vectors. Write a program that generates a sequence of 20 random values between 0 and 99 in an array, prints the sequence, sorts it, and prints the sorted sequence. Use the sort method from the C++ standard library. Do not add duplicate values to your array.   Hint: #include <algorithm> #include "math.h" using namespace std; const int SIZE = 100; while (numAdded < 100) { val = floor(rand() %...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 1: Largest and Smallest Vector Values Specifications: Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector. Create 3 functions in addition to your main function. One function should generate the...
using c++ 10. Sorting Orders Write a program that uses two identical arrays of eight integers....
using c++ 10. Sorting Orders Write a program that uses two identical arrays of eight integers. It should display the contents of the first array, then call a function to sort it using an ascending order bubble sort, modified to print out the array contents after each pass of the sort. Next the program should display the contents of the second array, then call a function to sort it using an ascending order selection sort, modified to print out the...
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and...
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and other files, loops, conditionals, data types, assignment.   Calculating fuel economy. This program will use exceptions and stream errors to make a robust application that gets the number of miles and gallons each time the user fuels their car. It will put those values into vectors. Once the user wants to quit enter values, it will calculate the fuel economy. Create GetMiles() function that returns...
This assignment is to give you practice using struts, arrays, and sorting. (Objective C++ and please...
This assignment is to give you practice using struts, arrays, and sorting. (Objective C++ and please have a screenshot of output) In competitive diving, each diver makes dives of varying degrees of difficulty. Nine judges score each dive from 0 through 10 in steps of 0.5. The difficulty is a floating-point value between 1.0 and 3.0 that represents how complex the dive is to perform. The total score is obtained by discarding the lowest and highest of the judges’ scores,...
Objectives:  Write classes in C++  Use dynamic arrays  Write and read from files...
Objectives:  Write classes in C++  Use dynamic arrays  Write and read from files 1. WriteaclassGradeBookcontainingthefollowing: Private attributes: - courseName: a string representing the name of the course. - nbOfStudents: an integer representing the number of students enrolled in the course. The number of students is greater than or equal to 5. - grades: a double dimensional array of integers representing the grades of Test1, Test2 and Final of every student. It should be a dynamic array. Public...
For C++ Use arrays and or vectors, no classes. Visualize and consider 100 lockers all lined...
For C++ Use arrays and or vectors, no classes. Visualize and consider 100 lockers all lined up horizontally in a row Each locker is numbered from 1 to 100 in sequential order Every locker can be fully closed (open state = 0.00) Every locker can be fully opened (open = 1.00) Every locker can be partially open with any possible value between 0.00 and 1.00 inclusive on both ends A locker cannot ever be more closed than fully closed (open...
For C++ Use arrays and or vectors, no classes. Visualize and consider 100 lockers all lined...
For C++ Use arrays and or vectors, no classes. Visualize and consider 100 lockers all lined up horizontally in a row Each locker is numbered from 1 to 100 in sequential order Every locker can be fully closed (open state = 0.00) Every locker can be fully opened (open = 1.00) Every locker can be partially open with any possible value between 0.00 and 1.00 inclusive on both ends A locker cannot ever be more closed than fully closed (open...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT