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. Good site, articles are written clearly and. But! uncomfortably, lessons that go into the tangled sequence: I was in the classroom while loop, on arrow farther fall in string, link to see a lesson arrays directly in the article, in the next to random, and if you go on the arrow below to find ourselves again greeting (1 article). Suffice confusing path, my advice is to do the right sequence of articles on the themes when you click on the active shooters, that under Article.

      1. thanks for the answer. Yes, I saw on the main. Just accustomed lessons of the past => PUSH next :) And to have to go back to home.

  2. Hello. The thing is , it is not correctly considers not only the maximum, minimum and average, and the amount of each element of the 1st and 2nd array 3rd. What is the problem?)
    #include
    #include
    #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, "rus");

    int Ar1 [30] = {};
    int Ar2 [30] = {};
    int Ar3 [30] = {};
    const int lowerLimit = 10;
    const int upperLimit = 30;

    srand(time(NULL));
    cout << "Массив Ar1: ";
    for (int i = 0; i < 30; i++)
    {
    Ar1[i] = lowerLimit + rand() % (upperLimit - lowerLimit + 1);
    cout << Ar1[i] << " | ";
    }
    cout << endl << endl;

    cout << "Массив Ar2: ";
    for (int i = 0; i < 30; i++)
    {
    Ar2[i] = lowerLimit + rand() % (upperLimit - lowerLimit + 1);
    cout << Ar1[i] << " | ";
    }
    cout << endl << endl;

    cout << "Массив Ar3: ";
    for (int i = 0; i < 30; i++)
    {
    Ar3[i] = Ar1[i] + Ar2[i];
    cout << Ar3[i] << " | ";
    }
    cout << endl << endl;
    float sum = 0;
    int srednee = 0;
    int max = Ar3[0];
    int min = Ar3[0];
    for ( int i = 0; i < 30; i++)
    {
    sum += Ar3[i];
    if (Ar3[30] > max)
    {
    max = Ar3[i];
    }

    }
    srednee = sum / 30;
    cout << "Макс = " << max << endl;
    cout << "Мин = " << min << endl;
    cout << "Среднее = " << srednee << endl;

    system("pause");
    return 0;
    }

    1. 1) Regarding the amount of elements of the 1st and 2nd array and entries in the third array cells. Look closely at the string 29. In the loop you fill the second array, and displayed on the screen of the first data.
      2) Maximum. See row 47: Ar 3[30] &gt; max ? Replace the Ar3[i] &gt; max
      3) Search minimum in the code is not registered. Fill in and you will have a minimum :)

  3. In the third task, I tried all in one for what to do, considering that the author and will. What was surprising, that in their reply 4. apparently, that was done, to such newcomers, as we, it clearer.
    for (int i=0; i<max;i )
    {
    arr[i]=1 rand()%100;
    arr2[i]=1 rand()%100;
    arr3[i]=arr[i]+arr2[i];
    a =arr3[i];

    if (min<arr3[i])
    {}
    else {min=arr3[i];}
    cout<<arr3[i]<<"|";
    }
    b=a/max;
    cout<<" = Nsred"<<b<<endl;
    cout<<"min = "<<min<<endl;
    return 0;
    }
    I apologize for the obscure code, I wrote it right in your phone, online compiler.

    1. Do not worry, I just have some experience. Some initially difficult to understand, why is the variable needed. Generally, more need to practice, each statement individually assorted.

    1. #include
      #include
      #include
      using namespace std;

      int main()
      {
      setlocale(LC_ALL, "rus");
      int arr[10] = {};

      srand(time(NULL));

      for (int i = 0; i < 10; i++) { arr[i] = 7 + rand() % 8; cout << arr[i] << " || "; } cout << endl; }

      rand() % 8 will generate a number of 1 to 7

  4. There is an easy way to generate numbers to cope with (so-called pseudo-number)

    [code]#include//who's ctime
    using namespace std;
    void main()
    {
    setlocale(LC_ALL, “ru”);
    srand(time(NULL));

    for(int i=0; i<size; i )
    {
    array[i]=rand()%(x-y)+(Y) //x – the largest number given to us
    //Y – the least number of set us
    cout << " array[" << i << "] = " << array[i] << "\n"; // on screen display
    }

    //eg set number of 10 to 20
    for(int i=0; i<size; i )
    {
    array[i] = rand()%(20-10)+(10); //20 – the largest number given to us
    //10 – the least number of set us
    cout << " array[" << i << "] = " << array[i] << "\n"; // on screen display
    }
    [/code]

    1. Taking the remnants of the division (%) rand() in all its forms and manifestations – it is a bad practice! In this case violated uniform distribution law rand(). In the simplest cases, this goes unpunished, but, for example, in the simulation – it gives a completely distorted results.

  5. Regarding the second objective,, like here so much easier to solve.

    1. got it, just in the subject should be written : fill … randomly from the odd numbers 1 to 99.

  6. And why in the code of the first task is the condition of greater than or equal, if said condition, it is necessary to overwrite those of the array elements, in which the numbers strictly greater than ten?

Leave a Reply

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