Haste makes waste

Nano01(自動運転)-【C++】-Lesson06-Arrays数组

Posted on By lijun

0. 小结

本节也讲得非常简单,其实在C++中,与其用原生的array,不如用vector方便。

2. Find Min & Max Answer

/*Find the min and max and average of 15 integers that a user will input.
**We will do it now for practice and again when we learn arrays. So you do not have to **keep all fifteen numbers stored in memory.
*/

#include <iostream>

int main()
{
    int userInput = 0;
    int maxNumber = 0;
    int minNumber = 100;
    int sumTotal = 0;
    float average = 0; 

    //get the numbers from the user
    for(int i = 0; i < 15; i++)
    {
        std::cout << "Enter a number: ";
        std::cin>>userInput;
        std::cout << userInput << "\n";
        if(userInput > maxNumber)
        {
            maxNumber = userInput;
        }
        if(userInput < minNumber)
        {
            minNumber = userInput;
        }
        sumTotal = sumTotal + userInput;
    }
    std::cout << "Maximum number = " << maxNumber << "\n";
    std::cout << "Minimum number = " << minNumber << "\n";
    average = sumTotal / 15.0;
    std::cout << "Average = " << average << "\n";
    return 0;
}
Maximum number = 107
Minimum number = 14
Average = 63.5333

3. Arrays

There is a good case that instead of using arrays, you can use vectors. Vectors are more powerful /versatile than arrays.

 /*Goal: Practice array manipulation in C++. 
 **The user will input 40 integers. 
 **Put them into an array. Then print the array in the order the numbers were
 **entered. Then print in reverse order. Then sort the array in ascending order 
 **and print it. 
 */

 #include <iostream>
 #include <stdio.h>

 int main()
 {
     int userInput[40];

     //Enter the numbers into an array called userInput
     for(int i = 0; i < 40; i++)
     {
         scanf("%d", &userInput[i]);
     }
     //print the array
     std::cout<<"\nThe array\n";
     for(int i = 0; i < 40; i++)
     {
         std::cout << userInput[i] <<" ";
     }
     //print the array in reverse order
     std::cout<<"\n\nThe array in reverse order\n";
     for(int i = 39; i >= 0; i--)
     {
         std::cout << userInput[i] <<" ";
     }    
     //sorting the array
      for(int i = 0; i < 40; i++)
     {
         for(int j = 0; j < 39 - i; j++)
             {
                 if(userInput[j] > userInput[j + 1])
                 {
                     int temp;
                     temp=userInput[j];
                     userInput[j]=userInput[j + 1];
                     userInput[j + 1]=temp;
                 }
             }
     }
    std::cout<<"\n\nThe array sorted\n";
        for(int i = 0; i< 40; i++)
    {
        std::cout << userInput[i] <<" ";
    }
return 0;
}
The array
43 32 21 23 43 98 90 65 53 42 69 79 82 7 1 63 64 90 87 96 4 55 3 7 2 1 67 89 543 567 985 34 975 890 4567 23 33 9 56 345 

The array in reverse order
345 56 9 33 23 4567 890 975 34 985 567 543 89 67 1 2 7 3 55 4 96 87 90 64 63 1 7 82 79 69 42 53 65 90 98 43 23 21 32 43 

The array sorted
1 1 2 3 4 7 7 9 21 23 23 32 33 34 42 43 43 53 55 56 63 64 65 67 69 79 82 87 89 90 90 96 98 345 543 567 890 975 985 4567 

7. Array Searches

判断用户输入是否在指定的数字串中:

 #include <iostream>
 #include <stdio.h>

 int main()
 {
     int searchKey = 0;
     int searchArray[10] = {324,4567,6789,5421345,7,65,8965,12,342,485};
     int location = 0;

     while(1)
     {
         std::cout<<"Enter an integer ('-1' to quit): ";
         scanf("%d", &searchKey);
         std::cout<< searchKey<<"\n";
         if(searchKey == -1)
         {
             break;
         }
         for(int i = 0; i < 10; i++)
         {
             if(searchKey == searchArray[i])
             {
                 location = i;
                 break;
             }
             location = -1;
         }
         if(location != -1)
         {
             std::cout<<searchKey<<" is at location "<<location<<" in the array.\n";
         }
         else
         {
             std::cout<<searchKey<<" is not in the array.\n";
         }
    }
     return 0;
 }

8. MultiDimensional Arrays

/*Goal: understand multidimensional arrays in C++*/

#include<iostream>

int main()
{
    int array2Dim[2][3] = {0,1,2,3,4,5};
    for(int i=0; i<2;i++)
        for(int j=0;j<3;j++)
            std::cout<<"array2Dim["<<i<<"]["<<j<<"] = " << array2Dim[i][j]<<"\n";
    return 0;
}
array2Dim[0][0] = 0
array2Dim[0][1] = 1
array2Dim[0][2] = 2
array2Dim[1][0] = 3
array2Dim[1][1] = 4
array2Dim[1][2] = 5

9. Multidimensional Arrays Programming Quiz

从用户那里先接受16个数字,然后4个数字,前16个数字赋予4×4的array,后面4个数字赋予size为4的vector,然后将array和vector相乘,输出乘积结果。

/*Goal: practice using multidimensional arrays.
**Write a program that will accept values for a 4x4 array 
**and a vector of size 4. 
**Use the dot product to multiply the array by the vector. 
**Print the resulting vector. 
*/

#include<iostream>

int main()
{
    //array dimensions must be known at compile time
    //so I used const int to set the row and col numbers
    const int row = 4;
    const int col = 4;

    int arr[row][col];
    int vector[row], product[row];
    int sum;

    //get the values for the array from the user
    for(int i=0;i<row;i++)
        for(int j=0;j<row;j++)
        {
            //std::cout<<"arr["<<i<<"]["<<j<<"] = \n";
            std::cin>>arr[i][j];
            //std::cout<<"arr["<<i<<"]["<<j<<"] ="<<arr[i][j]<<"\n";
        }

    //getting the values for the vector from the user
    for(int i=0; i<row; i++)
    {
        //std::cout<<"vector["<<i<<"] = \n";
        std::cin>>vector[i];
        //std::cout<<"vector["<<i<<"] = "<<vector[i]<<"\n";
    }
    sum = 0;

    for(int i=0;i<row;i++)
    {
        for(int j=0; j<row;j++)
        {
            sum = (arr[i][j] * vector[i]) + sum;
        }
        product[i] = sum;
        sum=0;
    }

    for(int i=0;i<row;i++)
    {
        std::cout<<"product["<<i<<"] = "<<product[i]<<"\n";
    }
    return 0;
}

上面的矩阵乘法,参考下图:

image