-2

I was told on here to use vectors instead of arrays for this particular solution ( My original solution using to dynamically allocate arrays) my problem is I don't really understand vectors, my book only covers a small section. So, maybe I am doing something wrong? I'm just trying to learn a different way in solving it with vectors. So, when I ask is it possible to dynamically allocate a vector. What I mean is it legal to do this std::vector<int*> vect= nullptr;, if not why? Also it would be really helpful to see an example how to modify my original solution with vectors. I'm a beginner just trying to learn from my mistakes.

#include <iostream>
#include <vector>
void sortAscendingOrder(int*, int );
void LowestTestScore(int*, int);
void calculatesAverage(int*,int);
int main()
{
    std::vector<int*> vect= nullptr;
    int input;

    std::cout << "Enter the number of testscores you want to enter." <<std::endl;
    std::cin >> input;

    vect = new int[input];

    for(int count =0; count < input; count++)
    {
        std::cout << "Enter the test score" << (count +1) <<":" <<std::endl;
        std::cin >> vect[count] ;
        while( vect[count] < 0)
        {
            std::cout <<"You enter a negative number. Please enter a postive number." <<std::endl;
            std::cin >> vect[count];
        }
    }

    sortAscendingOrder(vect,input);

    for(int count =0; count < input;count++)
    {
        std::cout << "\n" <<vect[count];
        std::cout << std::endl;
    }
    LowestTestScore(vect,input);
    calculatesAverage(vect,input);



    return 0;
}
void sortAscendingOrder(int* input,int size)
{
    int startScan,minIndex,minValue;

    for(startScan =0; startScan < (size-1);startScan++)
    {
        minIndex = startScan;
        minValue = input[startScan];
        for(int index = startScan+1;index<size;index++)
        {
            if(input[index] < minValue)
            {
                minValue = input[index];
                minIndex = index;
            }
        }
        input[minIndex]=input[startScan];
        input[startScan]=minValue;
    }
}
void LowestTestScore(int* input, int size)
{
    int count =0;
    int* lowest = nullptr;
    lowest = input;
    for(count =1; count <size;count++)
    {
        if(input[count] < lowest[0])
        {
            lowest[0] = input[count];
        }
    }
    std::cout << "Lowest score" << *lowest;
}
void calculatesAverage(int* input, int size)
{
    int total = 0;
    int average =0;
    for(int count = 0; count < size; count++)
    {
        total += input[count];

    }
    average =(total/size-1);
    std::cout << "Your average is" << average;
}
Community
  • 1
  • 1
Pierre
  • 41
  • 1
  • 6
  • You may need to explain your terms a little more. Vectors allocate a contiguous block of memory using new. That fits my definition of dynamically allocated. – Retired Ninja Mar 28 '17 at 04:15
  • 2
    The beauty of vector is it does the dynamic allocation for you. More here:http://en.cppreference.com/w/cpp/container/vector – user4581301 Mar 28 '17 at 04:16
  • Do you mean: `auto p_vec = std::make_unique>();`? – paddy Mar 28 '17 at 04:21
  • Sorry, for the vague question. Let me fix it and see if that helps. – Pierre Mar 28 '17 at 04:24
  • Get a better book. It's essential. – n. m. could be an AI Mar 28 '17 at 04:54
  • 1
    There is no need to use pointers or new in this problem, or worry about dynamic allocation one little bit. Vectors magically do it all for you. Though it is possible to dynamically allocate a vector, this is not what you should do here (most of the time, actually). – n. m. could be an AI Mar 28 '17 at 05:01
  • The book I am using right now is starting out with c++ from control structures through objects 8th edition, Tony Gaddis. It's the book I used for my programming class. – Pierre Mar 28 '17 at 05:02
  • It also depends on what you class as the dynamic allocation. `std::vector vect;` is not a dynamically allocated vector. It's a vector that will contain dynamically allocated elements. It will also have a dynamically allocated internal list of those pointers. A dynamically allocated vector would be `std::vector* vect;`, where the vector class itself must be dynamically allocated. The reasons for doing this are unconvincing, since the vectors internals are allocated anyway and its size otherwise is very small. – Paul Rooney Mar 28 '17 at 05:02
  • I think I need to learn a little bit more about vectors, there's a lot of resources out there but as a beginner I feel limited to know what resources I should learn from. Any suggestions? – Pierre Mar 28 '17 at 05:07
  • 1
    Looks like another run of the mill incredibly crappy and incredibly expensive C++ textbook (I only looked at the TOC). See this recommended list http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – n. m. could be an AI Mar 28 '17 at 05:13

1 Answers1

2

Dynamic allocation of an array is required when you want to increase the size of an array at run-time. Just like arrays, vectors used contiguous storage locations for their elements. But, unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.

Vectors do not reallocate each time an element is added to the container. It pre-allocates some extra storage to accommodate future growth. Libraries can implement different strategies for growth to balance between memory usage and reallocation, but in any case, reallocation should only happen at logarithmically growing intervals of size so that the insertion of individual elements at the end of the vector can be provided with amortized constant time complexity (see push_back)

To answer your question: Yes, it is possible to dynamically allocate a vector and no, it requires no extra effort as std:vector already does that. To dynamically allocate vectors is therefore, a redundant effort.

You can write:

  #include<vector>

   typedef std::vector< std::vector<double> >vec_array;
   vec_array name(size_x, std::vector<double>(size_y));

This means: size instances of a std::vector<double>, each containing size_y doubles (initialized to 0.0).

Or, you can simply use vector to increase the container size (knowing it handles storage requirements by itself) like:

#include <vector>

 vector<int> vec_array;
 int i;

 for(i=0;i<10;i++)
    vec_array.push_back(i);