C++, Overloading, Creating functions in the class that receive an array and the size of the array as input,...












-1














I am currently learning C++. I run into troubles when I work on an Overloading problem. The function in the class supposed to receive the array and size of the array as input and output the smallest value. There are three arrays in total including int, float, and char. My code works only under int. I can't understand why I have attached my code below. Can anyone tell my mistake?
I know there must be a better method than mine, I really wanna to figure out why my code doesn't work for float and char case. Any help will be greatly appreicated.



For the int case, it can correctly output the smallest value which is 2. However for the float condition, it keeps giving me 0 instead a number from the float list.



Main function



int main()
{
Compare c;
int arrayInt[5] = {65,43,2,898,678};
float arrayInF[4] = {4.5,4.9,4.3,6.5};
char arrayInC[6] = {'w','z','t','h','e','c'};

std::cout<<c.findSmaller(arrayInt, 5)<<" is the smallest of the input arrayn";
std::cout<<c.findSmaller(arrayInF, 4)<<" is the smallest of the input arrayn";
std::cout<<c.findSmaller(arrayInC, 6)<<" is the smallest of the input arrayn";
return 0;
}


Class



class Compare
{
public:
int findSmaller(int input1,int input2);
float findSmaller(float input1,int input2);
};


int Compare::findSmaller(int input1, int input2)
{
int small;
for(int i=0;i<input2;i++)
{
if(input1[i]<input1[i+1])
{
small = input1[i];
input1[i+1] = small;
}
else
{
small = input1[i+1];
input1[i+1] = small;
}
}
return small;
}

float Compare::findSmaller(float input1, int input2)
{
float small;
for(int i=0;i<input2;i++)
{
if(input1[i]<input1[i+1])
{
small = input1[i];
input1[i+1] = small;
}
else
{
small = input1[i+1];
input1[i+1] = small;
}
}
return small;
}

char Compare::findSmaller(char input1, int input2)
{
char small;
for(int i=0;i<input2;i++)
{
if(input1[i]<input1[i+1])
{
small = input1[i];
input1[i+1] = small;
}
else
{
small = input1[i+1];
input1[i+1] = small;
}
}
return small;
}









share|improve this question
























  • Can you explain how it doesn't work?
    – Ibu
    Nov 20 at 21:06










  • For the int case, it can correctly output the smallest value which is 2. However for the float condition, it keeps giving me 0 instead a number from the float list.
    – MOSOON-E
    Nov 20 at 21:10






  • 2




    Your methods does not work for int. You just happen to get the right answer.
    – Bo R
    Nov 20 at 21:21










  • So many people downvoting without commenting as to why. Just because it's bad code doesn't mean it's a bad question.
    – xaxxon
    Nov 20 at 21:27
















-1














I am currently learning C++. I run into troubles when I work on an Overloading problem. The function in the class supposed to receive the array and size of the array as input and output the smallest value. There are three arrays in total including int, float, and char. My code works only under int. I can't understand why I have attached my code below. Can anyone tell my mistake?
I know there must be a better method than mine, I really wanna to figure out why my code doesn't work for float and char case. Any help will be greatly appreicated.



For the int case, it can correctly output the smallest value which is 2. However for the float condition, it keeps giving me 0 instead a number from the float list.



Main function



int main()
{
Compare c;
int arrayInt[5] = {65,43,2,898,678};
float arrayInF[4] = {4.5,4.9,4.3,6.5};
char arrayInC[6] = {'w','z','t','h','e','c'};

std::cout<<c.findSmaller(arrayInt, 5)<<" is the smallest of the input arrayn";
std::cout<<c.findSmaller(arrayInF, 4)<<" is the smallest of the input arrayn";
std::cout<<c.findSmaller(arrayInC, 6)<<" is the smallest of the input arrayn";
return 0;
}


Class



class Compare
{
public:
int findSmaller(int input1,int input2);
float findSmaller(float input1,int input2);
};


int Compare::findSmaller(int input1, int input2)
{
int small;
for(int i=0;i<input2;i++)
{
if(input1[i]<input1[i+1])
{
small = input1[i];
input1[i+1] = small;
}
else
{
small = input1[i+1];
input1[i+1] = small;
}
}
return small;
}

float Compare::findSmaller(float input1, int input2)
{
float small;
for(int i=0;i<input2;i++)
{
if(input1[i]<input1[i+1])
{
small = input1[i];
input1[i+1] = small;
}
else
{
small = input1[i+1];
input1[i+1] = small;
}
}
return small;
}

char Compare::findSmaller(char input1, int input2)
{
char small;
for(int i=0;i<input2;i++)
{
if(input1[i]<input1[i+1])
{
small = input1[i];
input1[i+1] = small;
}
else
{
small = input1[i+1];
input1[i+1] = small;
}
}
return small;
}









share|improve this question
























  • Can you explain how it doesn't work?
    – Ibu
    Nov 20 at 21:06










  • For the int case, it can correctly output the smallest value which is 2. However for the float condition, it keeps giving me 0 instead a number from the float list.
    – MOSOON-E
    Nov 20 at 21:10






  • 2




    Your methods does not work for int. You just happen to get the right answer.
    – Bo R
    Nov 20 at 21:21










  • So many people downvoting without commenting as to why. Just because it's bad code doesn't mean it's a bad question.
    – xaxxon
    Nov 20 at 21:27














-1












-1








-1


0





I am currently learning C++. I run into troubles when I work on an Overloading problem. The function in the class supposed to receive the array and size of the array as input and output the smallest value. There are three arrays in total including int, float, and char. My code works only under int. I can't understand why I have attached my code below. Can anyone tell my mistake?
I know there must be a better method than mine, I really wanna to figure out why my code doesn't work for float and char case. Any help will be greatly appreicated.



For the int case, it can correctly output the smallest value which is 2. However for the float condition, it keeps giving me 0 instead a number from the float list.



Main function



int main()
{
Compare c;
int arrayInt[5] = {65,43,2,898,678};
float arrayInF[4] = {4.5,4.9,4.3,6.5};
char arrayInC[6] = {'w','z','t','h','e','c'};

std::cout<<c.findSmaller(arrayInt, 5)<<" is the smallest of the input arrayn";
std::cout<<c.findSmaller(arrayInF, 4)<<" is the smallest of the input arrayn";
std::cout<<c.findSmaller(arrayInC, 6)<<" is the smallest of the input arrayn";
return 0;
}


Class



class Compare
{
public:
int findSmaller(int input1,int input2);
float findSmaller(float input1,int input2);
};


int Compare::findSmaller(int input1, int input2)
{
int small;
for(int i=0;i<input2;i++)
{
if(input1[i]<input1[i+1])
{
small = input1[i];
input1[i+1] = small;
}
else
{
small = input1[i+1];
input1[i+1] = small;
}
}
return small;
}

float Compare::findSmaller(float input1, int input2)
{
float small;
for(int i=0;i<input2;i++)
{
if(input1[i]<input1[i+1])
{
small = input1[i];
input1[i+1] = small;
}
else
{
small = input1[i+1];
input1[i+1] = small;
}
}
return small;
}

char Compare::findSmaller(char input1, int input2)
{
char small;
for(int i=0;i<input2;i++)
{
if(input1[i]<input1[i+1])
{
small = input1[i];
input1[i+1] = small;
}
else
{
small = input1[i+1];
input1[i+1] = small;
}
}
return small;
}









share|improve this question















I am currently learning C++. I run into troubles when I work on an Overloading problem. The function in the class supposed to receive the array and size of the array as input and output the smallest value. There are three arrays in total including int, float, and char. My code works only under int. I can't understand why I have attached my code below. Can anyone tell my mistake?
I know there must be a better method than mine, I really wanna to figure out why my code doesn't work for float and char case. Any help will be greatly appreicated.



For the int case, it can correctly output the smallest value which is 2. However for the float condition, it keeps giving me 0 instead a number from the float list.



Main function



int main()
{
Compare c;
int arrayInt[5] = {65,43,2,898,678};
float arrayInF[4] = {4.5,4.9,4.3,6.5};
char arrayInC[6] = {'w','z','t','h','e','c'};

std::cout<<c.findSmaller(arrayInt, 5)<<" is the smallest of the input arrayn";
std::cout<<c.findSmaller(arrayInF, 4)<<" is the smallest of the input arrayn";
std::cout<<c.findSmaller(arrayInC, 6)<<" is the smallest of the input arrayn";
return 0;
}


Class



class Compare
{
public:
int findSmaller(int input1,int input2);
float findSmaller(float input1,int input2);
};


int Compare::findSmaller(int input1, int input2)
{
int small;
for(int i=0;i<input2;i++)
{
if(input1[i]<input1[i+1])
{
small = input1[i];
input1[i+1] = small;
}
else
{
small = input1[i+1];
input1[i+1] = small;
}
}
return small;
}

float Compare::findSmaller(float input1, int input2)
{
float small;
for(int i=0;i<input2;i++)
{
if(input1[i]<input1[i+1])
{
small = input1[i];
input1[i+1] = small;
}
else
{
small = input1[i+1];
input1[i+1] = small;
}
}
return small;
}

char Compare::findSmaller(char input1, int input2)
{
char small;
for(int i=0;i<input2;i++)
{
if(input1[i]<input1[i+1])
{
small = input1[i];
input1[i+1] = small;
}
else
{
small = input1[i+1];
input1[i+1] = small;
}
}
return small;
}






c++ class overloading






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 21:11

























asked Nov 20 at 21:01









MOSOON-E

163




163












  • Can you explain how it doesn't work?
    – Ibu
    Nov 20 at 21:06










  • For the int case, it can correctly output the smallest value which is 2. However for the float condition, it keeps giving me 0 instead a number from the float list.
    – MOSOON-E
    Nov 20 at 21:10






  • 2




    Your methods does not work for int. You just happen to get the right answer.
    – Bo R
    Nov 20 at 21:21










  • So many people downvoting without commenting as to why. Just because it's bad code doesn't mean it's a bad question.
    – xaxxon
    Nov 20 at 21:27


















  • Can you explain how it doesn't work?
    – Ibu
    Nov 20 at 21:06










  • For the int case, it can correctly output the smallest value which is 2. However for the float condition, it keeps giving me 0 instead a number from the float list.
    – MOSOON-E
    Nov 20 at 21:10






  • 2




    Your methods does not work for int. You just happen to get the right answer.
    – Bo R
    Nov 20 at 21:21










  • So many people downvoting without commenting as to why. Just because it's bad code doesn't mean it's a bad question.
    – xaxxon
    Nov 20 at 21:27
















Can you explain how it doesn't work?
– Ibu
Nov 20 at 21:06




Can you explain how it doesn't work?
– Ibu
Nov 20 at 21:06












For the int case, it can correctly output the smallest value which is 2. However for the float condition, it keeps giving me 0 instead a number from the float list.
– MOSOON-E
Nov 20 at 21:10




For the int case, it can correctly output the smallest value which is 2. However for the float condition, it keeps giving me 0 instead a number from the float list.
– MOSOON-E
Nov 20 at 21:10




2




2




Your methods does not work for int. You just happen to get the right answer.
– Bo R
Nov 20 at 21:21




Your methods does not work for int. You just happen to get the right answer.
– Bo R
Nov 20 at 21:21












So many people downvoting without commenting as to why. Just because it's bad code doesn't mean it's a bad question.
– xaxxon
Nov 20 at 21:27




So many people downvoting without commenting as to why. Just because it's bad code doesn't mean it's a bad question.
– xaxxon
Nov 20 at 21:27












3 Answers
3






active

oldest

votes


















1














The reason the code does not work as you expect is two fold




  1. because your algorithm is destructive

  2. because you are overstepping array bounds


your code snippet:



if(input1[i]<input1[i+1]) // <-- 2) over step array when i = (input2 - 1)
{
small = input1[i];
input1[i+1] = small; // <-- 1) this will overwrite the NEXT value
}
else
{
small = input1[i+1];
input1[i+1] = small;
}


if you walk through this with your arrayInt input



int arrayInt[5] = {65,43,2,898,678};


the data becomes {65, 43, 2, 2, 2} as it executes, destroying the original data.



c and c++ use 0 base indexing, meaning a 4 element array is indexed 0, 1, 2, 3, etc so when you are iterating "i < input2" where input2 = 5 the first iteration i will equal 0 and the last iteration i will equal 4. When your code then makes reference to input1[i+1] that would then be input1[5] which is out of bounds but not necessarily undefined nor 0. You see, the compiler will try to allocate an array in a continuous block of memory like so:



| item 0 | item 1 | item 2 | item 3 | item 4 | etc.



referencing input1[5] will simply return the next block of memory interpreted as the expected data type, an integer in the case of arrayInt.



Since the 3 arrays are declared together, the compiler allocated their space together, this means that arrayInt is adjacent to arrayInf in physical memory, which also means that arrayInt[5] would be the same as (int)arrayInf[0]. 4.5 float is a large integer and will engage the destructive nature of your algorithm, meaning that when iterating over the arrayInt you actually overwrote the 4.5 float with an integer 2 and that's going to be interpreted as a really small float, so you've clobbered the first element of the arrayInf array.



@Bo-r gives an example of a better algorithm for doing what you want.






share|improve this answer





















  • Thanks so much for your detailed walkthrough. Really appreciate it.
    – MOSOON-E
    Nov 20 at 23:19



















0














float arrayInF[4] = {4.5, 4.9, 4.3, 6.5};


has 4 values (use whitespace in your code, it makes reading it much easier)



You pass in an input2 (use more descriptive variable names, too) of 4 which means that



for(int i=0;i<input2;i++)


i goes up to 3.



You then access array indices 3 and 3+1=4 here (and other places as well):



if(input1[i]<input1[i+1])


When you only have valid indices up to 3, which completely breaks your program. Once you read/write invalid memory locations, the behavior of your program becomes undefined. It may still look like it's working sometimes, but that's just sheer luck.



This problem is not limited to just the float implementation.






share|improve this answer































    -2














    Seems you haven't declare and implemented the method char Compare::findSmaller(char *input1, int input2).



    A example of such an implementation would be:



    char Compare::findSmaller(char input1, int input2) {
    assert(input2 >0);
    char small = input1[0];
    for (int i = 1; i < input2; i++)
    if (input1[i] < small)
    small = input1[i];
    return small;
    }





    share|improve this answer



















    • 1




      This doesn't answer the question (although it is relevant and should be a comment).
      – Matthieu Brucher
      Nov 20 at 21:13










    • Thanks for your help, I have added the char class, however, like Matthieu said, it doesn't solve my question.
      – MOSOON-E
      Nov 20 at 21:17











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401466%2fc-overloading-creating-functions-in-the-class-that-receive-an-array-and-the%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The reason the code does not work as you expect is two fold




    1. because your algorithm is destructive

    2. because you are overstepping array bounds


    your code snippet:



    if(input1[i]<input1[i+1]) // <-- 2) over step array when i = (input2 - 1)
    {
    small = input1[i];
    input1[i+1] = small; // <-- 1) this will overwrite the NEXT value
    }
    else
    {
    small = input1[i+1];
    input1[i+1] = small;
    }


    if you walk through this with your arrayInt input



    int arrayInt[5] = {65,43,2,898,678};


    the data becomes {65, 43, 2, 2, 2} as it executes, destroying the original data.



    c and c++ use 0 base indexing, meaning a 4 element array is indexed 0, 1, 2, 3, etc so when you are iterating "i < input2" where input2 = 5 the first iteration i will equal 0 and the last iteration i will equal 4. When your code then makes reference to input1[i+1] that would then be input1[5] which is out of bounds but not necessarily undefined nor 0. You see, the compiler will try to allocate an array in a continuous block of memory like so:



    | item 0 | item 1 | item 2 | item 3 | item 4 | etc.



    referencing input1[5] will simply return the next block of memory interpreted as the expected data type, an integer in the case of arrayInt.



    Since the 3 arrays are declared together, the compiler allocated their space together, this means that arrayInt is adjacent to arrayInf in physical memory, which also means that arrayInt[5] would be the same as (int)arrayInf[0]. 4.5 float is a large integer and will engage the destructive nature of your algorithm, meaning that when iterating over the arrayInt you actually overwrote the 4.5 float with an integer 2 and that's going to be interpreted as a really small float, so you've clobbered the first element of the arrayInf array.



    @Bo-r gives an example of a better algorithm for doing what you want.






    share|improve this answer





















    • Thanks so much for your detailed walkthrough. Really appreciate it.
      – MOSOON-E
      Nov 20 at 23:19
















    1














    The reason the code does not work as you expect is two fold




    1. because your algorithm is destructive

    2. because you are overstepping array bounds


    your code snippet:



    if(input1[i]<input1[i+1]) // <-- 2) over step array when i = (input2 - 1)
    {
    small = input1[i];
    input1[i+1] = small; // <-- 1) this will overwrite the NEXT value
    }
    else
    {
    small = input1[i+1];
    input1[i+1] = small;
    }


    if you walk through this with your arrayInt input



    int arrayInt[5] = {65,43,2,898,678};


    the data becomes {65, 43, 2, 2, 2} as it executes, destroying the original data.



    c and c++ use 0 base indexing, meaning a 4 element array is indexed 0, 1, 2, 3, etc so when you are iterating "i < input2" where input2 = 5 the first iteration i will equal 0 and the last iteration i will equal 4. When your code then makes reference to input1[i+1] that would then be input1[5] which is out of bounds but not necessarily undefined nor 0. You see, the compiler will try to allocate an array in a continuous block of memory like so:



    | item 0 | item 1 | item 2 | item 3 | item 4 | etc.



    referencing input1[5] will simply return the next block of memory interpreted as the expected data type, an integer in the case of arrayInt.



    Since the 3 arrays are declared together, the compiler allocated their space together, this means that arrayInt is adjacent to arrayInf in physical memory, which also means that arrayInt[5] would be the same as (int)arrayInf[0]. 4.5 float is a large integer and will engage the destructive nature of your algorithm, meaning that when iterating over the arrayInt you actually overwrote the 4.5 float with an integer 2 and that's going to be interpreted as a really small float, so you've clobbered the first element of the arrayInf array.



    @Bo-r gives an example of a better algorithm for doing what you want.






    share|improve this answer





















    • Thanks so much for your detailed walkthrough. Really appreciate it.
      – MOSOON-E
      Nov 20 at 23:19














    1












    1








    1






    The reason the code does not work as you expect is two fold




    1. because your algorithm is destructive

    2. because you are overstepping array bounds


    your code snippet:



    if(input1[i]<input1[i+1]) // <-- 2) over step array when i = (input2 - 1)
    {
    small = input1[i];
    input1[i+1] = small; // <-- 1) this will overwrite the NEXT value
    }
    else
    {
    small = input1[i+1];
    input1[i+1] = small;
    }


    if you walk through this with your arrayInt input



    int arrayInt[5] = {65,43,2,898,678};


    the data becomes {65, 43, 2, 2, 2} as it executes, destroying the original data.



    c and c++ use 0 base indexing, meaning a 4 element array is indexed 0, 1, 2, 3, etc so when you are iterating "i < input2" where input2 = 5 the first iteration i will equal 0 and the last iteration i will equal 4. When your code then makes reference to input1[i+1] that would then be input1[5] which is out of bounds but not necessarily undefined nor 0. You see, the compiler will try to allocate an array in a continuous block of memory like so:



    | item 0 | item 1 | item 2 | item 3 | item 4 | etc.



    referencing input1[5] will simply return the next block of memory interpreted as the expected data type, an integer in the case of arrayInt.



    Since the 3 arrays are declared together, the compiler allocated their space together, this means that arrayInt is adjacent to arrayInf in physical memory, which also means that arrayInt[5] would be the same as (int)arrayInf[0]. 4.5 float is a large integer and will engage the destructive nature of your algorithm, meaning that when iterating over the arrayInt you actually overwrote the 4.5 float with an integer 2 and that's going to be interpreted as a really small float, so you've clobbered the first element of the arrayInf array.



    @Bo-r gives an example of a better algorithm for doing what you want.






    share|improve this answer












    The reason the code does not work as you expect is two fold




    1. because your algorithm is destructive

    2. because you are overstepping array bounds


    your code snippet:



    if(input1[i]<input1[i+1]) // <-- 2) over step array when i = (input2 - 1)
    {
    small = input1[i];
    input1[i+1] = small; // <-- 1) this will overwrite the NEXT value
    }
    else
    {
    small = input1[i+1];
    input1[i+1] = small;
    }


    if you walk through this with your arrayInt input



    int arrayInt[5] = {65,43,2,898,678};


    the data becomes {65, 43, 2, 2, 2} as it executes, destroying the original data.



    c and c++ use 0 base indexing, meaning a 4 element array is indexed 0, 1, 2, 3, etc so when you are iterating "i < input2" where input2 = 5 the first iteration i will equal 0 and the last iteration i will equal 4. When your code then makes reference to input1[i+1] that would then be input1[5] which is out of bounds but not necessarily undefined nor 0. You see, the compiler will try to allocate an array in a continuous block of memory like so:



    | item 0 | item 1 | item 2 | item 3 | item 4 | etc.



    referencing input1[5] will simply return the next block of memory interpreted as the expected data type, an integer in the case of arrayInt.



    Since the 3 arrays are declared together, the compiler allocated their space together, this means that arrayInt is adjacent to arrayInf in physical memory, which also means that arrayInt[5] would be the same as (int)arrayInf[0]. 4.5 float is a large integer and will engage the destructive nature of your algorithm, meaning that when iterating over the arrayInt you actually overwrote the 4.5 float with an integer 2 and that's going to be interpreted as a really small float, so you've clobbered the first element of the arrayInf array.



    @Bo-r gives an example of a better algorithm for doing what you want.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 at 23:14









    J R

    112




    112












    • Thanks so much for your detailed walkthrough. Really appreciate it.
      – MOSOON-E
      Nov 20 at 23:19


















    • Thanks so much for your detailed walkthrough. Really appreciate it.
      – MOSOON-E
      Nov 20 at 23:19
















    Thanks so much for your detailed walkthrough. Really appreciate it.
    – MOSOON-E
    Nov 20 at 23:19




    Thanks so much for your detailed walkthrough. Really appreciate it.
    – MOSOON-E
    Nov 20 at 23:19













    0














    float arrayInF[4] = {4.5, 4.9, 4.3, 6.5};


    has 4 values (use whitespace in your code, it makes reading it much easier)



    You pass in an input2 (use more descriptive variable names, too) of 4 which means that



    for(int i=0;i<input2;i++)


    i goes up to 3.



    You then access array indices 3 and 3+1=4 here (and other places as well):



    if(input1[i]<input1[i+1])


    When you only have valid indices up to 3, which completely breaks your program. Once you read/write invalid memory locations, the behavior of your program becomes undefined. It may still look like it's working sometimes, but that's just sheer luck.



    This problem is not limited to just the float implementation.






    share|improve this answer




























      0














      float arrayInF[4] = {4.5, 4.9, 4.3, 6.5};


      has 4 values (use whitespace in your code, it makes reading it much easier)



      You pass in an input2 (use more descriptive variable names, too) of 4 which means that



      for(int i=0;i<input2;i++)


      i goes up to 3.



      You then access array indices 3 and 3+1=4 here (and other places as well):



      if(input1[i]<input1[i+1])


      When you only have valid indices up to 3, which completely breaks your program. Once you read/write invalid memory locations, the behavior of your program becomes undefined. It may still look like it's working sometimes, but that's just sheer luck.



      This problem is not limited to just the float implementation.






      share|improve this answer


























        0












        0








        0






        float arrayInF[4] = {4.5, 4.9, 4.3, 6.5};


        has 4 values (use whitespace in your code, it makes reading it much easier)



        You pass in an input2 (use more descriptive variable names, too) of 4 which means that



        for(int i=0;i<input2;i++)


        i goes up to 3.



        You then access array indices 3 and 3+1=4 here (and other places as well):



        if(input1[i]<input1[i+1])


        When you only have valid indices up to 3, which completely breaks your program. Once you read/write invalid memory locations, the behavior of your program becomes undefined. It may still look like it's working sometimes, but that's just sheer luck.



        This problem is not limited to just the float implementation.






        share|improve this answer














        float arrayInF[4] = {4.5, 4.9, 4.3, 6.5};


        has 4 values (use whitespace in your code, it makes reading it much easier)



        You pass in an input2 (use more descriptive variable names, too) of 4 which means that



        for(int i=0;i<input2;i++)


        i goes up to 3.



        You then access array indices 3 and 3+1=4 here (and other places as well):



        if(input1[i]<input1[i+1])


        When you only have valid indices up to 3, which completely breaks your program. Once you read/write invalid memory locations, the behavior of your program becomes undefined. It may still look like it's working sometimes, but that's just sheer luck.



        This problem is not limited to just the float implementation.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 at 21:24

























        answered Nov 20 at 21:18









        xaxxon

        14.3k43059




        14.3k43059























            -2














            Seems you haven't declare and implemented the method char Compare::findSmaller(char *input1, int input2).



            A example of such an implementation would be:



            char Compare::findSmaller(char input1, int input2) {
            assert(input2 >0);
            char small = input1[0];
            for (int i = 1; i < input2; i++)
            if (input1[i] < small)
            small = input1[i];
            return small;
            }





            share|improve this answer



















            • 1




              This doesn't answer the question (although it is relevant and should be a comment).
              – Matthieu Brucher
              Nov 20 at 21:13










            • Thanks for your help, I have added the char class, however, like Matthieu said, it doesn't solve my question.
              – MOSOON-E
              Nov 20 at 21:17
















            -2














            Seems you haven't declare and implemented the method char Compare::findSmaller(char *input1, int input2).



            A example of such an implementation would be:



            char Compare::findSmaller(char input1, int input2) {
            assert(input2 >0);
            char small = input1[0];
            for (int i = 1; i < input2; i++)
            if (input1[i] < small)
            small = input1[i];
            return small;
            }





            share|improve this answer



















            • 1




              This doesn't answer the question (although it is relevant and should be a comment).
              – Matthieu Brucher
              Nov 20 at 21:13










            • Thanks for your help, I have added the char class, however, like Matthieu said, it doesn't solve my question.
              – MOSOON-E
              Nov 20 at 21:17














            -2












            -2








            -2






            Seems you haven't declare and implemented the method char Compare::findSmaller(char *input1, int input2).



            A example of such an implementation would be:



            char Compare::findSmaller(char input1, int input2) {
            assert(input2 >0);
            char small = input1[0];
            for (int i = 1; i < input2; i++)
            if (input1[i] < small)
            small = input1[i];
            return small;
            }





            share|improve this answer














            Seems you haven't declare and implemented the method char Compare::findSmaller(char *input1, int input2).



            A example of such an implementation would be:



            char Compare::findSmaller(char input1, int input2) {
            assert(input2 >0);
            char small = input1[0];
            for (int i = 1; i < input2; i++)
            if (input1[i] < small)
            small = input1[i];
            return small;
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 20 at 21:19

























            answered Nov 20 at 21:11









            Bo R

            616110




            616110








            • 1




              This doesn't answer the question (although it is relevant and should be a comment).
              – Matthieu Brucher
              Nov 20 at 21:13










            • Thanks for your help, I have added the char class, however, like Matthieu said, it doesn't solve my question.
              – MOSOON-E
              Nov 20 at 21:17














            • 1




              This doesn't answer the question (although it is relevant and should be a comment).
              – Matthieu Brucher
              Nov 20 at 21:13










            • Thanks for your help, I have added the char class, however, like Matthieu said, it doesn't solve my question.
              – MOSOON-E
              Nov 20 at 21:17








            1




            1




            This doesn't answer the question (although it is relevant and should be a comment).
            – Matthieu Brucher
            Nov 20 at 21:13




            This doesn't answer the question (although it is relevant and should be a comment).
            – Matthieu Brucher
            Nov 20 at 21:13












            Thanks for your help, I have added the char class, however, like Matthieu said, it doesn't solve my question.
            – MOSOON-E
            Nov 20 at 21:17




            Thanks for your help, I have added the char class, however, like Matthieu said, it doesn't solve my question.
            – MOSOON-E
            Nov 20 at 21:17


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401466%2fc-overloading-creating-functions-in-the-class-that-receive-an-array-and-the%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'