«

89~90、 3道数组习题

点亮灯 发布于 阅读:88 C++


1,定义一个有5个元素的整型数组,初始化为(33,55,67,23,11),将下标为0,2,4的三个元素打印输出。

//定义一个有5个元素的整型数组,初始化为(33,55,67,23,11),将下标为0,2,4的三个元素打印输出。
#include<iostream>
using namespace std;
int main() {
    int a[5]{ 33,55,67,23,11 };
    cout<< a[0] << endl
        << a[2] << endl
        << a[4] << endl;
    system("pause");//卡屏函数
    return 0;
}
//定义一个有5个元素的整型数组,初始化为(33,55,67,23,11),将下标为0,2,4的三个元素打印输出。
#include<iostream>
using namespace std;
int main() {
int shu[5]={33,55,67,23,11};
cout << shu[0]<<"\t"<<shu[2]<<"\t"<<shu[4];
    return 0;
}

2,定义两个包含有4个元素的的整型数组,对其中一个数组进行赋值使其每个元素的值等于该元素的下标;再将这个数组的每一个元素的值乘以2后,赋值给另一个数组同下标的元素,并打印输出另一个数组各个元素的值。

//定义两个包含有4个元素的的整型数组,对其中一个数组进行赋值使其每个元素的值等于该元素的下标;
// 再将这个数组的每一个元素的值乘以2后,赋值给另一个数组同下标的元素,并打印输出另一个数组各个元素的值。
#include<iostream>
using namespace std;
int main() {
    int a[5] = { 0,1,2,3,4 };
    int b[5] = {0};
    for (int i = 0; i < 5; i++) {
        b[i] = a[i] * 2;
        cout << b[i] << endl;
    }

    system("pause");//卡屏函数
    return 0;
}

3,定义一个包含4个元素的数组,对其进行赋值使其每个元素的值等于其下标+10;然后打印输出所有下标是偶数的元素的值;最后再打印输出所有能够被3整除的下标的元素。

//定义一个包含4个元素的数组,对其进行赋值使其每个元素的值等于其下标+10;
// 然后打印输出所有下标是偶数的元素的值;最后再打印输出所有能够被3整除的下标的元素。
#include<iostream>
using namespace std;
int main() {
    int a[4]{ 0,1,2,3 };
    for (int i = 0; i < 4; i++) {
        a[i]=a[i] +10;
        if (a[i] % 2 == 0) {
            cout <<a[i] <<"\t";
        }
    }
    for (int i = 0; i < 4; i++){
        if(a[i]%3==0){
                cout <<endl<<a[i] << endl;
            }
    }
    system("pause");//卡屏函数
    return 0;
}