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.

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

  1. Once again, not credit. This time, even that, who came up with the job. Where the term dripped “combined operators” – not by chance have devised? – in any book I have not seen.

    ourArr[i] = 7 + rand() % 8;
    This is bad, because you recently wrote an article about the correct execution of the code. 100% Agreements about coding that I have read prohibit the use of “magic constants”, your 7 and 8 they are just. If 7 It occurs in the condition, you can leave, but 8 it is necessary to rewrite.

    const int lower_limit = 7;
    const int upper_limit = 14;

    … % (const int upper_limit – const int lower_limit + 1)
    And comments in this piece we can add, tk. still it is not clear why +1.

    ourArr[i] -= 10; // take away 10
    And here you can comment (necessary) not write. In many agreements, comments generally are not encouraged coding, tk. outdated (you change the code, a comment will not forget?). Comments should not be more than, should not repeat the code and must explain not only the obvious points. It is a moot point for any javadoc / doxygen, but in your case it is obviously superfluous.

    In the third undertaking to replace “similar items ” on “the corresponding elements” (you can try to come up with a better word)

  2. in the problem 2 It does not say that the numbers should be random)) I have it so:

    int main(){
    int aMass[50], p=0;
    for(int i=1; i<100; i++) {
    if(i%2 != 0) {
    aMass[p] = i;
    p++;
    }
    }
    for(int i=0; i<50; i++) cout <<aMass[i] <<' ';
    cout <<endl;

    system("pause");
    return 0;
    }

  3. int massiv1[30]={};
    int massiv2[30]={};
    int massiv3[30]={};
    int max=1, min=0,sum=0,count=0;
    float sredarifm=0;

    srand(time(NULL));

    for (int i = 0; i 0)
    {
    if (massiv3[i]>max)
    {
    max=massiv3[i];
    }
    if (massiv3[i]<min)
    {
    min=massiv3[i];
    }
    }
    }
    sredarifm=static_cast(sum)/count;
    cout<<"Maksimalnoe znachenie = "<<max<<'\n';
    cout<<"Minimalnoe znachenie = "<<min<<'\n';
    cout<<"Sredne-arifmeticheskoe znachenie = "<<sredarifm;

  4. Lungs, but interesting puzzles, and your decisions eschi and very stylish. I do not know, What a dude dokapalis to you, I personally really liked. pasibo)

  5. task №1
    #include
    #include

    using namespace std;

    int main(){
    setlocale(LC_ALL, “Russian”);
    int const size = 10;
    int array[size];
    int random_value, first_value, last_value;
    cout << "Задайте начальное значение и ширину диапазона: " <> first_value >> last_value;
    for (int i = 0; i = 10){
    random_value -= 10;
    }
    array[i] = random_value;
    }
    for (int i = 0; i < size; i ) {
    cout << array[i] << " ";
    }
    system("pause");
    return 0;
    }

    1. not the entire source code Copy(

      #include
      #include
      #include

      using namespace std;

      int main(){
      setlocale(LC_ALL, "Russian");
      int const size = 10;
      int array[size];
      int random_value, first_value, last_value;
      cout << "Задайте начальное значение и ширину диапазона: " <> first_value >> last_value;
      for (int i = 0; i = 10){
      random_value -= 10;
      }
      array[i] = random_value;
      }
      for (int i = 0; i < size; i++) {
      cout << array[i] << " ";
      }
      system("pause");
      return 0;
      }

  6. task №3
    #include
    #include
    #include
    #include

    using namespace std;

    int main(){
    setlocale(LC_ALL, "Russian");
    srand(time(NULL));
    bool exit = true;
    bool exit1 = true;
    int const size = 15;
    int minNum = 100;
    int maxNum = 0;
    int sum = 0;
    int change;
    int average;
    int array1[size], array2[size], array3[size];
    int first_value, last_value;
    cout <> first_value >> last_value;
    if (last_value > first_value)
    exit = false;
    if (last_value < first_value)
    cout << "Введите првильно диапазон: ";
    } while (exit);
    for (int i = 0; i maxNum)
    maxNum = array3[i];
    if (array3[i] < minNum)
    minNum = array3[i];
    sum += array3[i];
    }
    do {
    cout << "\nХотите вывести массив на экран?(1-3)\nВыход(4)" <> change;
    switch (change) {
    case 1:cout << "Первый массив:" << endl;
    for (int i = 0; i < size; i++) {
    cout << array1[i] << " | ";
    }
    break;
    case 2:cout << "\nВторой массив:" << endl;
    for (int i = 0; i < size; i++) {
    cout << array2[i] << " | ";
    }
    break;
    case 3:cout << "\nТретий массив:" << endl;
    for (int i = 0; i < size; i++) {
    cout << array3[i] << " | ";
    }
    break;
    case 4:
    exit1 = false;
    break;
    default:
    cout << "Введите правильный номр массива(1-3)" << endl;
    break;
    }

    } while (exit1);
    average = sum / size;
    cout << "\nСреднее арефметическое: " << average << endl;
    cout << "Минимальное значение: " << minNum << endl;
    cout << "Максимальное значение: " << maxNum << endl;
    system("pause");
    return 0;
    }

  7. task №3.
    I got here so:
    #include
    #include
    using namespace std;
    int main()
    {
    setlocale(LC_ALL, “”);
    srand(time(NULL));
    int iArray1[10] = {}, iArray2[10] = {}, iArray3[10] = {};
    int iMaxArray3 = 0, iMinArray3 = 31, iSumma = 0;
    for (int j = 0; j < 10; j )
    {
    iArray1[j] = 10 + rand() % 21;
    cout << iArray1[j] << " ";
    }
    cout << endl << endl;
    for (int j = 0; j < 10; j )
    {
    iArray2[j] = 10 + rand() % 21;
    cout << iArray2[j] << " ";
    }
    cout << endl << endl;
    for (int i = 0; i < 10; i )
    {
    iArray3[i] = iArray1[i] +iArray2[i];
    cout << iArray3[i] << " ";
    }
    cout << endl << endl;
    for (int i = 0; i < 10; i )
    {
    iSumma + = iArray3[i];
    if (iArray3[i] iMaxArray3)
    iMaxArray3 = iArray3[i];
    }
    cout << " Arithmetic mean = " << iSumma / 10 << endl;
    cout << " Максимум = " << iMaxArray3 << endl;
    cout << " Минимум = " << iMinArray3 << endl;
    system("pause");
    return 0;
    }

Leave a Reply

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