Answer to Question #233108 in C++ for Krishna

Question #233108

generate random integers in the range [ LOW = 1, HIGH = 10000 ] and to store them in a vector < int > of size VEC_SIZE = 250. Then, sort the contents of the vector (in ascending order) and display it on stdout. To sort the contents of a vector, use the sort ( ) function in the STL. In addition to the main ( ) routine, implement the following subroutines in your program: • void genRndNums ( vector < int >& v ) : This routine generates VEC_SIZE integers and puts them in vector v. Initializes the random number generator (RNG) by calling the function srand ( SEED ) with the seed value SEED = 1 and generates random integers by calling the function rand ( ). • void printVec ( const vector < int >& v ) : This routine displays the contents of vector v on stdout, printing exactly NO_ITEMS = 12 numbers on a single line, except perhaps the last line. The sorted numbers need to be properly aligned on the output. For each printed number, allocate ITEM_W = 5 spaces on stdout.


1
Expert's answer
2021-09-04T15:22:44-0400


#include <vector>
#include <cmath>
#include<time.h>
#include<dos.h>
#include<iostream>
#include <stdio.h>
#include <stack>
using namespace std;

/*
generate random integers in the range [ LOW = 1, HIGH = 10000 ] and to store them in a vector < int > of size VEC_SIZE = 250. 
Then, sort the contents of the vector (in ascending order) and display it on stdout. 
To sort the contents of a vector, use the sort ( ) function in the STL. In addition to the main ( ) routine, 
implement the following subroutines in your program: • void genRndNums ( vector < int >& v ) : 
This routine generates VEC_SIZE integers and puts them in vector v. Initializes the random number generator (RNG) 
by calling the function srand ( SEED ) with the seed value SEED = 1 and generates random integers by calling the function rand ( ). 
• void printVec ( const vector < int >& v ) : This routine displays the contents of vector v on stdout, printing exactly NO_ITEMS = 12 numbers on a single line, 
except perhaps the last line. The sorted numbers need to be properly aligned on the output. For each printed number, allocate ITEM_W = 5 spaces on stdout.
*/


#define VEC_SIZE 15
#define MIN	1
#define MAX 1000
#define NO_ITEMS  12


vector<int> V;


void genRndNums(void)
{
    int n,t=MIN-1,SEED=1;
	srand(SEED); 
    for(n=0;n<VEC_SIZE;n++)
    {
    	while(t<MIN || t>MAX) t=rand();
       	V.push_back(t);
       	t=MIN-1;
	}
}


void printVec ( const vector < int >& v )
{
	for(int i=0;i<NO_ITEMS ;i++) cout<<v[i]<<"\t";
}
main(void)
{
	int i,j;
	genRndNums();
	cout<<"\nBefore Sorting, the vector is :";
	printVec(V);
	sort(V.begin(),V.end());
	cout<<"\nAfter Sorting, the vector is :";
	printVec(V);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS