In: Computer Science
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.
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.