简体   繁体   English

C++ 程序将用户的十个数字放入一个数组并使用指针按顺序对数组进行排序

[英]C++ program than takes ten numbers from user into an array and sorts the array in order, using pointers

Write a program that asks user to input 10 positive integer numbers, stores them in an array and then replaces the first element with the minimum number and the last element with the maximum number.编写一个程序,要求用户输入 10 个正数 integer 数字,将它们存储在一个数组中,然后用最小数字替换第一个元素,用最大数字替换最后一个元素。

You must write 3 functions:您必须编写 3 个函数:

  1. void getNumber(int *): inputs 10 positive integer numbers and stores them in an array. void getNumber(int *):输入 10 个正 integer 数字并将它们存储在一个数组中。

  2. void minMax(int *): finds the minimum and maximum numbers, and re-arranges the array. void minMax(int *):找到最小和最大数,并重新排列数组。

  3. printNumber(int *): prints the resulted new array. printNumber(int *):打印生成的新数组。

Notes:笔记:
You MUST use POINTERS to access the array in all functions (failure to use pointer will result in 50% deduction).您必须使用 POINTERS 访问所有函数中的数组(未使用指针将导致 50% 的扣除)。

It works, but I need the user input portion that's in main now, to be its own function too.它可以工作,但我需要现在main的用户输入部分,也是它自己的 function。 I don't know how to do that!我不知道该怎么做!

#include <iostream>
using namespace std;

// Function to sort the numbers using pointers 
void sort(int n, int* ptr)
{
    int i, j, t;

    // Sort the numbers using pointers 
    for (i = 0; i < n; i++) {
        for (j = i + 1; j < n; j++) {
            if (*(ptr + j) < *(ptr + i)) {
                t = *(ptr + i);
                *(ptr + i) = *(ptr + j);
                *(ptr + j) = t;
            }
        }
    }
}

int print(int n, int *ptr) 
{
    for (int i = 0; i < n; i++)
        cout << *(ptr + i) << " " ;
    cout << endl;
    return *ptr + 1;
}

int main()
{
    //int n = 10;
    //int arr[] = { 0, 4, 74, 88, 12, 37, 12, 7, 65, 2 };

    const int SIZE = 10;
    int values[SIZE];
    int i;
    //user input
    cout << "Enter 10 numbers: ";
    for (i = 0; i < 10; ++i)
        cin >> values[i];
    //prints forward array
    cout << "You entered: ";
    for (i = 0; i < 10; ++i)
        cout << values[i] << " ";
    cout << endl;

    sort(i, values);
    cout << "The numbers in order are: ";
    print(i, values);

    system("pause");
    return 0;
}

Here is the code that factor the user input into a seperate function:这是将用户输入分解为单独的 function 的代码:

void FillInArrayFromInput(int* arr, int size) // take the pointer to first element of the array and the size
{
    cout << "Enter 10 numbers: ";
    for (int i = 0; i < size; ++i)
        cin >> arr[i]; // Edit: there was little typo here!
}

Now the main function will be:现在主要的 function 将是:

int main()
{
    const int SIZE = 10;
    int values[SIZE];

    //user input
    FillInArrayFromInput(values, SIZE);

    //prints forward array
    cout << "You entered: ";
    for (i = 0; i < 10; ++i)
        cout << values[i] << " ";
    cout << endl;

    sort(i, values);
    cout << "The numbers in order are: ";
    print(i, values);

    system("pause");
    return 0;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM