简体   繁体   English

这是GFG的问题。 我正在尝试分析和理解这段代码

[英]This is a question from GFG. I am trying to analyse and understand this code

This is a code I have taken from GeeksForGeeks.这是我从 GeeksForGeeks 获取的代码。 I am trying to understand the code.我试图理解代码。

In the line in the function deleteEle which says if(i==n) return n , how could i become n if it is running for < n times in the for loop just above it?在 function deleteEle的行中,它说if(i==n) return n ,如果它在它上面的for循环中运行< n次, i怎么会变成n呢?

    #include <iostream >
    #include <cmath>
    using namespace std;

    int deleteEle(int arr[], int n, int x)
    {
        int i = 0;

        for(i = 0; i < n; i++)
        {
            if(arr[i] == x)
                break;
        }

        cout << i << endl;
        if(i == n)
            cout<<i;
            return n;

        for(int j = i; j < n - 1; j++)
        {
            arr[j] = arr[j + 1];
        }

        return n-1;
    } 

    
    int main() {
    
           int arr[] = {3, 8, 12, 5, 6}, x = 13, n = 5;
 
           cout<<"Before Deletion"<<endl;

           for(int i=0; i < n; i++)
           {
            cout<<arr[I]<<" ";
           }

           cout<<endl;

     
           n = deleteEle(arr, n, x);

           cout<<"After Deletion"<<endl;

           for(int i=0; i < n; i++)
           {
                cout<<arr[I]<<" ";
           }
    
    }

how could I become n if it is running for < n times in the for loop just above it?如果它在它上面的 for 循环中运行< n次,我怎么能变成n呢?

Because it may happen that the if condition arr[i] == x inside the preceding for loop is never satisfied which in turn means that the break is never executed in which case the last iteration will increment i so that i becomes equal to n .因为可能会发生前面 for 循环中的 if 条件arr[i] == x永远不会满足,这反过来意味着break永远不会执行,在这种情况下,最后一次迭代将递增i以便 i 变得等于n

After the last iteration of this loop ( if arr[i],= x for all i in range [0, n] ):在这个循环的最后一次迭代之后( if arr[i],= x for all i in range [0, n] ):

    for(i = 0; i < n; i++)
    {
        if(arr[i] == x)
            break;
    }

i will equal n if break is not executed.如果不执行breaki将等于n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以下是 gfg 中子集和问题的代码。 请告诉我它有什么问题 - Below is the code for subset sum problem in gfg. Please tell me what is wrong in it 我正在尝试理解这两个代码块,有人可以帮忙吗? - I am trying to understand these 2 blocks of code, could someone help please? 我试图理解字符串声明的这一行 - I am trying to understand the line with string declaration 我正在尝试使用 C++ 语言使用 MPI 库来理解并行代码和顺序代码之间的区别 - I am trying to understand the difference between parallel and sequential code using MPI library with C++ language 我无法理解 UVA 10242 第四点的代码!! 尽管尝试了几个小时 - I am unable to understand code of UVA 10242 Fourth Point !! despite trying for hours 我正在尝试解决一个需要数组总和值但代码不起作用的问题我想做这样的事情 - I am trying to solve a question where i need array sum values but the code is not working I want to do something like this 我试图了解内存分配是如何工作的以及它何时发生 - I am trying to understand how memory allocation works and when it happens 我试图了解形式参数在 c++ 中的工作原理 - i am trying to understand how formal parameters work in c++ 试图了解ASM代码 - Trying to understand ASM code 我无法理解 output 到我的 c++ 代码 - I am unable to understand the output to my c++ code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM