Hi guys, In my lab I was given a source code and was tasked to write the function to compute the largest value of a vector, and a for loop to show how elements does the vector has.But the output is now right,it gives me the largest element is zero,and he prints only one element inside a vector,can anyone help?))))

#include <iostream>
#include <vector>

// using std namespace
using namespace std;


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//  Function Prototypes.
//  Make sure to add the proper comments (as per 233 Commenting Guideline)
//  in this section
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


void read_vector(vector<int>& v);
// Students to write function comments here.
// Hint: This function reads in the size of vector v followed by its elements
// from cin. It assumes that there are no input failues (i.e. all the
// information can be read properly from cin)


void display_vector(const vector<int>& v);
// Students to write function comments here.
// Hint: This function displays vector size and vector elements on cout.
// Output is printed on a single line, which only looks good when
// the number of elements is small.


int largest_element(const vector<int>& v);
// Students to write function comments here.
// Hint: This function returns value of largest element of v.
// Don't call this function if size of v = 0.



//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//             Main Program
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


int main()
{
  vector<int> user_vector;

  read_vector(user_vector);

// Part A:
// Uncomment the following line once you have written the
// function definitions for display_vector() and largest_element()


display_vector(user_vector);



// Part B:
// Uncomment the following lines once you have written the
// function definitions for largest_element()


  if (user_vector.size() > 0)
    {
       cout << "The largest element value is "
       << largest_element(user_vector) << '.' << endl;
     }
   else
     cout << "Can't compute largest element values." << endl;

  return 0;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Functions Definitions
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void read_vector(vector<int>& v)
{
  int size_input;

  cout << "Reading data into a vector<int> " << endl;
  cin >> size_input;

  if (size_input == 0)
    {
      cout << "Making vector empty." << endl;
      v.resize(0);
    }

  else
    {
      v.resize(size_input);

      for (int i = 0; i < v.size(); i++)
    cin >> v.at(i);
    }
}


void display_vector(const vector<int>& v)
{
  cout << "size is " << v.size();
  if (v.size() == 0)
    cout << "; no elements." << endl;
  else
    {
      cout << "; elements are { ";

      for (int i=0;i<v.size();i++)//the fuction I've written
      { cout<<v.at(i)<<" ";
        cout <<endl; }

    }
}


int largest_element(const vector<int>& v)//The function I've written
{
  int largest=v[1];
  for (int i=0;i<v.size();i++)
  { if (v[i]>largest)
  largest=v[i];
  }}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Hello.
First of all: Use code tags.
Great now that's settled I'm not completely able to understand your problem.
Anyway, to list all the elements in a vector, look at this:

typedef std::vector<int> integerVector; // a standard typedef of our vector type
integerVector mNumbers; // here we actually make an instance of the vector based on our typedef
mNumbers.push_back(25); // could be any number
mNumbers.push_back(72); // could be any number
mNumbers.push_back(12); // could be any number

/* Ok, lets get the vector content */
unsigned int highest = 0; // create a variable to keep track of highest value, note this approach will only work with positive  numbers

integerVector::const_iterator pos; // an iterator to store the current vector element
for(pos = mNumbers.begin(); pos != mNumbers.end(); ++pos)
{
    // this loop will run for each item in mNumbers, and the item is accessible in pos

    std::cout << (*pos) << std::endl; // output pos, dereferenced with a *, otherwise we output the address
    if((*pos) > highest) // if this element is the highest yet
        highest = (*pos); // then register it
} // now we've been through all the elements

/* highest should now contain the biggest integer element
std::cout << "The highest element was: " << highest << std::endl;

Hope this is of any help.
Also note that you can easily get the number of elements with mNumbers.size()

Edited 6 Years Ago by Excizted: info reg. std::vector&lt;T&gt;::size

well I dont know if my function are correct,well they seem to be correct but still the output is not right..

If you edit your original post so that the code is in CODE tags, and then indent the code like this (which is only possible in code tags)

{
   stuff happens;
}

Then I'd be happy to read your code and help you further.

Edited 3 Years Ago by Nick Evan: Fixed formatting

// include necessary libraries
#include <iostream>
#include <vector>
#include <algorithm>
// using std namespace
using namespace std;


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//  Function Prototypes.
//  Make sure to add the proper comments (as per 233 Commenting Guideline)
//	in this section
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


void read_vector(vector<int>& v);
//Requires:Const vector<int>&v represent the array of some elements.
//Promises:Fills in the elements into the array,so creating a line of elements


void display_vector(const vector<int>& v);

//Promises:Displays all elements of an array on the monitor.


int largest_element(const vector<int>& v);
//Promises:Finds the largest value in an array filled with elements.

void bubble_sort(vector <int>&a,int l);
//Promises: Sorts the elements in the array from lowest to highest

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//             Main Program
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


int main()
{
  vector<int> user_vector;

  read_vector(user_vector);

// Part A:
// Uncomment the following line once you have written the
// function definitions for display_vector() and largest_element()


display_vector(user_vector);



// Part B:
// Uncomment the following lines once you have written the
// function definitions for largest_element

  if (user_vector.size() > 0)
    {
       cout << "The largest element value is "
 	   << largest_element(user_vector) << '.' << endl;
     }
   else
     cout << "Can't compute largest element values." << endl;

  return 0;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Functions Definitions
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void read_vector(vector<int>& v)
{
  int size_input;

  cout << "Reading data into a vector<int> " << endl;
  cin >> size_input;

  if (size_input == 0)
    {
      cout << "Making vector empty." << endl;
      v.resize(0);
    }

  else
    {
      v.resize(size_input);

      for (int i = 0; i < v.size(); i++)
	cin >> v.at(i);
    }
}


void display_vector(const vector<int>& v)
{
  cout << "size is " << v.size();
  if (v.size() == 0)
    cout << "; no elements." << endl;
  else
    {
      cout << "; elements are { ";

      for (int i=0;i<v.size();i++)
       {cout<<v.at(i)<<" "<<"}"<<endl;}
         }

    }

void bubbleSort( vector <int>&v, int l) {
   for (int x=1; x<l; x++) {
       for (int i=0; i < l-x; i++) {
           if (v[i] > v[i+1]) {
               int temp = v[i]; v[i] = v[i+1]; v[i+1] = temp;
           }
       }
   }
}

int largest_element(const vector<int>& v)
{
  int largest=v[0];
  for (int i=0;i<v.size();i++)
  { if (v[i]>largest)  largest=v[i];
  }
  return largest;
}

ok,here is the code,the program compiles,all the functions seem to be right,but when it outputs,it outputs not all the elements,for example in you input 1 2 3 4 5 6,it outputs like "the elements are{2,3,4} and it also does not sort the elements.so I'm confused.and also when I redirect input like c: myprog<fgg.txt, it gives me an infinite loop with 0.

Your code works perfectly for me unfortunately.
I made four elements, entered them 1, 2, 3, 4 and it posted back all the elements and stated that the highest was 4.

It seems to work fine for me, too, though it took me a minute to figure out what the program does. The first number input is the size, right?

So if you input 4,3,2,1,0 it will say that the size is 4 and the elements are {3} 2} 1} 0}

It gets the highest number correctly for me, every time.

But it doesn't sort them. I can't see anywhere in your program where you have called your sort function, though, so maybe that's why it's not sorting?

This article has been dead for over six months. Start a new discussion instead.