为什么我的程序不起作用

Why is My Program not Working

本文关键字:不起作用 程序 我的 为什么      更新时间:2023-10-16

我是一名菜鸟程序员,刚开始C++。我写了一个程序来回答一个问题。当我尝试从我的cmd.exe运行它时,Windows告诉我"一个问题导致该程序停止工作,我们将关闭该程序并在解决方案可用时通知您"。

我已经包含一个指向文档齐全的源代码的链接。请看一下代码,并帮助我。链接: http://mibpaste.com/ZRevGf

我相信,用我的代码找出错误可能会帮助其他几个菜鸟程序员,他们可能会使用与我类似的方法。

来自链接的代码:

 //This is the source code for a puzzle,well kind of that I saw on the internet. I will include the puzzle's question below.
//Well, I commented it so I hope you understand.

//ALAFIN OLUWATOBI 100L DEPARTMENT OF COMPUTER SCIENCE BABCOCK UNIVERSITY.
//Future CEO of VERI Technologies inc.

/*
* In a corridor, there are 100 doors. All the doors are initially closed.
* You walk along the corridor back and forth. As you walk along the corridor, you reverse the state of each door.
* I.e if the door is open, you close it, and if it is closed, you open it.
* You walk along the corrdor, a total of 200 times.
* On your nth trip, You stop at every nth door, that you come across.
* I.e on your first trip, you stop at every door. On your second trip, every second door, on your third trip every third door and so on and so forth
* Write a program to display, the final states of the doors.
*/

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
inline void inverse(bool args[]); //The prototype of the function. I made the function inline in the declaration, to increase efficiency, ad speed of execution.
bool doors [200]; //Declaring a global array, for the doors.
int main ()
{
    inverse(doors); //A call to the inverse function
    cout << "This is the state of the 100 doors...n";
    for (int i = 0 ; i<200 ; i++) //Loop, to dis play the final states of the doors.
    {
        cout << "DOOR " << (i+1) << "t|" << doors[i] << endl;
    }
    cout << "Thank you, for using this program designed by VERI Technologies. :)"; //VERI Technologies, is the name of the I.T company that I hope to establish.
    return 0;
}
void inverse(bool args [])
{
    for (int n = 1 ; n<= 200 ; n++) //This loop, is for the control of every nth trip. It executes 100 times
    {
        if (n%2 != 0) //This is to control the reversal of the doors going forward, I.e on odd numbers
        {
            for (int b = n, a = 1 ; b<=200 ;b = n*++a) //This is the control loop, for every odd trip, going forwards. It executes 100 times
                args [b] = !args[b] ; //The reversal operation. It reverses the boolean value of the door.
        }
/*
* The two variables, are declared. They will be used in controlling the program. b represents the number of the door to be operated on.
* a is a variable, which we shall use to control the value of b.
* n remains constant for the duration, of the loop, as does (200-n)
* the pre increment of a {++a} multiplied by n or (200-n) is used to calculate the value of b in the update.
* Thus, we have the scenario, of b increasing in multiples of n. Achieving what is desired for the program. Through this construct, only every nth door is considered.
*/
        else if((n%2) == 0) //This is to control the reversal of the doors going backwards, I.e on even numbers
        {
            for (int b = (200-n), a = 1 ; b>=1 ; b = (200-n)*++a) //This is the control loop for every even trip, going backwards. It executes 100 times.
                args [b] = !args[b] ; //The reversal operation. It reverses the boolean value of the door.
        }
    }
}
我相信

异常是由于以下行:

for (int b = (200 - n), a = 1; b >= 1; b = (200 - n)*++a)

发生异常时,将为变量分配以下值:

b = 3366
n = 2
a = 17

据我所知,b 由 (200 - n) * a 计算得出。

如果我们替换给定的值,我们有:198 * 17

这给了我们 3366 的值,它超出了门的索引,并在

args[b] = !args[b]; 

被执行。

我已经创建了以下解决方案,如果您希望使用它,它应该提供所需的结果。

void inverse(bool args[])
{
    //n represents what trip you are taking down the hallway
    //i.e. n = 1 is the first trip, n = 2 the second, and so on
    for (int n = 1; n <= 200; n++){
        //We are on trip n, so now we must change the state of all the doors for the trip
        //The current door is represented by i
        //i.e. i = 1 is the first door, i = 2 the second, and so on
        for (int i = 1; i <= 200; i++){
            //If the current door mod the trip is 0 then we must change the state of the door
            //Only the nth door will be changed which occurs when i mod n equals 0
            //We modify the state of doors[i - 1] as the array of doors is 0 - 199 but we are counting doors from 1 to 200
            //So door 1 mod trip 1 will equal 0 so we must change the state of door 1, which is really doors[0]
            if (i % n == 0){
                args[i - 1] = !args[i - 1];
            }
    }
}

EUREKA!!!!!

我终于想出了一个可行的解决方案。没有更多的错误。我称之为2.0.0版我已经在线上传了它,这是链接

[版本 2.0.0] http://mibpaste.com/3NADgl

剩下的就是去超越,并得出门的最终状态,并确保它工作得很好。请查看我的解决方案,并评论我可能犯的任何错误,或者您认为我可以优化代码的任何方式。感谢您的帮助,它使我能够重新设计该程序的工作解决方案。我开始认为越界错误可能导致我的版本 1 崩溃,但无论如何,逻辑是有缺陷的,所以我正在废弃它。

这是代码:

/**********************************************************************************************
200 DOOR PROGRAM
Version 2.0.0
Author: Alafin OluwaTobi Department of Computer Science, Babcock University
New Additions: I redrew, the algorithm, to geneate a more logically viable solution,
I additionally, expanded the size of the array, to prevent a potential out of bounds error.
**********************************************************************************************/
//Hello. This a program,I've written to solve a fun mental problem.
//I'll include a full explanation of the problem, below.
/**********************************************************************************************
    *You are in a Hallway, filled with 200 doors . 
    *ALL the doors are initially closed . 
    *You walk along the corridor, *BACK* and *FORTH* reversing the state of every door which you stop at .
    *I.e if it is open, you close it .
    *If it is closed,  you open it .
    *On every nth trip, you stop at every nth door .
    *I.e on your first trip, you stop at every door. On your second trip every second door, On your third trip every third door, etc .
*Write a program to display the final state of the doors .
**********************************************************************************************/
/**********************************************************************************************
                                        SOLUTION
    *NOTE: on even trips, your coming back, while on odd trips your going forwards .
    *2 Imaginary doors, door 0 and 201, delimit the corridor .
    *On odd trips, the doors stopped at will be (0+n) doors .
    *I.e you will be counting forward, in (0+n) e.g say, n = 5: 5, 10, 15, 20, 25
    *On even trips, the doors stopped at will be (201-n) doors.
    *I.e you will be counting backwards in (201-n) say n = 4: 197, 193, 189, 185, 181
**********************************************************************************************/
#include <iostream>
#include <cstdlib>  //Including the basic libraries
bool HALLWAY [202] ; 
/*
    *Declaring the array, for the Hallway, as global in order to initialise all the elements at zero.
    *In addition,the size is set at 202 to make provision for the delimiting imaginary doors, 
    *This also serves to prevent potential out of bound errors, that may occur, in the use of thefor looplater on.
*/
inline void inverse (bool args []) ;
/*
    *Prototyping the function, which will be used to reverse the states of the door.
    *The function, has been declared as inline in order to allow faster compilation, and generate a faster executable program.
*/
using namespace std ;   //Using the standard namespace
int main ()
{
    inverse (HALLWAY) ; //Calling the inverse function, to act on the Hallway, reversing the doors.
    cout << "tttttttttt200 DOOR TABLEn" ;
    for(int i = 1 ; i <= 200 ; i++ )
    //A loop to display the states of the doors.
    {
        if (HALLWAY [i] == 0) 
        //The if construct allows us to print out the state of the door as closed, when the corresponding element of the Array has a value of zero.
        {
            cout << "DOOR " << i << " istCLOSED" << endl ;
            for (int z = 0 ; z <= 300 ; z++)
                cout << "_" ;
            cout << "n" ;
        }
        else if (HALLWAY [i] == 1)
        //The else if construct allows us to print out the state of the door as open, when the corresponding element of the Array has a value of one.
        {
            cout << "DOOR " << i << " istOPEN" << endl ;
            for (int z = 0 ; z <= 300 ; z++)
                cout << "_" ;
            cout << "n" ;
        }
    } 
return 0 ; //Returns the value of zero, to show that the program executed properly
}
void inverse (bool args[])`
{
    for ( int n = 1; n <= 200 ; n++)
    //This loop, is to control the individual trips, i.e trip 1, 2, 3, etc..
    {
        if (n%2 == 0)
        //This if construct, is to ensure that on even numbers(i,e n%2 = 0), that you are coming down the hallway and counting backwards
    {
            for (int b = (201-n) ; b <= 200 && b >= 1 ; b -= n)
            /*
                *This loop, is for the doors that you stop at on your nth trip. 
                *The door is represented by the variable b.
                *Because you are coming back, b will be reducing proportionally, in n.
                *The Starting value for b on your nth trip, will be (201-n)
                * {b -= n} takes care of this. On the second turn for example. First value of b will be 199, 197, 195, 193, ..., 1
            */
                args [b] = !(args [b]) ;
                //This is the actual reversal operation, which reverses the state of the door.
       }
        else if (n%2 != 0)
        //This else if construct, is to ensure that on odd numbers(i.e n%2 != 0), that you are going up the hallway and counting forwards
        {
            for (int b = n ; b <= 200 && b >= 1 ; b += n)
            /*
                *This loop, is for the doors that you stop at on your nth trip. 
                *The door is represented by the variable b.
                *Because you are going forwards, b will be increasing proportionally, in n.
                *The starting value of b will be (0+n) whch is equal to n
                * {b += n} takes care of this. On the third turn for example. First value of b will be 3, 6, 9, 12, ...., 198
            */
                args [b] = !(args [b]) ;
                //This is the actual reversal operation, which reverses the state of the door
        }
    }
}