数组循环取最大值和数组索引位置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[])
输出 将是:
2
0
使用左旋转。
在第一次旋转 (0) 中,位置将是 2,因为 4 是最高的 a[1,2,4,3]
在第二次旋转 (2) 中,位置将为 0,因为 4 是最高的 a[4,3,1,2]
问题是我没有得到所需的输出,并且 for(j=0;j<rotateValue;j++)
中出现警告
我希望功能保持原样并将这部分修复为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()
函数中的代码。
为什么将getMaxIndex()
中的数组indices[]
声明为static int?
indices[]
是getMaxIndex()
的局部变量。所以getMaxIndex()
的return语句执行完后,就会被销毁。这意味着,如果你 return indices[]
到 main()
,主函数将无法再访问 indices[]
。这个问题可以通过将 indices[]
声明为 static int
而不是 int
.
来解决
注意: 静态数组应具有恒定大小。因此,它的大小应该声明为 m 的最大可能值而不是 m。
main()
中的必要调整:
将 a[]
和 rotate[]
声明为 int
而不是 int*
。
看看我的 code.I 是否得到了正确的输出。我已经记下了你犯的一些错误。
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);
}
我的建议:
- 始终尝试为您的阵列动态分配内存,以避免诸如分段错误或核心转储之类的错误。
- 在你的代码中你使用了指针数组而不是指针,你错了。尝试参考您的教科书或其他来源以获得关于指针的清晰概念。
例如,在您的代码中,您使用以下行传递了名为 indices 的数组:
return 指数;
现在,要传递指针,您不需要使用星号 ()。简单地写:return indices;
- 此外,不要使用星号来声明数组。
你的代码:
整数*一个[n];
在这里,您声明的是指针数组而不是数组。
正确代码:
int [n];
但我喜欢你的逻辑。您只需要使用正确的语法来实现它。继续练习。
如果您理解我编写的代码,我的工作就完成了。快乐编码!!!
我正在创建一个程序来获取数组中最高元素的索引值。
示例输入:
4 (Size of a[])
1 2 4 3 (Elements of a[])
2 (Size of rotate[])
0 2 (Elemnts of rotate[])
输出 将是:
2
0
使用左旋转。
在第一次旋转 (0) 中,位置将是 2,因为 4 是最高的 a[1,2,4,3]
在第二次旋转 (2) 中,位置将为 0,因为 4 是最高的 a[4,3,1,2]
问题是我没有得到所需的输出,并且 for(j=0;j<rotateValue;j++)
我希望功能保持原样并将这部分修复为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()
函数中的代码。
为什么将getMaxIndex()
中的数组indices[]
声明为static int?
indices[]
是getMaxIndex()
的局部变量。所以getMaxIndex()
的return语句执行完后,就会被销毁。这意味着,如果你 return indices[]
到 main()
,主函数将无法再访问 indices[]
。这个问题可以通过将 indices[]
声明为 static int
而不是 int
.
注意: 静态数组应具有恒定大小。因此,它的大小应该声明为 m 的最大可能值而不是 m。
main()
中的必要调整:
将 a[]
和 rotate[]
声明为 int
而不是 int*
。
看看我的 code.I 是否得到了正确的输出。我已经记下了你犯的一些错误。
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);
}
我的建议:
- 始终尝试为您的阵列动态分配内存,以避免诸如分段错误或核心转储之类的错误。
- 在你的代码中你使用了指针数组而不是指针,你错了。尝试参考您的教科书或其他来源以获得关于指针的清晰概念。 例如,在您的代码中,您使用以下行传递了名为 indices 的数组: return 指数; 现在,要传递指针,您不需要使用星号 ()。简单地写:return indices;
- 此外,不要使用星号来声明数组。 你的代码: 整数*一个[n]; 在这里,您声明的是指针数组而不是数组。 正确代码: int [n];
但我喜欢你的逻辑。您只需要使用正确的语法来实现它。继续练习。 如果您理解我编写的代码,我的工作就完成了。快乐编码!!!