why it doesn't working?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: why it doesn't working?

  1. #1
    Join Date
    Nov 2013
    Posts
    11

    why it doesn't working?

    Hi.i program a software to get scores from admin and sort them ascending then show the middle score(if admin enter middle).but when i compile it nothing happen.what should i do?here's code ,i use dev compiler
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <math.h>
    #include <string.h>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int counter,j,count,counter2,m;
        char function[80];
        float scores,varyans,middle,ave,sum=0,i;
        float student [100];
        cout<<"enter scores\n";
        cout<<"for end enter-1\n";
        for(counter=0;counter<100;counter++)
        {
                                            cin>>student[counter];
                                            if(student[counter]==-1)
                                            break;
                                            }
                                            
                                   count=counter;          
        cout<<"what to do\n";
        cin>>function;
        if(strcmp(function,"middle")==0)
        {
                                     for( ;counter>=0;count--)
                                     for(j=0;j<=count;j++)
                                     if(student[j]>student[j+1])
                                     {
                                                               i=student[j];
                                                               student[j]=student[j+1];
                                                               student[j+1]=i;
                                                               }
                                                               for(;count>0;count--)
                                                               cout<<student[count];
                                                               }
                                                    m=count/2;
                                                    cout<<student[m]; 
      cout<<"\n";
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: why it doesn't working?

    What debugging of the program have you done? If you trace through the operation of the progam using the debugger you will soon see the various problems. Being able to debug properly programs is a skill that all programmers need to learn and acquire.

    I suspect this is an assignment so I won't provide the corrected code but this is some guidance.

    As your final -1 is entered into the array, your count of the number of entries is too great.

    Your outer sort loop has a condition on counter but changes count.

    Arrays are indexed from 0 to one less than the number of elements. Your inner sort loop j accesses the final -1 element for j and then tries to compare it to element j + 1 which is outside the number of entered elements and could be any value. If you entered 99 data items this would cause a buffer overflow problem.

    You are printing the elements descending not ascending.

    Your output loop after the sort doesn't print the final number as it doesn't access student[0].

    If you select function middle, count will be -1 (or 0 in your case at the moment) after the numbers have been printed so m will be the wrong value and hence it won't output the correct middle number.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Nov 2013
    Posts
    11

    Re: why it doesn't working?

    thanks,but i dont understood how to solve it and the correct code is not needed,just if you could guide me more.
    it means the last if should be :if(student[j-1]<student[j]
    everything would be correct??

  4. #4
    Join Date
    Nov 2013
    Posts
    11

    Re: why it doesn't working?

    hi again.i change the main code to this but the output is not that i wanted.what should i do????
    Code:
    for( ;counter>=0;counter--)
                                     for(j=0;j<=count;j++)
                                     if(student[j-1]<student[j])
                                     {
                                                               i=student[j-1];
                                                               student[j-1]=student[j];
                                                               student[j]=i;
                                                               }
                                                               for(;count>1;count--)
                                                               cout<<student[count];
                                                               }
                                                    m=count/2;
                                                    cout<<student[m];
    Last edited by Faraz95; November 23rd, 2013 at 08:47 AM.

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: why it doesn't working?

    but the output is not that i wanted.what should i do????
    Debug your program using the debugger! If you don't know how to use the debugger, now is the time to learn. It's not difficult. What compiler are you using?

    Even without using the debugger, it is quite easy to find the mistakes in this simple code using a pencil and paper. Walk through the program manually and write down on paper the contents of the variables as they are changed and see where the code deviates from what is expected from your program design. You could even put appropriate cout statments in the code to see what is happening when you run the code.

    How has your tutor explained how to find run-time errors in code?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,423

    Re: why it doesn't working?

    Quote Originally Posted by Faraz95 View Post
    thanks,but i dont understood how to solve it
    Debug your code. Why haven't you done that? That is how you solve the problem. Asking expert or professional programmers to debug code you wrote is not how you learn how to write a program. Debugging is part of learning how to write a program -- you're just going to have to learn how to debug.

    When you write a program, you're supposed to understand what every line, function, etc. is supposed to do. If those lines of code do not perform correctly, then you debug the program to see where the code goes against your plans, and fix the code accordingly. Instead of 100 students, how about just trying 4 or 5 students? Step through your program with a debugger and see where it fails.

    You were told already that you were going beyond the boundaries of the array, but you didn't fix it. If you tried 4,5, or even 3 students, you should see where your code fails. Change your arrays to only 3 students instead of 100. You should see easily that you're comparing the third student to the fourth student, and there is no fourth student.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 23rd, 2013 at 10:21 AM.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,423

    Re: why it doesn't working?

    For example, take a look at your input loop. What if you entered 2 students?
    Code:
    for(counter=0;counter<100;counter++)
    {
        cin >> student[counter];
        if(student[counter]==-1)
                break;
    }
    count=counter;
    To enter only 2 student, that loop has to be performed three times (the first two times for the students, and then again to enter a -1). What is the value of count? It will be 2. Now what happens here?
    Code:
    for(j=0;j<=count;j++)
        if(student[j-1]<student[j])
    So what happens when j is 0? You are comparing student[j-1] (which is student[-1]) to student [0]. Does that sound right to you? You are comparing a student that doesn't exist, and you are accessing the array out of bounds.

    Now at the other end, what happens if j is 2? You are comparing student[2], but student[2] is equal to -1 (the -1 you entered to stop the input loop). The only students that can be compared are student[0] and student[1]. So with just 2 students, your code fails.

    Also, if you entered just 1 student, your code would fail because it will be trying to sort a single student.

    But if you just debugged your code, you should have seen easily seen this.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 23rd, 2013 at 10:43 AM.

  8. #8
    Join Date
    Nov 2013
    Posts
    11

    Re: why it doesn't working?

    Quote Originally Posted by 2kaud View Post
    Debug your program using the debugger! If you don't know how to use the debugger, now is the time to learn. It's not difficult. What compiler are you using?

    Even without using the debugger, it is quite easy to find the mistakes in this simple code using a pencil and paper. Walk through the program manually and write down on paper the contents of the variables as they are changed and see where the code deviates from what is expected from your program design. You could even put appropriate cout statments in the code to see what is happening when you run the code.

    How has your tutor explained how to find run-time errors in code?
    i use dev c++ compiler

  9. #9
    Join Date
    Nov 2013
    Posts
    11

    Re: why it doesn't working?

    again i change the code to this but aint solved
    Code:
     for( ;counter>=0;counter--)
                                     for(j=0;j<=(counter-1);j++)
                                     if(student[j]<student[j+1])
                                     {
                                                               i=student[j];
                                                               student[j]=student[j+1];
                                                               student[j+1]=i;
                                                               }
                                                               for(count=counter-1;count>=0;count--)
                                                               cout<<student[count];
                                                               }
                                                    m=(counter)/2;
                                                    cout<<"\n"<<student[m];

  10. #10
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,288

    Re: why it doesn't working?

    Quote Originally Posted by Faraz95 View Post
    i use dev c++ compiler
    Do you mean your "dev c++ compiler" does not have a debugger?
    Or you don't know what debugger is?
    Victor Nijegorodov

  11. #11
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: why it doesn't working?

    There are plenty of web resources available about the dev c++ debugger.

    https://www.google.co.uk/search?q=de...en-GB:official
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  12. #12
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: why it doesn't working?

    Quote Originally Posted by Faraz95 View Post
    again i change the code to this but aint solved
    You seem to be just trying things to see what happens. You can't do this with programs. First design the program, then code the program from the design, then test/debug the code against the design. This is what professional programmers do. They don't just write some code and hope for the best. When something doesn't work as expected, we use the debugger to see why, where the code is deviating from what was expected and then change the code appropriately.

    You need to use the debugger to work through your code and see where is the problem.
    We're not going to fix this program for you as it's an assignment and that would be cheating. I gave you a lot of guidance in my post #2 together with Paul's comments in his post #7.

    See http://forums.codeguru.com/showthrea...ork-assignment
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  13. #13
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: why it doesn't working?

    You're trying to do a simple bubble sort and getting the conditions not quite right. Do a web search for bubble sort.

    https://www.google.co.uk/search?q=bu...Hsva0QWjtIHYDQ

    http://mathbits.com/MathBits/CompSci/Arrays/Bubble.htm
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  14. #14
    Join Date
    Apr 1999
    Posts
    27,423

    Re: why it doesn't working?

    Quote Originally Posted by Faraz95 View Post
    again i change the code to this but aint solved
    So the question to you is this:

    Why did you change the code to this new code?

    If your answer is "I don't know" or "I'm trying everything", then that is the wrong answer. You only change code if you know that the changes will produce the right results. The only way to know that is to debug the program and then determine what the corrections are. Just writing code and trying things to see if they work is not how you write programs (as 2kaud pointed out).

    It seems that you are getting the impression that if the program doesn't work, then something unusual is happening and then you must call "help!!!" on a programming forum. That isn't the case -- almost every program that is written will have bugs in it that have to be fixed. There is nothing unusual if a program initially has bugs -- that is expected. Since that is expected, then the other thing that is expected is for the programmer to know how to fix the bugs. Again, fixing bugs is part of learning how to write programs.

    Regards,

    Paul McKenzie

  15. #15
    Join Date
    Nov 2013
    Posts
    11

    Re: why it doesn't working?

    you know,i understand how to solve it and it's over.but i want to know that how can i use debuger of a compiler?
    i use dev c++ compiler

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center