繁体   English   中英

数组旋转与获取数组的最大值和索引位置 C 语言

[英]Array Rotation with Getting Max Value and Index Location of Array C language

我正在创建一个程序来获取数组中最高元素的索引值。

样本输入:

4 (Size of a[])
1 2 4 3 (Elements of a[])
2 (Size of rotate[])
0 2 (Elemnts of rotate[])

Output将是:

2
0

使用左旋转。
在第一次旋转 (0) 中,位置将为 2,因为 4 是最高的 a[1,2,4,3]
在第二次旋转 (2) 中,位置将为 0,因为 4 是最高的 a[4,3,1,2]

问题是我没有得到想要的 output 并且有一个警告for(j=0;j<rotateValue;j++)

我希望 function 保持原样并将这部分修复为int* output = getMaxIndex(a,rotate); 但我不知道怎么做。 提前感谢您的帮助!

#include<stdio.h>

int i,j,k; // for looping
int n, m; // sizes of arrays


int getMaxIndex(int* a[], int* rotate[])
{
    int indices[m]; 

                
    for(i=0;i<m;i++)
    {
        int* rotateValue = rotate[i];                           
        for(j=0;j<rotateValue;j++)      // for rotation
        {               
            int* first = a[0];  
            for(i=0;i<n-1;i++)
            {       
                a[i] = a[i+1];
            }
            a[n-1] = first;
        }
                
        int location;
        int* max = a[0];
        for(j=0;j<n;j++) // getting the max element 
        {
            if(a[j] > max)
            {
                max = a[j];
//              printf("Max added");
            }
        }
        
        for(j=0;j<n;j++) // getting the location
        {
            if(max == a[j])
            {
                location = j;
//              printf("Loc added");
            }
        }
        
        
        indices[i] = location;      
    

    }
    
//  for(i=0;i<m;i++) // printing here to know if correct
//  {
//      printf("%d",indices[i]);
//  }
    
    
    
    return *indices;
            
        
    

}

int main()
{
    
    
    scanf("%d",&n); // inputting array size 
    int* a[n];  
    
    for(i=0;i<n;i++) // filling elements of a[]
    {
        scanf("%d",&a[i]);
    }
    
    scanf("%d",&m); // inputting rotate array size
    int* rotate[m];
    
    for(i=0;i<m;i++) // filling elements of rotate[]
    {
        scanf("%d",&rotate[i]);
    }
    

    int* output = getMaxIndex(a,rotate); // call function
    
    for(i=0;i<m;i++) // printing output
    {
        printf("%d",output[i]);
    }
    
}


int getMaxIndex(int* a[], int* rotate[]);

按以下方式设计getMaxIndex()应该可以解决大部分问题:

int* getMaxIndex(int a[], int rotate[])
{
    static int indices[MAX_POSSIBLE_VALUE_OF_M];
    /*
        your code
    */
    return indices;
}

现在,您所要做的就是相应地调整main() function 中的代码。


为什么将getMaxIndex()中的数组indices[]声明为 static int?

indices[]getMaxIndex()的局部变量。 所以在getMaxIndex()的return语句执行后,它应该被销毁。 这意味着,如果您将indices[]返回给main() ,主 function 将无法再访问indices[] 这个问题可以通过将indices[]声明为static int而不是int来解决。

注意: static 阵列应该具有恒定大小。 因此,它的大小应该声明为 m 的最大可能值而不是 m。

main()中所需的调整:

a[]rotate[]声明为int而不是int*

查看我的代码。我得到了正确的 output。 我已经写下了你犯的一些错误。

void getMaxIndex(); //function declaration
int n, m; //for storing array size
int * a, * rotate;
int main(void) {

    int i; //to use in loops

    scanf("%d", & n); // inputting array size
    a = (int * ) malloc(n * sizeof(int));

    for (i = 0; i < n; i++) // filling elements of a[]
    {
        scanf("%d", & a[i]);
    }

    scanf("%d", & m); // inputting rotate array size
    rotate = (int * ) malloc(m * sizeof(int));

    for (i = 0; i < m; i++) // filling elements of rotate[]
    {
        scanf("%d", & rotate[i]);
    }

    getMaxIndex();

    free(a);
    free(rotate);
    return 0;
}

void getMaxIndex() {
    int i;
    int aMax, rotateMax;
    int aMaxIndex, rotateMaxIndex;
    aMax = a[0];
    rotateMax = rotate[0];

    for (i = 1; i < n; i++) {
        if (aMax < a[i]) {
            aMax = a[i];
            aMaxIndex = i;
        }
    }
    for (i = 1; i < m; i++) {
        if (rotateMax < rotate[i]) {
            rotateMax = rotate[i];
            rotateMaxIndex = i;
        }
    }
    printf("%d\n%d", aMaxIndex, rotateMaxIndex);
}

我的建议:

  1. 始终尝试为您的阵列动态分配 memory,以避免出现分段错误或核心转储等错误。
  2. 在您的代码中,您使用了指针数组而不是指针,您出错了。 尝试参考您的教科书或其他资源,以获得关于指针的清晰概念。 例如,在您的代码中,您使用以下行传递了名为 indices 的数组:return indices; 现在,要传递一个指针,您不需要使用星号( )。 简单地写:返回索引;
  3. 另外,不要使用星号来声明数组。 您的代码: int* a[n]; 在这里,您声明的是指针数组而不是数组。 正确代码:int a[n];

但我喜欢你的逻辑。 您只需要使用正确的语法来实现它。 只要继续练习。 我写的代码被你理解了,我的工作就完成了。 快乐编码!!!

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM