Click here to Skip to main content
13,668,134 members

C / C++ / MFC

 
AnswerRe: i would like to be a programmer Pin
Vaclav_30-May-18 9:19
memberVaclav_30-May-18 9:19 
AnswerRe: i would like to be a programmer Pin
Tarun Jha5-Jun-18 10:53
memberTarun Jha5-Jun-18 10:53 
Questiontry /catch Exception missing type Pin
Vaclav_29-May-18 4:00
memberVaclav_29-May-18 4:00 
AnswerRe: try /catch Exception missing type Pin
Jochen Arndt29-May-18 5:09
mvpJochen Arndt29-May-18 5:09 
AnswerRe: try /catch Exception missing type Pin
Richard MacCutchan29-May-18 5:12
protectorRichard MacCutchan29-May-18 5:12 
AnswerRe: try /catch Exception missing type SOLVED Pin
Vaclav_30-May-18 9:06
memberVaclav_30-May-18 9:06 
GeneralRe: try /catch Exception missing type SOLVED Pin
leon de boer30-May-18 16:25
memberleon de boer30-May-18 16:25 
Questionhow to correct an segmentation fault ? Pin
Tarun Jha26-May-18 11:55
memberTarun 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
protectorRichard MacCutchan26-May-18 21:10 
AnswerRe: how to correct an segmentation fault ? Pin
CPallini29-May-18 0:01
mvpCPallini29-May-18 0:01 
GeneralRe: how to correct an segmentation fault ? Pin
Tarun Jha5-Jun-18 1:50
memberTarun Jha5-Jun-18 1:50 
QuestionMessage Closed Pin
26-May-18 6:27
memberben lewis26-May-18 6:27 
QuestionGraphical representation of Data Pin
sree7926-May-18 1:09
membersree7926-May-18 1:09 
AnswerRe: Graphical representation of Data Pin
Richard MacCutchan26-May-18 2:41
protectorRichard MacCutchan26-May-18 2:41 
GeneralMessage Closed Pin
26-May-18 6:24
memberben lewis26-May-18 6:24 
GeneralMessage Closed Pin
26-May-18 6:26
memberben lewis26-May-18 6:26 
AnswerRe: Graphical representation of Data Pin
leon de boer26-May-18 5:09
memberleon de boer26-May-18 5:09 
GeneralRe: Graphical representation of Data Pin
sree7927-May-18 17:44
membersree7927-May-18 17:44 
Questionvolatile issue - repost Pin
Vaclav_22-May-18 5:24
memberVaclav_22-May-18 5:24 
QuestionRe: volatile issue - repost Pin
Richard MacCutchan22-May-18 8:26
protectorRichard MacCutchan22-May-18 8:26 
AnswerRe: volatile issue - repost Pin
CPallini22-May-18 10:11
mvpCPallini22-May-18 10:11 
AnswerRe: volatile issue - repost Pin
leon de boer22-May-18 19:12
memberleon de boer22-May-18 19:12 
GeneralRe: volatile issue - repost Pin
CPallini22-May-18 21:39
mvpCPallini22-May-18 21:39 
QuestionRe: volatile issue - repost Pin
CPallini22-May-18 10:11
mvpCPallini22-May-18 10:11 
AnswerRe: volatile issue - repost Pin
Vaclav_22-May-18 11:54
memberVaclav_22-May-18 11:54 

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.


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180820.1 | Last Updated 21 Aug 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid