Click here to Skip to main content
15,881,803 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: i would like to be a programmer Pin
Vaclav_30-May-18 9:19
Vaclav_30-May-18 9:19 
AnswerRe: i would like to be a programmer Pin
Tarun Jha5-Jun-18 10:53
Tarun Jha5-Jun-18 10:53 
Questiontry /catch Exception missing type Pin
Vaclav_29-May-18 4:00
Vaclav_29-May-18 4:00 
AnswerRe: try /catch Exception missing type Pin
Jochen Arndt29-May-18 5:09
professionalJochen Arndt29-May-18 5:09 
AnswerRe: try /catch Exception missing type Pin
Richard MacCutchan29-May-18 5:12
mveRichard MacCutchan29-May-18 5:12 
AnswerRe: try /catch Exception missing type SOLVED Pin
Vaclav_30-May-18 9:06
Vaclav_30-May-18 9:06 
GeneralRe: try /catch Exception missing type SOLVED Pin
leon de boer30-May-18 16:25
leon de boer30-May-18 16:25 
Questionhow to correct an segmentation fault ? Pin
Tarun Jha26-May-18 11:55
Tarun Jha26-May-18 11:55 
below is an hackerrank question link where i am encountering an segmentation falut.

HackerRank[^]

and this is my solution
#include <iostream>
#include <vector>
using namespace std;

void fillVector(vector<int> &temp, unsigned long number);
void RotateVector(vector<int> &arr1, vector<int> &arr2, unsigned long k);

int main(){
    unsigned long n,k,q;
    
    cin >> n >> k >> q;
    
    vector<int> a, b;
    fillVector(a, n);
    fillVector(b, q);
    
    RotateVector(a, b, k);
    
    
    return 0;
}

//to rotate the vector by kth measure.
void RotateVector(vector<int> &arr1, vector<int> &arr2, unsigned long k){
    unsigned long arraySize = arr1.size();
    unsigned long querySize = arr2.size();
    unsigned long rotation = k, i;
    unsigned long start = rotation%arraySize;       //this was causing the problem.

    if(rotation<arraySize)    start=rotation;      
    vector<int> rotatedArray;
    
    for(i=0; i<arraySize; i++){ 
        if(((arraySize-rotation)+i) < arraySize){                      // here i have to use start, but instead i used rotation.
            rotatedArray.push_back(arr1[(arraySize-rotation)+i]);
        }
        else{
            rotatedArray.push_back(arr1[i-rotation]);
        }
    }
    
    for(i=0; i<querySize; i++){
        cout << rotatedArray[arr2[i]] << endl;
    }   
}

//filling the vector
void fillVector(vector<int> &arr, unsigned long n){
    unsigned long temp;
    for(int i=0; i<n; i++){
        cin >> temp;
        arr.push_back(temp);
    }
}


the above solution is giving segmentation fault and i am not able to detect it.(rest all the test-cases are passed)

here are the links to that test case inputs : https://hr-testcases-us-east-1.s3.amazonaws.com/1884/input04.txt?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1527377038&Signature=sA5C43bnnqyLeXwWA9L16en3rEE%3D&response-content-type=text%2Fplain

and expected outputs : https://hr-testcases-us-east-1.s3.amazonaws.com/1884/output04.txt?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1527377042&Signature=%2BSyDUj0B5DwySkSPGjiwfmB7MlE%3D&response-content-type=text%2Fplain

Edit :
Below is the working code:

#include <bits/stdc++.h>
using namespace std;

void fillVector(vector<int> &temp, unsigned long number);
void RotateVector(vector<int> &arr1, vector<int> &arr2, unsigned long k);

int main(){
    unsigned long n,k,q;
    
    cin >> n >> k >> q;
    
    vector<int> a, b;
    fillVector(a, n);
    fillVector(b, q);
    
    RotateVector(a, b, k);
    
    
    return 0;
}

//to rotate the vector by kth measure.
void RotateVector(vector<int> &arr1, vector<int> &arr2, unsigned long k){
    unsigned long arraySize = arr1.size();
    unsigned long querySize = arr2.size();
    unsigned long rotation = k, i;
    unsigned long start = rotation%arraySize;

    if(rotation<arraySize)    start=rotation;
    vector<int> rotatedArray;
    
    for(i=0; i<arraySize; i++){
        if(((arraySize-start)+i) < arraySize){
            rotatedArray.push_back(arr1[(arraySize-start)+i]);
        }
        else{
            rotatedArray.push_back(arr1[i-start]);
        }
    }
    
    for(i=0; i<querySize; i++){
        cout << rotatedArray[arr2[i]] << endl;
    }   
}

//filling the vector
void fillVector(vector<int> &arr, unsigned long n){
    unsigned long temp;
    for(int i=0; i<n; i++){
        cin >> temp;
        arr.push_back(temp);
    }
}


Thank you.

modified 6-Jun-18 1:05am.

SuggestionRe: how to correct an segmentation fault ? Pin
Richard MacCutchan26-May-18 21:10
mveRichard MacCutchan26-May-18 21:10 
AnswerRe: how to correct an segmentation fault ? Pin
CPallini29-May-18 0:01
mveCPallini29-May-18 0:01 
GeneralRe: how to correct an segmentation fault ? Pin
Tarun Jha5-Jun-18 1:50
Tarun Jha5-Jun-18 1:50 
QuestionGraphical representation of Data Pin
sree7926-May-18 1:09
sree7926-May-18 1:09 
AnswerRe: Graphical representation of Data Pin
Richard MacCutchan26-May-18 2:41
mveRichard MacCutchan26-May-18 2:41 
AnswerRe: Graphical representation of Data Pin
leon de boer26-May-18 5:09
leon de boer26-May-18 5:09 
GeneralRe: Graphical representation of Data Pin
sree7927-May-18 17:44
sree7927-May-18 17:44 
Questionvolatile issue - repost Pin
Vaclav_22-May-18 5:24
Vaclav_22-May-18 5:24 
QuestionRe: volatile issue - repost Pin
Richard MacCutchan22-May-18 8:26
mveRichard MacCutchan22-May-18 8:26 
AnswerRe: volatile issue - repost Pin
CPallini22-May-18 10:11
mveCPallini22-May-18 10:11 
AnswerRe: volatile issue - repost Pin
leon de boer22-May-18 19:12
leon de boer22-May-18 19:12 
GeneralRe: volatile issue - repost Pin
CPallini22-May-18 21:39
mveCPallini22-May-18 21:39 
QuestionRe: volatile issue - repost Pin
CPallini22-May-18 10:11
mveCPallini22-May-18 10:11 
AnswerRe: volatile issue - repost Pin
Vaclav_22-May-18 11:54
Vaclav_22-May-18 11:54 
GeneralRe: volatile issue - repost Pin
leon de boer22-May-18 15:44
leon de boer22-May-18 15:44 
GeneralRe: volatile issue - repost Pin
Vaclav_22-May-18 17:44
Vaclav_22-May-18 17:44 
GeneralRe: volatile issue - repost Pin
leon de boer22-May-18 18:13
leon de boer22-May-18 18:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.