为什么程序崩溃.. [英] Why program is crashed..

查看:90
本文介绍了为什么程序崩溃..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.      'S D'. D is an integer. D will be added with all the elements of the array.
2.      'M D'. D is an integer. All the elements of the array will be multiplied by D.
3.      'D K'. K is a non zero integer. All the elements of the array will be divided by K (integer division).
4.      'R'. It means reverse. It will reverse the elements of the array.
5.      'P Y Z'. Y and Z are integers. It will swap the elements a[Y] and a[Z].







i试图做数组中的上述模拟......但程序崩溃...



我尝试过的事情:






i was trying to do the above simulation in array...but program chrashed...

What I have tried:

#include <iostream>
#include
using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    while(scanf("%d",&n)==1)
    {
        for(int i=0; i<n;>        {
            int x,y;
            scanf("%d%d",&x,&y);
            float a[x];
            for(int j=0; j<x;>            {
                scanf("%lf",&a[j]);
            }
            for(int j=0;j<y;j++)>
            {
                char c;
                cin>>c;
                if(c=='S')
                {
                    float b;
                    scanf("%lf",&b);
                    for(int k=0; k<x;>                        a[k]+=b;

                }
                else if(c=='D')
                {
                     float b;
                    scanf("%lf",&b);
                    for(int k=0; k<x;>                        a[k]/=b;
                }
                else if(c=='M')
                {
                    float b;
                    scanf("%lf",&b);
                    for(int k=0; k<x;>                        a[k]*=b;
                }
                else if(c=='P')
                {
                    int b,d;
                    scanf("%d%d",&b,&d);
                    swap(a[b],a[d]);
                }
                else if(c=='R')
                {

                    double temp=0;
                    for(int k=0; k                    {
                        temp=a[k];
                        a[k]=a[x-1-k];
                             a[x-k-1]=temp;
                    }
                }
            }
            cout<<"Case "<<i+1<<":\n";
            for(int j=0; j<x;>            {
                if(j==x-1)
                {
                    cout<<a[j]<<endl;
                    break;
                }
                cout<<a[j]<<" ";
            }

        }
    }


    //cout << "Hello world!" << endl;
    return 0;
}

推荐答案

此代码部分无法编译:

This code part will not compile:
int x,y;
scanf("%d%d",&x,&y);
float a[x];





你必须分配数组并在不再使用时释放它:



You must allocate an array and free it when no longer used:

// Place this on top of function (outside any loop)
float *a = NULL;

// ...

int x,y;
scanf("%d%d",&x,&y);
// Delete old array
delete [] a;
// Allocate new array
a = new float[x];

// ...

// On bottom of function
delete [] a;


您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



您的代码无法读取。使用第一个文本框中的新代码更新问题并使用代码按钮格式化。



调试器通过向您展示崩溃的位置,您可以缩小研究范围。检查变量以查看哪个变量具有意外值。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Your code is unreadable. Update the question with fresh code in first textbox and use the code button to format.

The debugger will allow you to narrow the research by showing you where is the crash. Check the variables to see what variable have unexpected value.


这篇关于为什么程序崩溃..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆