in c++ Exercise 1 For this exercise you are required to write three functions. T
ID: 3586241 • Letter: I
Question
in c++
Exercise 1 For this exercise you are required to write three functions. The function declarations should be as follows 1. vector 8ortVecAscending (vector numbers); 2. void sortVecAscending (vector numbers) 3. void sortVecAscending (vector numbers); These functions will sort a vector of integers into ascending order. Remember to test that each function works Exercise2 For this exercise you are required to read in a list of integers from a file numbers.txt. The number of values stored in this file can vary Once you have read in the digits from the file you should ask the user which function they would like to use to sort the list. When asking the user for input you should display Enter 1 to sort by value, 2 to sort by reference and 3 to sort by pointer: If a user enters 1 you should call the first function you created in exercise 1, if a user enters 2 you should call the second function you created in exercise 1 and if a user enters 3 you should call the third function you created in exercise 1. Make sure to include input validation!Explanation / Answer
#include<vector>
#include<stdio.h>
#include <algorithm>
#include<iostream>
using namespace std;
vector<int> sortVecAscending_by_value(vector<int> numbers);
void sortVecAscending_by_reference(vector<int>& numbers);
void sortVecAscending_by_pointer(vector<int>* numbers);
vector<int> sortVecAscending_by_value(vector<int> numbers)
{
std::sort (numbers.begin(), numbers.end());
printf("Sorted in Ascending Order (by value) ");
return numbers;
}
void sortVecAscending_by_reference(vector<int> &numbers)
{
std::sort (numbers.begin(), numbers.end());
printf("Sorted in Ascending Order (by reference) ");
}
void sortVecAscending_by_pointer(vector<int> *numbers)
{
std::sort (numbers->begin(), numbers->end());
printf("Sorted in Ascending Order (by pointer) ");
}
int main()
{
/*opening numbers.txt in read mode to read the integer values*/
/* sample numbers.txt
* ------
* 1
* 23
* 43
* 78
* 88
* 2
* 100
* -------
* */
FILE* file = fopen ("numbers.txt", "r");
int i = 0;
std::vector<int> int_vector;
std::vector<int> numbers;
fscanf (file, "%d", &i);
while (!feof (file))
{
printf ("%d ", i);
/*pushing the unsorted integers into a vector of integers*/
int_vector.push_back(i);
fscanf (file, "%d", &i);
}
fclose (file);
/*asking the user which function to use*/
printf(" Which function you would like to use ");
printf("enter 1 for sort by value ");
printf("enter 2 for sort by reference ");
printf("enter 3 for sort by pointer ");
int choice=0;
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("1 ");
/*call by value*/
numbers = sortVecAscending_by_value(int_vector);
for (std::vector<int>::iterator it=numbers.begin(); it!=numbers.end(); ++it)
std::cout << ' ' << *it;
std::cout << ' ';
break;
case 2:
printf("2 ");
/*call by reference*/
sortVecAscending_by_reference(int_vector);
for (std::vector<int>::iterator it=int_vector.begin(); it!=int_vector.end(); ++it)
std::cout << ' ' << *it;
std::cout << ' ';
break;
case 3:
printf("3 ");
/* call by pointer*/
sortVecAscending_by_pointer(&int_vector);
for (std::vector<int>::iterator it=int_vector.begin(); it!=int_vector.end(); ++it)
std::cout << ' ' << *it;
std::cout << ' ';
break;
default:
printf("You have entered invalid choice ");
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.