Menu

(Solved) : Using Vectors Process Array Numbers Continuing First Exercise Write Program Read Number Re Q44146838 . . .

Using Vectors to Process an Array of Numbers Continuing with thefirst exercise, write a program to read in any number of realnumbers from the command line, using a vector to save such numbers,and printing them out afterwards. Following this scan the vectorand determine the smallest number and build a new vector of sizeN-1 removing one of the smallest numbers. The following shows asample of inputs and outputs:

$./lab4 88 62.4 5 6 8 5 92.1 63.12 Original array :

Numbers[0] : 88

Numbers[1] : 62.4

Numbers[2] : 5

Numbers[3] : 6

Numbers[4] : 8

Numbers[5] : 5

Numbers[6] : 92.1

Numbers[7] : 63.12

New array with smallest element removed :

Numbers[0] : 88

Numbers[1] : 62.4

Numbers[2] : 6

Numbers[3] : 8

Numbers[4] : 5

Numbers[5] : 92.1

Numbers[6] : 63.12

Hint: your code could look like the following:

// lab4.cpp#include <iostream>#include <vector>#include <stdlib.h>using namespace std;int smallestIndex ( vector <double> v ){ double min = 10000.0; int j = 0; ………. return j;}void print ( vector <double> v ){ for ( int i = 0; i < v.size(); i++ ) { …………… } }int main( int argc, char *argv[] ){ vector<double> v; if ( argc < 2 ) { cout << “Usage: ” << argv[0] << ” number1 number2 ….” << endl; exit ( 0 ); } int N = argc – 1; for ( int i = 0; i < N; i++ ) v.push_back ( atof ( argv[i+1] ) ) ; cout << “Original array : ” << endl; print ( v ); int j = smallestIndex ( v ); vector <double> v1; …………. cout << “New array with smallest element removed : ” << endl; print ( v1 ); return 0;}

Expert Answer


Answer to Using Vectors to Process an Array of Numbers Continuing with the first exercise, write a program to read in any number o…

OR