Uploaded by Waqar Shahid

Assigment no 2 Vector

advertisement
Vector exercises
Vector exercisesVector exercises
1. Create a vector<> container with elements containing the integers from 1 to an arbitrary
upper bound entered by the user. Output the elements from the vector that contain values that
are not multiples of 7 or 13. Output them 10 on a line, aligned in columns.
---------------2. Write a program that will read and store an arbitrary sequence of records relating to products.
Each record includes three items of data—an integer product number, a quantity, and a unit price.
For product number 1001, the quantity is 25, and the unit price is $9.95. The program should
output each product on a separate line and include the total cost. The last line should output the
total cost for all products.
-----------------3. The famous Fibonacci series is a sequence of integers with the first two values as 1 and the
subsequent values as the sum of the two preceding values. So, it begins 1, 1, 2, 3, 5, 8, 13, and
so on. This is not just a mathematical curiosity. The sequence also regularly appears in
biological settings, for instance. It relates to the way shells grow in a spiral, and the number of
petals on many flowers is a number from this sequence. Create an vector<> container with 93
elements. Store the first 93 numbers in the Fibonacci series in the vector and then output them
one per line. Any idea why we’d be asking you to generate 93 Fibonacci numbers and not, say,
100?
-----------------4. Read a sequence of words from cin and store the values a vector. After you’ve read all the
words, process the vector and change each word to uppercase. Print the transformed elements,
eight words to a line.
-----------------5. Read a set of integers into a vector. Print the sum of each pair of adjacent elements. Change
your program so that it prints the sum of the first and last elements, followed by the sum of the
second and second-to-last, and so on.
------------------6. Write a program that uses the vector and checks whether the vector contains a palindrome,
i.e. whether the vector does or does not read the same forward as backward (e.g., a vector
containing 1, 2, 3, 2, 1 is a palindrome, but a vector containing 1, 2, 3, 4 is not).
Here's a C++ program that creates a vector container with integers from 1 to an upper bound
entered by the user, and outputs the elements that are not multiples of 7 or 13:
c++
#include <iostream>
#include <vector>
using namespace std;
int main() {
int upperBound;
vector<int> nums;
cout << "Enter an upper bound: ";
cin >> upperBound;
for (int i = 1; i <= upperBound; i++) {
if (i % 7 != 0 && i % 13 != 0) {
nums.push_back(i);
}
}
int count = 0;
for (int num : nums) {
cout << setw(6) << num;
count++;
if (count % 10 == 0) {
cout << endl;
}
}
cout << endl;
return 0;
}
Here's a C++ program that reads and stores records relating to products, and outputs each
product on a separate line along with its total cost, followed by the total cost for all products:
c++
#include <iostream>
#include <vector>
using namespace std;
struct Product {
int number;
int quantity;
double unitPrice;
};
int main() {
vector<Product> products;
int number;
int quantity;
double unitPrice;
while (true) {
cout << "Enter product number (0 to stop): ";
cin >> number;
if (number == 0) {
break;
}
cout << "Enter quantity: ";
cin >> quantity;
cout << "Enter unit price: ";
cin >> unitPrice;
Product product = {number, quantity, unitPrice};
products.push_back(product);
}
double totalCost = 0;
for (Product product : products) {
double cost = product.quantity * product.unitPrice;
cout << "Product " << product.number << ": " << cost << endl;
totalCost += cost;
}
cout << "Total cost for all products: " << totalCost << endl;
return 0;
}
Here's a C++ program that generates the first 93 numbers in the Fibonacci series and outputs
them one per line:
c++
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> fibonacci = {1, 1};
for (int i = 2; i < 93; i++) {
fibonacci.push_back(fibonacci[i-1] + fibonacci[i-2]);
}
for (int num : fibonacci) {
cout << num << endl;
}
return 0;
}
The reason for generating 93 Fibonacci numbers is that the 93rd number in the series is the
largest number that can be represented as a 64-bit integer (which is the largest integer type
available on many systems).
Here's a C++ program that reads a sequence of words from cin and stores them in a vector,
then converts each word to uppercase and outputs them 8 per line:
c++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<string> words;
string word;
while (cin >> word) {
words.push_back(word);
}
transform(words.begin(), words.end(), words.begin(), [](string s){
transform(s.begin(), s.end(), s.begin(), ::toupper);
return s;
});
int count = 0;
for (string word : words) {
cout << setw(15) << word;
Download