I have written the following code and all im trying to do is

I have written the following code and all i\'m trying to do is finish the function at the bottom printVector that takes the vector in main called grades and prints it to the screen but i can\'t figure out how to write the function. the rest of my code works just the way i want it to but i can\'t figure out how to write the printVector function and all i have so far is a for loop that i\'m not sure will help it.

#include<iostream>
#include<fstream>
#include<vector>
using namespace std;

int search(const vector<int> A, int n, int value);
int printVector(vector<int> N);
int main()
{
   vector<int> grades(5);
   grades = { 100, 95, 87, 92, 79 };
   cout << \"Enter a number\ \";
   int x;
   int count = 5;
   cin >> x;
   search(grades, count, x);
   int position = search(grades, count, x);
   if (position == -1)
   {
       cout << \"This number was not found.\ \" << endl;
       cout << \"Vector contents: \" << printVector(grades)<< \"\ \";
   }
   else
       cout << \"Found at position \" << position << \"\ \";

   return 0;
}

int search(const vector<int> A, int n, int value)
{
   int index(0);
   while (index < n && A[index] != value)
   {
       ++index;
   }
   if (index < n&& A[index] == value)
       return (index);
   else
       return (-1);
}

int printVector(vector<int> N)
{
   for (int c = 0; c <= N.size(); c++)
      
      
}

Solution

int printVector(vector<int> N)
{
for(int c = 0; c <= N.size(); c++)
cout<<N[c]<<endl; //printing vector is similar to printing array,just access the element by index.
  
  
}

Not:please do ask in case of any doubt,Thanks.

I have written the following code and all i\'m trying to do is finish the function at the bottom printVector that takes the vector in main called grades and pri
I have written the following code and all i\'m trying to do is finish the function at the bottom printVector that takes the vector in main called grades and pri

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site