CS202 vector spr2008

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

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

View Full Document Right Arrow Icon
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 "
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/18/2008 for the course CS 202 taught by Professor Conway during the Spring '08 term at Bowling Green.

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 Right Arrow Icon
Ask a homework question - tutors are online