In: Computer Science
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.
Exercise 1:
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.
exercise1.cpp file:
#include <algorithm>
#include "math.h"
#include<iostream>
using namespace std;
int main()
{
bool map[100] = {false};
int numbers[20],i=0, val;
const int SIZE = 20;
int numAdded = 0;
while (numAdded < 20) {
val = floor(rand() % 100);
// cout<< "random value generated is : " << val
<<endl;
if(map[val] == false){
numbers[i] = val;
i++;
numAdded++;
map[val] = true;
}
}
cout << "Sequence before sorting : " <<endl;
for(int j=0; j<20; j++){
cout << numbers[j] << " ";
}
cout<< endl;
size_t size = sizeof(numbers) / sizeof(numbers[0]);
sort(numbers, numbers + size);
cout << "Sequence after sorting : " <<endl;
for(int j=0; j<20; j++){
cout << numbers[j] << " ";
}
return 0;
}
Below is the screenshot of execution for excercise1:
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.
exercise2.cpp file:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string countries[] = {"Egypt", "Switzerland", "Argentina", "Spain",
"Portugal", "Luxemburg", "India", "Germany", "China", "United
States of America", "Guatemala", "Croatia", "Panama", "Nicargoa",
"Pakistan"};
int N = sizeof(countries)/sizeof(countries[0]); //Get the array
size
cout<<"List of Countries unsorted"<<endl;
for(int i = 0; i < N; i++){
cout << countries[i] << " ";
}
cout<<endl;
sort(countries,countries+N);
cout<<"\nList of Countries after sorting"<<endl;
for(int i = 0; i < N; i++)
{
cout << countries[i] << " ";
}
cout<<endl;
return 0;
}
Below is the screenshot of Execution:
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.
exercise3_1.cpp:
This contains source code for the code exercise1 with vectors implementation:
#include <random>
#include <algorithm>
#include "math.h"
#include<iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> numbers;
bool map[100] = {false};
int val, i=0,numAdded = 0;
uniform_int_distribution<> dist(0, RAND_MAX);
while (numAdded < 20) {
val = floor(rand() % 100);
if(map[val] == false){
numbers.push_back(val);
i++;
numAdded++;
map[val] = true;
}
}
cout << "\nSequence before sorting : " <<endl;
for (int x=0; x < 20; x++)
{
cout<< numbers[x] << " ";
}
std::sort(numbers.begin(), numbers.end());
cout<< endl;
cout << "\nSequence after sorting : " <<endl;
for (int x=0; x < 20; x++)
{
cout<< numbers[x] << " ";
}
//asked to add additional element to the list
while (numAdded < 21) {
val = floor(rand() % 100);
if(map[val] == false){
numbers.push_back(val);
i++;
numAdded++;
map[val] = true;
}
}
cout<<"\n\n\nUnsorted sequence after adding additional
element: "<<endl;
for (int x=0; x < 21; x++)
{
cout<< numbers[x] << " ";
}
std::sort(numbers.begin(), numbers.end());
cout<< endl;
cout << "\nSorted Sequence after adding additional element :
" <<endl;
for (int x=0; x < 21; x++)
{
cout<< numbers[x] << " ";
}
return 0;
}
Below is the screenshot of execution:
exercise3_2.cpp:
This contains source code for the code exercise2 with vectors implementation and an additional element added:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector<string> countries = {"Egypt", "Switzerland",
"Argentina", "Spain", "Portugal",
"Luxemburg", "India", "Germany", "China", "United
States of America", "Guatemala", "Croatia", "Panama", "Nicargoa",
"Pakistan"};
cout<<"List of Countries unsorted"<<endl;
for(int i = 0; i < countries.size(); i++){
cout << countries[i] << " ";
}
cout<<endl;
sort(countries.begin(), countries.end());
cout<<"\nList of Countries after sorted"<<endl;
for(int i = 0; i < countries.size(); i++)
{
cout << countries[i] << " ";
}
cout<<endl;
//asked to add additional element to the list
countries.push_back("Afghanistan");
cout<<"\n\nList of Countries unsorted after addition of new
country Afghanistan"<<endl;
for(int i = 0; i < countries.size(); i++){
cout << countries[i] << " ";
}
sort(countries.begin(), countries.end());
cout<<endl;
cout<<"\nList of Countries sorted after addition of new
country Afghanistan"<<endl;
for(int i = 0; i < countries.size(); i++)
{
cout << countries[i] << " ";
}
return 0;
}
Below is the screenshot of the result after execution:
Please upvote if this answer helps.
Thanks