Question

Please answer in C++ ONLY, and please fill all comments elaboratively.

Question 1 Lets consider the following code * * * * Compile and fix all errors No test cases are required for this question.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem included comments wherever needed. Go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include<vector>

#include<iostream>

using namespace std;

int main(){

                vector<double> v; //creating a vector of double values

               

                v.assign(8,16.3); //initializing vector with 8 elements having value 16.3 into the vector

                v[2]=16.4; v[3]=20.6; //changing element at index 2 to 16.4 and element at index 3 to 20.6

                v.push_back(17.8); //adding 17.8 to the end of vector

                v.push_back(-7.3); //adding -7.3 to the end of vector

                for(int i=0;i<v.size();i++){ //v.size() gives the number of elements currently on vector

                                cout<<v[i]<<" "; //v[i] returns the element at index i on vector

                }

                cout<<endl;

                cout<<v.front()<<" "<<v.at(2)<<" "<<v.back()<<endl;

                // v.front gives the first element on vector

                //v.at(2) is similar to v[2] returning element at index 2

                //v.back() returns the rear element on vector

                return 0;

               

}

/*OUTPUT*/

16.3 16.3 16.4 20.6 16.3 16.3 16.3 16.3 17.8 -7.3

16.3 16.4 -7.3

Add a comment
Know the answer?
Add Answer to:
Question 1 Let's consider the following code * * * * Compile and fix all errors No test cases are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The following code will not compile. Fix it by inserting one line of code. Do not...

    The following code will not compile. Fix it by inserting one line of code. Do not change anything else. Hint: copy and paste the code below into your answer box, then make the required addition. Also include a comment indicating the line of code that you inserted, and why that line of code is required include <iostream> using namespace std int mainO cout <<"The word of the day is: sayword 0 return 0 void sayWordO f cout くく "WORD!";

  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

  • C++ Fix the errors in the following code. (Not all errors are syntax related) #include <iostream>...

    C++ Fix the errors in the following code. (Not all errors are syntax related) #include <iostream> using namespace std; int main() { //Part A int numA numA = 10; cout << numA << end; /* Part B */*/ int numB = numA; cin >> usrInput; int usrInput = 0; numB = numB + usrInput; cout << numB <"/n";    /* Part C */ int numC = 10000000000; cout << numC << endl; return 0; }

  • Find and fix the errors in this C++ code: * This program illustrates a variety of...

    Find and fix the errors in this C++ code: * This program illustrates a variety of common loop errors. * Fix the errors in each section. */ #include <iostream> using namespace std; int main() { cout << "Welcome to Loop World" << endl; // SECTION I: update comment below on how you fixed this section's code, and tests run // FIX = // TESTS: cout << endl; cout << "******************" << endl; cout << "Section I" << endl; cout <<...

  • What are the errors in the following code? My professor gave us this prewritten code and...

    What are the errors in the following code? My professor gave us this prewritten code and asked us to find the errors in it so that it can run properly #include <cstdlib> #include <ctime> #include <iostream> using namespace std; // input: integer // output: none // adds five to the given parameter void addFive( int x ) { x += 5; } // input: none // output: a random number int generateRandomNumber() { srand( time(0) ); return rand() % 100;...

  • The C++ code below will compile but has a variety of runtime issues. Identify a runtime...

    The C++ code below will compile but has a variety of runtime issues. Identify a runtime issue with the program and describe how you might fix the problem. #include <iostream> #include <vector> using namespace std; //------------------------------------------------------------------------------ int main() { vector<double> temps; // temperatures double temp = 0; double sum = 0; double high_temp = 0; double low_temp = 0; while (cin >> temp) // read and put into temps temps.push_back(temp); for (int i = 0; i < temps.size(); ++i) {...

  • A) Fix any errors to get the following program to run in your environment.               B)...

    A) Fix any errors to get the following program to run in your environment.               B) Document each line of code with comments and describe any changes you had to make to the original code to get it to work. C) Write a summary of what your final version of the program does. You may also add white space or reorder the code to suit your own style as long as the intended function does not change. Program 3 #include...

  • How do I compile c++ code in terminal. Below i have some code to apply Gaussian...

    How do I compile c++ code in terminal. Below i have some code to apply Gaussian Filter on an image using OpenCV. I have tried compling using the code g++ Project2.cpp -o Project2 `pkg-config --cflags --libs opencv` && ./Project2 and it gives me a whole list of errors saying directory not found. --------------------------------------------------------------------------------------------------------- #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <opencv2/core/core.hpp> #include <iostream> using namespace cv; using namespace std; int main() { Mat image = imread("//u//oowens//First.jpg"); int rows=image.rows; int cols=image.cols; if (image.empty())...

  • Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std;...

    Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std; int main(){    int rows = 5; int cols = 5; int x;    int** arr = new int[rows][cols]    cin >> x; arr[x][x] = x; cout << "arr[x][x] = " << arr[x][x];    return 0; } Question 2: Fix the code to initialize the 2D array elements to x #include <iostream> using namespace std; int main(){    int rows; int cols; int x;...

  • Hi there! I need to fix the errors that this code is giving me and also...

    Hi there! I need to fix the errors that this code is giving me and also I neet to make it look better. thank you! #include <iostream> #include <windows.h> #include <ctime> #include <cstdio> #include <fstream> // file stream #include <string> #include <cstring> using namespace std; const int N=10000; int *freq =new int [N]; int *duration=new int [N]; char const*filename="filename.txt"; int songLength(140); char MENU(); // SHOWS USER CHOICE void execute(const char command); void About(); void Save( int freq[],int duration[],int songLength); void...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT