Enter 10 numbers, the smallest of which is swapped with the first, the largest number and the last

Source: Internet
Author: User

Enter 10 integers, swap the smallest number with the first number, and swap the largest number with the last.

Write three functions: 1, enter 10 integers

2, to deal with

3, output 10 number


#include "stdafx.h"
#include <iostream>
using namespace Std;


int array[10],*p;

First function, enter a number
void Shuru (int *p)
{
cout<< "Please enter 10 integers:" <<endl;
for (int i=0;i<10;i++)
{
cin>>* (P+i); Enter 10 numbers, enter each number, and place it in the address (p+i)
}
}

The second function, which is processed
void Chuli (int *p)
{
int *max,*min,*b;
int t;
Min=p; To point the pointer min to the same position as the pointer p
Max=p; Point pointer Max to the same position as pointer p
B=p; Point Pointer b to the same position as pointer p
for (int j=1;j<10;j++)
{
if (*min>* (B+J))//The number that the pointer points to is compared here
{
Min=b+j; The change here is that the pointer is pointing
}
}
T=*min;
*min=*p;
*p=t; Swap two pointers to the position of the number, so that the minimum number is exchanged with the first number of positions


d=6; The last operation changed the direction of the B pointer, so this resets the pointer
for (int l=1;l<10;l++)
{
if (*max<* (b+l))
{
Max=b+l;
}
}
T=*max;
*max=* (p+9); Make the smallest number and the last number swap positions
* (p+9) =t;
}

Third function, output reorder number
void Shuchu (int *p)
{
for (int k=0;k<10;k++)
{
cout<<* (p+k) << "";
}
}


int _tmain (int argc, _tchar* argv[])
{
P=array; This causes P to point to the array of arrays, at which point P points to a[0],p+1 pointing to a[1] ...
Shuru (P);
Chuli (P);
Shuchu (P);

return 0;
}


If a pointer is defined, then p in the program indicates that the pointer is pointing, and *p represents the number stored in the address pointed to by the pointer.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Enter 10 numbers, the smallest of which is swapped with the first, the largest number and the last

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.