c语言指针数组反序存放,用指针实现逆序存放数组元素

/*

* Copyright (c) 2012, 烟台大学计算机学院

* All rights reserved.

* 作 者:解晓东

* 完成日期:2012 年 12 月 6 日

* 版 本 号:v1.0

*

* 输入描述:

* 问题描述:用指针实现逆序存放数组元素

* 程序输出:

* 问题分析:

* 算法设计:

*/

#include using namespace std;

int inverte(int *x, int n); /*函数声明*/

int main()

{

int i, a[10]=

{

1, 2, 3, 4, 5, 6, 7, 8, 9, 0

}; /*定义数组*/

puts("The elements of original array:\n");

for (i=0; i<10; ++i) /*输出数组*/

cout << a[i] << " ";

cout << endl;

inverte(a, 10); /*使数组元素逆序*/

puts("The elements has been inverted:\n");

for (i=0; i<10; ++i) /*输出逆序后的数组*/

cout << a[i] << " ";

cout << endl;

return 0;

}

int inverte(int *x, int n)

{

int *p, temp, *i, *j, m = (n-1)/2;/*声明变量*/

i = x;/*变量i存放数组首地址*/

j = x + n - 1;/*变量j存放数组末尾元素地址*/

p = x + m;/*变量p存放数组中间元素地址*/

for (; i<=p; ++i, --j)/*交换数组前半部分和后半部分元素*/

{

temp = *i;

*i = *j;

*j = temp;

}

return 0;

}

/*

*在VC++6.0中运行的结果是:

*-----------------------------

*The elements of original array:

*

*1 2 3 4 5 6 7 8 9 0

*The elements has been inverted:

*

*0 9 8 7 6 5 4 3 2 1

*Press any key to continue

*-----------------------------

*/

C语言数组逆序操作:利用指针高效实现 指针是一种变量,其值为另一个变量的地址。指针的初始化需要指定它将指向哪种数据类型的内存地址。例如:// pNumber 指向 number 的地址这里的pNumber是一个整型指针,&number是取number变量的地址。一维数组是最基本的数据结构之一,它是一种线性表结构,可以存储同类型的数据元素。在内存中,一维数组按照连续的内存空间顺序存储,每个数组元素占据相同的内存大小。数组中的每个元素都可以通过下标访问,而下标通常从0开始。 阅读详情

相关推荐

【理解机器学习算法】之岭回归Ridge - L2 Rgularization

本人就职海外从事人工智能领域开发设计。 希望花时间总结下流行的机器学习算法,希望大家一起学习,欢迎指正!

Hyman Qiu 2323

C语言指针—实现数组逆序的二种方法

1. 设立中间量进行交换,这种方法需要使用一个中间变量,逐个交换数组的前半部分和后半部分,直到交换完整个数组。 2. 指针进行交换。这种方法只需要两个指针,一个指向数组的第一个元素,一个指向数组的最后一个元素,然后逐个交换它们指向的元素,直到交换完整个数组。

Half-up的博客 6143

从防御者视角复盘:用WinArpAttacker和Wireshark在Windows上模拟并检测ARP欺骗攻击

本文详细解析了Windows平台下的ARP欺骗攻击与防御策略,通过WinArpAttacker和Wireshark工具模拟攻击并检测异常流量。文章涵盖实验环境搭建、攻击原理深度解析、流量特征分析及企业级防御方案,帮助安全运维人员全面掌握ARP攻击检测与防护技术。

weixin_30918633的博客 124

C语言指针实现反向输出

*题目:通过指针传递实现将一数组中的数据按相反顺序存放。输入输出格式参见样张。

MUZHU55的博客 2143

什么是plm系统?-数字化转型网

当今世界,产品研发能力是企业在激烈的市场竞争中保持战略优势的核心竞争力之一。如何有效地缩短产品研发周期、改善产品服务的设计迭代流程、有效降低产品的研发成本,也是企业面临的主要问题之一。PLM(Product Lifecycle Management,产品生命周期管理)在优化产品研发的全管理流程、增强企业产品的市场竞争力方面具有关键的作用。

cxo17757154043的博客 889

Python实现支付宝转账接口

由于工作需要使用python开发一个自动转账接口,记录一下开发过程。 首先需要在蚂蚁金服上申请开通开发者账户,有了开发者账户就可以使用沙箱进行开发了。 在开发之前我们需要在沙箱应用中填写密钥,密钥的获取可以使用阿里提供的工具包自动生成。 前期准备工作完成了,接下来是编写代码部分。主要用到了python-alipay-sdk库,使用pip安装即可,如果安装的过程中遇到问题推荐使用Anaconda...

FanMLei的博客 8519

C语言 用指针 逆序存放数组

数组逆序存放。 数组元素为奇数,就只有最中间的元素不用替换。 数组元素为偶数,从头到尾全替换。 用指针时,拿出(a,b,c)三个指针变量,a指针指向数组第一个元素,b指针指向数组最后一个元素,c指针就指向数组最中间的元素。 用循环把指针a和指针b替换,循环到中间元素序号时就结束。 #include<stdio.h> #define N 6 //宏定义N 为6,下面的 N 都是6 的替身。 int main() {void inv(int *x,int n); int a[N]; .

Lulu_qifei的博客 7479

15、分布式参数热交换器:建模、动态特性与控制

本文综述了分布式参数热交换器的建模、动态特性和控制方法。从热交换器的基本方程推导出发,探讨了一维流动、流动强迫、错流、螺旋和旋转再生式热交换器的建模方法,并分析了其动态行为和频率响应中的共振现象。同时,介绍了热交换器的模拟方法,包括数值模拟和近似方法,并讨论了多种控制策略,如常规控制、最优控制、数字控制以及自适应控制。最后,总结了热交换器研究的发展历程,并展望了未来的研究方向,包括建模精度提升、控制算法优化和跨学科融合等。

w8x9y0z1的博客 128

c 语言数组指针之反向排序

void method_three(){ int arr_first[10] = {1,2,3,4,5,6,7,8,9,10}; // defination number array int *start; // defination array start index int *end; //defination array end index int *temp; //defination temporary .

紫梦涵的博客 1015

TIA博途中S7-1200进行MODBUS TCP通信(作为服务器)的具体方法演示

TIA博途中S7-1200进行MODBUS TCP通信(作为服务器)的具体方法演示

Robot_PLC_HMI_Automation 8647

Linux内存从0到1学习笔记(10.1 BugsShooter之cma配置过小导致页迁移繁忙而内存申请失败)

通过向武神讨教,顺便记录cma内存有空间但页繁忙(页正被访问,无法迁移)迁移不了导致cma内存分配失败的排查过程。

从0到1,突破自己 1万+

c语言200例 051 使用指针实现逆序存放数组元素

c语言200例 051 使用指针实现数组逆序输出

无限大的博客 4621

【GeoDa实用技巧100例】024:geoda计算全局(局部)莫兰指数Moran‘s I,LISA聚类地图,显著性地图

【GeoDa实用技巧100例】024:geoda计算全局(局部)莫兰指数Moran's I,单变量莫兰指数,双变量莫兰指数计算,LISA聚类地图,显著性地图,Geoda双变量莫兰指数是一种强大的地理空间数据分析工具。

智慧空间实验室 6061

C语言 写一个函数,将一维数组中的元素逆序存放

// // main.c // c_test2 // // Created by vincent_guo on 13-3-26. // Copyright (c) 2013年 vincent_guo. All rights reserved. // #include #include //写一个函数,将一维数组中的元素逆序存放(用指针做这题) //逆序存放第一种写法 void re

wfung_kwok的专栏 4万+

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

(c语言)用指针将数组中的元素逆序存放

用指针将数组中的元素逆序存放。 函数接口定义: void inv(int *x,int n); 其中x和n是用户传入的参数。函数将指针x所指数组中的n个元素逆序存放。 裁判测试程序样例: #include<stdio.h> void inv(int *x,int n); int main ( ) {int i ,a[10]={3,7,9,11,0,6,7,5,4,2}; printf("The original array:\n"); for (i=0;i<10;...

m0_62318806的博客 1万+

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

使用C语言实现字符串逆序操作

使用C语言实现字符串逆序操作

m0_60748587的博客 1万+

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

用指针实现逆序存放数组元素的值

1 题目 函数:invert() 功能:用指针实现逆序存放数组元素的值 描述:使用指针将数组中的元素值逆序放置,并且将结果打印出来 2 思路 1. 程序变量中两个数字进行交换的三种方法 第一种:引入一个中间量 temp=a; a=b; b=temp; 第二种:要注意的是a+b可能会超过int的范围,这个是该方法的缺点 a=a+b; b=a-b; a=a-b; 第三种:这种方法效率最高,推荐使用 a=a^b; b=a^b; a=a^b; 2. 指针的使用 使用指针对数组内容进行操作,数组 a, a

迷失技术de小猪 4968

c语言题目 - 将数组逆序存放

编写一个函数,将整型数组中n个数按反序存放。

qq_44947439的博客 1281

【C】指针之数组逆序函数

编写函数invert,将数组中的n个整数按相反顺序存放,要求用指针变量作为函数形参,并用指针的方法遍历该数组。 在main函数中输入n个整数,存入数组a中;然后调用上述函数处理数组a,最后逐个输出数组a中的元素。 注意:不要改变函数名称,注意大小写敏感。

m0_62866456的博客 2660

C语言 指针与数组

在C语言中,数组的指针是指数组在内存中的起始地址, -----数组元素的地址是指数组元素在内存中的起始地址 一维数组的数组名为一维数组的指针(起始地址) 下面用代码验证一下 x[i],(px+i),(x+i)和px[i]是否具有相同功能(访问数组第i+1个数组元素)。 #include <stdio.h> int main(int argc, char *argv[]){ int a[] = {1, 3, 5, 7, 9}; int *p, i, n; p = a; n = size

仙八哥的博客 3382

C语言字符串逆序输出

手把手带你拿捏C语言的字符串逆序输出!

weixin_53811859的博客 8416

c语言反序存放,c语言:将数组中n个整数按相反顺序存放。

将数组中n个整数按相反顺序存放。解:程序1:#includeint inv(int x[], int n){int temp, i, j, m = (n - 1) / 2;for (i = 0; i <= m; i++){j = n - 1 - i;temp = x[i];x[i] = x[j];x[j] = temp;}return;}int main(){int i, a[10] = {...

weixin_28707739的博客 4814

逆序输出一个数组

问题 如何实现一个数组的逆序输出 代码如下: #include <stdio.h> #define length 10 int main() { int a[length]; int *p,t,i; p=&a[0]; for(;p<(a+length);p++) { scanf("%d",p); } for(i=0;i<length/2;i++) { t=a[i];

qq_51907130的博客 2480

【C语言和Java】P216 例5.写一个函数,使输入的一个字符串按反序存放,在主函数中输入和输出字符串

P216 例5.写一个函数,使输入的一个字符串按反序存放,在主函数中输入和输出字符串。

m0_58734370的博客 5361

c语言指针字符串逆序存放,C语言指针实现字符串的反转

题目要求:编写程序:从键盘任意输入一个字符串,输出该字符串。然后,将该字符串逆序存放后再输出,要求用字符指针完成。代码://解法一#include #include int main(){char a[20] = {0}, b[20] = {0}, *p1, *p2;printf("任意输入一个字符串:");//memset(b, 0X00, sizeof(b));gets(a);p1 = a;p...

weixin_35995661的博客 2784

习题7-5 字符串逆序存放

题目描述 写一个函数将一个字符串按反序存放。在主函数中输入一个字符串,通过调用该函数,得到该字符串按反序存放后的字符串,并输出。 输入 一行字符串。 输出 输入字符串反序存放后的字符串。单独占一行。 样例输入 abcd 样例输出 dcba 交换函数思路 1.分成两种情况: 字符串个数为偶数个: 判断条件与下标的关系为:i<(strlen(a)/2) 字符串个数为奇数个: 判断条件与下标的关系...

pounds008的博客 1819
上一篇: c语言简单的编程题,C语言编程题,比较简单
下一篇: C 语言程序设计全真模拟试卷,计算机二级《C语言程序设计》全真试题
命题LM
博客等级 码龄7年 22粉丝 82原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值