Write code to perform the following steps for an array of integers Declare an ar
ID: 3816375 • Letter: W
Question
Write code to perform the following steps for an array of integers Declare an array of100 integers Have the user enter a value for each element in the array Find the location in the array of the largest integer value Write out the largest value and the array location where it was found (i.e, The largest value is 95, which was found in position 7) Vectors of doubles Declare a variable that will be a vector of doubles (unknown size) Store every positive integer from 2 to 100 (50 values) into the vector Change the first element in the vector by subtracting 1 from it Write out all of the data in the vector separated by spacesExplanation / Answer
Question 4:
Program:
#include<iostream>
using namespace std;
int main()
{
int array[100]; // Declaring array of 100 integers
//reading 100 inputs from user
for(int i=0;i<100;i++) {
cin >> array[i];
}
//Assgining Default value for maxValue as first array value
int maxValue = array[0];
//Assgining Default value for maxIndex as first index
int maxIndex = 0;
//Logic to find max value
for(int i=1;i<100;i++) {
if(maxValue<array[i]) { // if current value is greater than maxValue then update MaxValue and MaxIndex
maxValue = array[i];
maxIndex = i;
}
}
cout << "The largest value is " << maxValue << ", which was found at position " << maxIndex;
return 0;
}
Sample Input:
Sample Output:
The largest value is 100, which was found at position 99.
Question 5:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<double> VectorOfDoubles; // Declaration of Vector Doubles
for(int i=2;i<100;i++) {
VectorOfDoubles.push_back(i); // pushing positive numbers to vector
if(VectorOfDoubles.size()==50) //push only 50 numbers
break;
}
VectorOfDoubles[0] -= 1; //Subtracting 1 from first element
//printing all the data in VectorOfDoubles with space
for(int i=0;i<VectorOfDoubles.size();i++) {
cout << VectorOfDoubles.at(i) << " ";
}
return 0;
}
Sample Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.