CS202 vector spr2008

# CS202 vector spr2008 - CS201 Vector Examples Reading into a...

This preview shows pages 1–2. Sign up to view the full content.

CS201 - Vector Examples Reading into a fixed vector vector< int > values(5); for (k = 0 ; k < values.size(); k++) { cout << "Enter a value:"; cin >> values[k]; } Reading a fixed amount of data into an empty vector vector< int > values; cout << "How many values do you wish to average? " ; cin >> numValues; for (k = 0; k < numValues; k ++) { cout << "Enter an integer value: " ; cin >> tempValue; values.push_back(tempValue); } Reading a variable amount of data into an empty vector vector< int > values; cout << “Enter a value or -1 to quit:”; cin >> tempValue; while(tempValue!= -1) { values.push_back(tempValue); cout << “Enter a value or -1 to quit:”; cin >> tempValue; } Averaging a vector Total = 0; for (k = 0 ; k < values.size(); k++) Total = Total + value[k]; Avg = Total/ values.size(); Finding the smallest in a vector smallpos = 0; for (k = 1 ; k < values.size(); k++) if(values[k] < values[smallpos]) smallpos = k; cout << "The smallest value of " << values[smallpos] << " was found at position "

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 2

CS202 vector spr2008 - CS201 Vector Examples Reading into a...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online