in C++
Problem:
Subtract 4 to any element's value that is greater than maxVal.
Ex: If maxVal = 10, then dataPoints = {2, 12, 9, 20} becomes {2, 8, 9, 16}.
------------------------
#include
#include
using namespace std;
int main() {
int maxVal;
int NUM_POINTS;
unsigned int i;
cin >> maxVal;
cin >> NUM_POINTS;
vector dataPoints(NUM_POINTS);
for (i = 0; i < dataPoints.size(); ++i) {
cin >> dataPoints.at(i);
}
/* Your solution goes here */
for (i = 0; i < dataPoints.size(); ++i) {
cout << dataPoints.at(i) << " " ;
}
cout << endl;
return 0;
}
Code:
#include<iostream>
#include<vector>
using namespace std;
int main() {
int maxVal;
int NUM_POINTS;
unsigned int i;
cin >> maxVal;
cin >> NUM_POINTS;
vector<int> dataPoints(NUM_POINTS);
for (i = 0; i < dataPoints.size(); ++i) {
cin >> dataPoints.at(i);
}
/* Your solution goes here */
for (i = 0; i < dataPoints.size(); ++i) {
if(dataPoints.at(i) > maxVal ){
dataPoints.at(i) =
dataPoints.at(i) - 4;
}
}
for (i = 0; i < dataPoints.size(); ++i) {
cout << dataPoints.at(i) << " " ;
}
cout << endl;
return 0;
}
Only added a for block.
in C++ Problem: Subtract 4 to any element's value that is greater than maxVal. Ex: If...
Double any element's value that is less than minVal. Ex: If minVal = 10, then dataPoints = {2, 12, 9, 20} becomes {4, 12, 18, 20}. #include <stdio.h> int main(void) { const int NUM_POINTS = 4; int dataPoints[NUM_POINTS]; int minVal; int i; dataPoints[0] = 2; dataPoints[1] = 12; dataPoints[2] = 9; dataPoints[3] = 20; minVal = 10; /* Your solution goes here */ for (i = 0; i < NUM_POINTS; ++i) { printf("%d ", dataPoints[i]); } printf("\n"); return 0; }
Double any element's value that is less than minValue. Ex: If minValue 10, then dataPoints = {2, 12, 9, 20) becomes {4, 12, 18, 20). public static void main (String ] args) { Scanner scnr = new Scanner(System.in); 4 5 final int NUM_POINTS = 4; int dataPoints = int minValue int i new int[NUM POINTS]; 7 8 minValue 10 scnr.nextInt(); 11 for (i 0; i dataPoints[i] } dataPoints.length; ++i) { scnr.nextInt); 12 13 14 15 /Your solution goes here */...
C++ Write a function so that the main() code below can be replaced by the simpler code that calls function MphAndMinutesToMiles(). Original main(): int main() { double milesPerHour; double minutesTraveled; double hoursTraveled; double milesTraveled; cin >> milesPerHour; cin >> minutesTraveled; hoursTraveled = minutesTraveled / 60.0; milesTraveled = hoursTraveled * milesPerHour; cout << "Miles: " << milesTraveled << endl; return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> using...
C++ Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}. #include <iostream> using namespace std; int main() { const int SCORES_SIZE = 4; int lowerScores[SCORES_SIZE]; int i; for (i = 0; i < SCORES_SIZE; ++i) { cin >> lowerScores[i]; } /* Your solution goes here */ for (i = 0; i < SCORES_SIZE;...
C++ class 3.2.4: If-else statement: Fix errors. Re-type the code and fix any errors. The code should convert non-positive numbers to 1. if (userNum > 0) cout << "Positive." << endl; else cout << "Not positive, converting to 1." << endl; Answer #include using namespace std; int main() { int userNum; cin >> userNum; /* Your solution goes here */ return 0; userNum = 1; cout << "Final: " << userNum << endl;
Assign numMatches with the number of elements in userValues that equal matchValue. userValues has NUM_VALS elements. Ex: If userValues is {2, 2, 1, 2} and matchValue is 2 , then numMatches should be 3. Your code will be tested with the following values: * matchValue: 2, userValues: {2, 2, 1, 2} (as in the example program above) * matchValue: 0, userValues: {0, 0, 0, 0} * matchValue: 50, userValues: {10, 20, 30, 40} (Notes) #include <iostream> #include <vector> using namespace...
5.9.2: Modify a string parameter. C++ Complete the function to replace any period by an exclamation point. Ex: "Hello. I'm Miley. Nice to meet you." becomes: "Hello! I'm Miley! Nice to meet you!" #include <iostream> #include <string> using namespace std; void MakeSentenceExcited(string& sentenceText) { /* Your solution goes here */ } int main() { string testStr; getline(cin, testStr); MakeSentenceExcited(testStr); cout << testStr; return 0; }
CHALLENGE ACTIVITY 8.4.2: Find char in C string Assign a pointer to any instance of searchChar in personName to searchResult. #include <iostream> #include <cstring> using namespace std; int main() { char personName[100]; char searchChar; char* searchResult = nullptr; cin.getline(personName, 100); cin >> searchChar; /* Your solution goes here */ if (searchResult != nullptr) { cout << "Character found." << endl; } else { cout << "Character not found." << endl; } return 0; }
Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with userNum initially 9 and user input of 5, 2, -1, then with userNum initially 0 and user input of -17, then with userNum initially -1. See "How to Use zyBooks". . Also note: If the submitted code has an infinite loop, the system will...
C++ Given numRows and numColumns, print a list of all seats in a theater. Rows are numbered, columns lettered, as in 1A or 3E. Print a space after each seat, including after the last. Ex: numRows = 2 and numColumns = 3 prints: 1A 1B 1C 2A 2B 2C #include <iostream> using namespace std; int main() { int numRows; int numColumns; int currentRow; int currentColumn; char currentColumnLetter; cin >> numRows; cin >> numColumns; /* Your solution goes here */ cout...