1

I am a newbie in programming. Whenever I include vectors in my programming ,compiler shows segmentation fault. Can anyone help me where am I wrong? And I strongly doubt that it is due to vectors since after commenting out vectors part program runs well. I thought it might be the issue with FOLLOWING IS THE CODE

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

int main()
{
    int a, b; // a rows b columns
    cin >> a >> b;
    vector<vector<int>> k(a, vector<int>(b));

    for (int i = 0; i < a; i++) {
        for (int j = 0; j < b; j++) {
            int ele;
            cin >> ele;
            k[i].push_back(ele);
        }
    }
    vector<vector<int>> ans(a, vector<int>(b));

    for (int count = 0; count < a * b; count++) {
        int j2 = count % b, i2 = count / b;

        if (count % 4 == 0) { // left to right
            int q = k[0].size();
            while (q > 0) {
                ans[i2].push_back(k[0][0]);
                k[0].erase(k[0].begin());
                q--;
            }
        }

        if (count % 4 == 1) { // up to down
            int q = 0;
            while (q < k.size()) {

                ans[i2].push_back(k[q][k[0].size() - 1]);
                k[q].pop_back();
                q++;
            }
        }

        if (count % 4 == 2) { // right to left
            int q = k[0].size();
            while (q > 0) {

                ans[i2].push_back(k[k.size() - 1][q - 1]);
                k[k.size() - 1].pop_back();
                q--;
            }
        }

        if (count % 4 == 3) { // down to up
            int q = k.size();
            while (q > 0) {

                ans[i2].push_back(k[q - 1][0]);
                k[q - 1].erase(k[q - 1].begin());
                q--;
            }
        }
    }

    for (int i = 0; i < a; i++) {
        for (int j = 0; j < b; j++) {
            cout << ans[a][b];
        }
    }

    return 0;
}
Waqar
  • 8,558
  • 4
  • 35
  • 43
  • What is the topic of the chapter in your C++ textbook that this practice problem is from, and what C++ topic is this practice problem is intended to teach? The shown code exhibits many unsafe programming practices, and segfaults and other crashes are a typical result of any one of common programming pitfalls, like running off the end of a vector or array; which often comes as a result of unsafe programming practices. Sounds like you might want to look for a better C++ textbook? – Sam Varshavchik Jul 28 '23 at 11:06
  • no, its not from textbook. I am learning from Internet. But the intention to make us quite familiar with use of vectors.Could you please suggest any good textbook. – Arnav jagtap Jul 28 '23 at 11:08
  • When using nested vectors, it is important to facilitate keeping track of your nested code. For that, using consistent indentation is almost a must. Please update your [mre] accordingly. – Yunnosch Jul 28 '23 at 11:10
  • 1
    Unfortunately, you will not be able to learn C++ "from Internet". Any clown can upload a video to Youtube or publish a rambling blog on some web site, with fancy graphics. Even I can do that. But only an edited, quality textbook will explain core fundamental principles of C++ in an organized manner, and teach how to properly use vector methods, iterators, and algorithms. A publisher is not going take a risk on spending money on dead trees without thoroughly editing and checking for the quality of published content. The same is not true of the "Internet". – Sam Varshavchik Jul 28 '23 at 11:10
  • @Arnavjagtap there are many great resources on the internet for learning c++, here's one: https://www.learncpp.com/ – Waqar Jul 28 '23 at 11:17
  • 2
    @Arnavjagtap - A major problem seems to be that you first create vectors of a certain size, and then use `push_back` to add elements. `push_back` changes the size of the vector to make room for the new elements. Seems wasted if you have already made it the correct size. Also, all uses of `k[0].size() - 1` will fail horribly if `size()` is 0, like after erasing a bunch of elements. – BoP Jul 28 '23 at 11:32
  • 1
    Turn on compiler warnings. Fix those warnings. Use your debugger. Use sanitizers. Use the correct types (probably should use `std::size_t` rather than `int` for indexing). If in doubt about your index being in range, either check the index or use `k.at(index)` for the vector to validate the index value for you. If your tutorial website didn't mention these things, consider getting a [good C++ book](https://stackoverflow.com/a/388282/4641116). – Eljay Jul 28 '23 at 11:45
  • 1
    Sorry but why does *'commenting out the vectors part makes it run well'* make you conclude that your use of vectors is not to blame? I would have concluded the opposite. – john Jul 28 '23 at 11:51
  • Anyway vectors are not complicated, it's just that newbies make incorrect assumptions about how they work. When they program to those incorrect assumptions a segfault is a common result. BoP seems to have described the major problem with your code. – john Jul 28 '23 at 11:52
  • Welcome to Stack Overflow! Please [edit] your code to reduce it to a [mcve] of your problem. Your current code includes much that is peripheral to your problem - a minimal sample normally looks similar to a good unit test: only performing one task, with input values specified for reproducibility. – Toby Speight Jul 28 '23 at 12:06

0 Answers0