Question

In: Computer Science

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() % 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.

Submission Instructions:

  • Submit C++ source code files that solve the defined problems.
  • Submit screenshots of your test output for each exercise.

Solutions

Expert Solution

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


Related Solutions

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...
Write a program, using any language you want, and any sorting algorithm discussed in this unit...
Write a program, using any language you want, and any sorting algorithm discussed in this unit to sort the following : 243, 1, 4, 6, 234, 33, 674, 32, 3333 Note: Can you write it in quick sort
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[ ] ).
Using the concepts from the Concurrency Basics Tutorial I provided in Modules, write a program that...
Using the concepts from the Concurrency Basics Tutorial I provided in Modules, write a program that consists of two threads. The first is the main thread that every Java application has. The main thread should create a new thread from the Runnable object, MessageLoop, and wait for it to finish. If the MessageLoop thread takes too long to finish, the main thread should interrupt it. Use a variable named maxWaitTime to store the maximum number of seconds to wait. The...
Write a program that takes two integer arrays a and b of size n from the...
Write a program that takes two integer arrays a and b of size n from the user, the use a method product to find the product of a and b and return the results after storing them in an array c, then prints the returned results to the screen. (Note: c[i] = a[i] * b[i], for i = 0, ..., n-1) Sample Output: Enter the size of your arrays: 5 Enter the integer values of the first array a: 1...
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...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows: If the right-most digit of all the three integers is equal, the program displays them in ascending order on the screen and continues. If the generated integers have different right-most digits, they are not displayed and the program continues. The program terminates once the right-most digits of all the three random numbers are...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
1) Find the sum of vectors A + B in cartesian component from using the unit...
1) Find the sum of vectors A + B in cartesian component from using the unit vector X and Y http://tinypic.com/r/5v6bdy/8 <- is where the graph 2) then find the sum of the vectors in polar form 3) if A+b+c=0 find C? Note in case the graph isnt showing: In a X and Y planar: Vector A is in the positve quadrant of X and positive of Y, A= 145.0m and the angle= 25.8 Vector B is in the negative...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT