The basics of programming in c++ for beginners

Tasks: One-dimensional arrays C ++

Getting to the solution of tasks, read articles Arrays in C++  and The random number generator rand(), if you have not read them.   Try to address the proposed tasks independently and watch our decisions only in extremis.

1. Create an array of type int on 10 elements and fill it with random numbers from 7 to 14. After filling overwrite all numbers, who for more than ten:  from the stored value to take away 10.  For example the number is stored in the cell 12: 12 – 10 = 2. Write to this cell 2 Writing the new value, use composite(combined) statements.

2. To fill an array of elements 50 odd numbers from 1 to 99. (use the operation remainder of the division, to check on the number of parity)

3. Declare three arrays. The first two to fill with random values ​​from 10 to 30. In the third elements of the array to record the amount of the corresponding elements of the first two sets. (in the third cell zero – the sum of zero cells first and second arrays, and so on). Then, find the arithmetic mean of the elements of the third array, the maximum value and minimum value, which it holds.

110 thoughts on “Tasks: One-dimensional arrays C ++

  1. help me please!
    (Robot with one-dimensional arrays). How to swap each 1 with every 2 provided that the number of elements can be both even and odd.

  2. #include
    #include
    using namespace std;

    //setlocale(LC_ALL, “ru”);

    int main()
    {
    setlocale(LC_ALL, “rus”);

    int const SIZE = 10;
    int arr[SIZE];
    cout << "Вывод каждого элемента массива" << endl;
    for (int i = 0; i < SIZE; i )
    {
    arr[i] = rand() % 7+14;
    cout << arr[i] << endl;

    }
    cout << "Каждый элемент -10" << endl;

    for (int i = 0; i < SIZE; i )
    {
    cout << arr[i] – 10 << endl;
    }

    return 0;
    }

  3. Incorrect job conditions. For example, the task 2:
    2. To fill an array of elements 50 odd numbers from 1 to 99. (use the modulo operation, to check on the number of parity)
    The condition does not say, that the array should be filled with random numbers, not consistent, how did i do:

    #include

    using namespace std;

    int main()
    {
    int Array[50];
    int j = 0;
    for (int k = 1; k < 100; k ) {
    if (k % 2) {
    Array[j] = k;
    j ;
    };
    }
    //Code verification
    for (int n = 0; n < 50; n++) cout << Array[n] << '\n';
    return 0;
    }

  4. 1.
    #include
    #include

    using namespace std;

    int main()
    {
    srand(time(NULL));
    setlocale(LC_ALL, “RU”);
    const int ROW = 10;
    int arr[ROW];

    for (int i = 0; i = 10)
    {
    arr[i] -=10;
    }
    }
    for (int i = 0; i < ROW; i )
    {
    cout << arr[i] << "\t";
    }
    }

  5. To fill an array of elements 50 odd numbers from 1 to 99.
    (use the modulo operation, to check on the number of parity)

    #include

    using namespace std;

    int main()
    {
    setlocale(LC_ALL, “RU”);
    const int ROW = 50;
    int arr[ROW];
    int number = 1;

    for (int i = 0; i < ROW;)
    {
    number = rand() % 98 + 1;

    if (number % 2 != 0) {
    arr[i] = number;
    i ;
    }
    }
    for (int i = 0; i < ROW; i )
    {
    cout << arr[i] << " ";
    }
    }

  6. Declare three arrays.
    The first two to fill with random values ​​from 10 to 30.
    In the third elements of the array to record the amount of the corresponding elements of the first two sets.
    (into the zero cell of the third - the sum of the zero cells of the first and second arrays, and so on).
    Then, find the arithmetic mean of the elements of the third array, the maximum value and minimum value,
    which it holds.

    #include

    using namespace std;

    int main()
    {
    setlocale(LC_ALL, “RU”);
    const int ROW = 5;
    int arrOne[ROW];
    int arrTwo[ROW];
    int arrThree[ROW];
    int sumArr = 0;
    for (int i = 0; i < ROW; i )
    {
    arrOne[i] = rand() % 20 + 10;
    arrTwo[i] = rand() % 20 + 10;
    }
    for (int i = 0; i < ROW; i )
    {
    arrThree[i] = arrOne[i] + arrTwo[i];
    sumArr + = arrThree[i];
    }
    cout << "Среднее арифметическое третьего массива : " << sumArr / ROW << endl;
    int maxCount = arrThree[0];
    int minCount = arrThree[0];
    for (int i = 0; i < ROW; i )
    {
    if (maxCount arrThree[i])
    {
    minCount = arrThree[i];
    }
    }
    cout << "Максимальное число в третьем массиве : " << maxCount << endl
    <<"Минимальное число в третьем массиве : " << minCount << endl;
    }

  7. help me please.
    Write a program,which displays a one-dimensional array,consisting of 15 elements,as well as the sum and products of all elements of this array.
    how?!

  8. help me please.
    Given a one-dimensional array of dimension 20, change the order of elements to
    reverse.
    2. In this matrix, multiply the elements of the first and second rows.
    how?

Leave a Reply

Your email address will not be published. Required fields are marked *