从数组接受元素并打印最大奇数的C ++代码 [英] C++ code that accept element from array and print the max odd number

查看:95
本文介绍了从数组接受元素并打印最大奇数的C ++代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i write c++ code that  accept element from array and print the max odd number location 





我尝试过:





What I have tried:

#include <iostream>

using namespace std;
int main()
{
int a[5];
int i;
int c[5];
int j;
cout<<"enter the numbers"<<endl;
for(i=0;i<=5;i++)
{
cin>>a[i];
}
cout<<"\nThe result is:\n";
for(i=0;i<=5;i++)
{
if(a[i]%2!=0)
c[i]=a[i];
}
//}
for(int i=0;i<=5;i++)
for(int j=i+1;j<5;j++)
{
if(c[i]>c[j])
{
int temp;
temp=c[i];
c[i]=c[j];
c[j]=temp;

}

}
//for(int i=0;i<5;i++)
    cout<<c[i+1]<<endl;
}

推荐答案

你有什么问题?



您是否收到编译器错误,运行时错误,或者它是否按预期工作?



但是,您的阵列存在一些问题。我建议您再次阅读有关数组的课程资料或教程。



您的阵列固定大小为5项。因此,您可以访问索引为0到4的项目。但是您的循环正在运行索引5存储和读取6个元素。这可能会在执行时导致访问冲突或导致至少错误的结果。



您还初始化 c 具有奇数输入值的数组但是使偶数项未初始化。你应该用零初始化 c 数组的所有项目。



最后你打算 c [i + 1] 之后已被用作循环计数器。想想循环结束后 i 的值是多少。
What is your question?

Did you get compiler errors, runtime errors, or is it not working as expected?

However, there are some problems with your arrays. I suggest to read again your course material or tutorial about arrays.

You have arrays with the fixed size of 5 items. So you can access the items with the indexes 0 to 4. But your loops are running up to index 5 storing and reading 6 elements. This might result in an access violation while executing or lead at least to wrong results.

You are also initialising the c array with the odd input values but let the even items uninitialised. You should initialise all items of the c array with zero.

Finally you are priniting c[i+1] after i has been used as loop counter. Think about what value is held by i after the loop has finished.


你没有问过任何问题。



除了解决方案4中的问题之外,在给出答案之前,您从未检查列表中是否至少有一个奇数。

使用未初始化的变量或数组也是一种不好的做法。



只是一个建议:学会正确缩进你的代码,这有助于阅读。

You didn't asked any question.

Apart from problems as in Solution 4, you never checked if there is at least an odd number in the list before giving answer.
Using uninitialized variables or arrays is also a bad practice.

Just an advice: learn to indent your code properly, it helps reading.
#include <iostream>

using namespace std;
int main()
{
    int a[5];
    int i;
    int c[5];
    int j;
    cout<<"enter the numbers"<<endl;
    for(i=0;i<=5;i++)
    {
        cin>>a[i];
    }
    cout<<"\nThe result is:\n";
    for(i=0;i<=5;i++)
    {
        if(a[i]%2!=0)
            c[i]=a[i];
    }
    //}
    for(int i=0;i<=5;i++)
        for(int j=i+1;j<5;j++)
        {
            if(c[i]>c[j])
            {
                int temp;
                temp=c[i];
                c[i]=c[j];
                c[j]=temp;
            }
        }
    //for(int i=0;i<5;i++)
    cout<<c[i+1]<<endl;
}


hi
包括< iostream>



使用namespace std;

int main()

{

int a [5];

int i;

int c [5] = {0};



cout<<输入数字<< endl;

for(i = 0; i< = 5; i ++)

{

cin>> a [i];

}

cout<<\ n结果是:\ n;

int j = 0;

for (i = 0; i< = 5; i ++)

{

if(a [i]%2!= 0)

c [ j] = a [i];

j ++;

}

//}

int g = c [0];

int p = 0;

for(int i = 0; i< j; i ++)

if(g< c [i])

{

g = c [i];

p = i;

}

}

cout<< p<< endl;

}
hi include <iostream>

using namespace std;
int main()
{
int a[5];
int i;
int c[5]={0};

cout<<"enter the numbers"<<endl;
for(i=0;i<=5;i++)
{
cin>>a[i];
}
cout<<"\nThe result is:\n";
int j=0;
for(i=0;i<=5;i++)
{
if(a[i]%2!=0)
c[j]=a[i];
j++;
}
//}
int g=c[0];
int p=0;
for(int i=0;i<j;i++)
if(g<c[i])
{
g=c[i];
p=i;
}
}
cout<<p<<endl;
}


这篇关于从数组接受元素并打印最大奇数的C ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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