C++设计一个在数组中找最大数的函数findmax, 要求该元素的下标通过参数返回,其地址通过函数值返回
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++设计一个在数组中找最大数的函数findmax, 要求该元素的下标通过参数返回,其地址通过函数值返回相关的知识,希望对你有一定的参考价值。
没搞懂它的意思。通过参数返回?地址通过函数值返回?什么意思?
#include "stdafx.h"#include <iostream>//.h
using namespace std;//
int *findthemax(int *pa,int ln,int &spt)
spt=0;
int tmp=pa[spt];
for(int i=0;i<ln;i++)
if(tmp<pa[i])
tmp=pa[i];
spt=i;
return pa+spt;
void main(void)
int n,*pa,subscript;
cout << "Enter the size of the array:";
cin >> n;
if(!(pa=new int[n]))
cout << "Application memory failure...\n";
return;
cout << "Enter " << n << " integers(with ' '):\n";
for(int i=0;i<n;cin >> pa[i++]);
cout << "The address is " << findthemax(pa,n,subscript) << endl;
cout << "The max is " << *(pa+subscript) << endl;
delete [n]pa;
参考技术A #include<iostream>
using namespace std;
int* findthemax(int * a,int n,int &max)
max=0;
int temp=a[max];
for(int i=0;i<n;i++)
if(temp<a[i])
temp=a[i];
max=i;
return &a[max];
int main()
cout<<"input the size of the array:";
int size;
cin>>size;
int a[size];
int themax=0;
for(int i=0;i<size;i++)
cin>>a[i];
void *p=NULL;
p=findthemax(a,size,themax);
cout<<"the max is "<<themax<<endl;
cout<<"the address is "<<p<<endl;
return 0;
参考技术B 函数返回值是地址
函数的参数要引用
就是这个意思,ipad不打太多字了
该元素参数前面加上&就行
int findmax(int []a,int &A);
iPad打字不容易啊
设计一个泛型类orderedCollection
import java.util.Arrays;
/**
* 设计一个泛型类orderedCollection,它存储的Comparable对象的集合(在数组中),
* 以及该集合的当前大小。提供public方法isEmpty,makeEmpty,insert,remove,findMin和
* findMax。finfMin和findMax分别返回该集合中最小的和最大T对象的引用(如果该集合为空,则返回null)
* @author wulei
*
* @param <E>
*/
public class CollectionTest<E> {
private Comparable[] elementData;
public void setElementData(Comparable[] obj){
this.elementData = obj;
}
public Comparable[] getElementData(){
return elementData;
}
public boolean isEmpty(){
return elementData.length == 0;
}
/**
* makeEmpty与List的clear()方法类似,Collection不为null,但是元素个数为0
*/
public void makeEmpty(){
elementData = new Comparable[]{};
}
public void insert(Comparable obj){
int length = elementData.length;
Comparable[] temp = new Comparable[length+1];
System.arraycopy(elementData, 0, temp, 0, length);
temp[length] = obj;
elementData = temp;
}
public void remove(int index){
if(index < 0 || elementData == null){
return ;
}
int length = elementData.length;
if((length-1) < index){
return ;
}
Comparable[] temp = new Comparable[length-1];
System.arraycopy(elementData, 0, temp, 0, index);
System.arraycopy(elementData, index+1, temp, index, length-index-1);
elementData = temp;
}
public void remove(E obj){
if(elementData == null || obj == null){
return;
}
int length = elementData.length;
for (int i = 0; i < length; i++) {
if(elementData[i] != null && elementData[i] == obj){
Comparable[] temp = new Comparable[length-1];
System.arraycopy(elementData, 0, temp, 0, i);
System.arraycopy(elementData, i+1, temp, i, length-i-1);
elementData = temp;
break;
}
}
}
public boolean isPresent(E obj){
if(elementData == null || obj == null){
return false;
}
boolean flag = false;
for (int i = 0; i < elementData.length; i++) {
if(null != elementData[i] && obj == elementData[i]){
flag = true;
break;
}
}
return flag;
}
public Comparable<E> findMin() {
if(elementData == null || elementData.length == 0){
return null;
}
int index = 0;
for (int i = 0; i < elementData.length; i++) {
if(elementData[i].compareTo(elementData[index]) < 0){
index = i;
}
}
return elementData[index];
}
public Comparable<E> findMax(){
if(elementData == null || elementData.length == 0){
return null;
}
int index = 0;
for (int i = 0; i < elementData.length; i++) {
if(elementData[i].compareTo(elementData[index]) > 0){
index = i;
}
}
return elementData[index];
}
public static void main(String[] args){
int i =0;
CollectionTest<String> collectionTest = new CollectionTest<String>();
Comparable[] objects =new Comparable[]{"9"};
collectionTest.setElementData(objects);
collectionTest.insert("1");
collectionTest.insert("2");
collectionTest.insert("3");
collectionTest.insert("4");
System.err.println(Arrays.toString(collectionTest.getElementData()));
collectionTest.remove(0);
System.err.println(Arrays.toString(collectionTest.getElementData()));
collectionTest.remove("2");
System.err.println(Arrays.toString(collectionTest.getElementData()));
boolean flag = collectionTest.isEmpty();
System.err.println("isEmpty: "+flag);
boolean flag1 = collectionTest.isPresent("1");
System.err.println("isPresent: "+flag1);
//collectionTest.makeEmpty();
boolean flag2 = collectionTest.isEmpty();
System.err.println("isEmpty: "+flag2);
System.err.println("min: "+collectionTest.findMin());
System.err.println("max: "+collectionTest.findMax());
}
}
以上是关于C++设计一个在数组中找最大数的函数findmax, 要求该元素的下标通过参数返回,其地址通过函数值返回的主要内容,如果未能解决你的问题,请参考以下文章
使用PDL语言和PAD图描述在数组A(1)~A(10)中找最大数的算法
编写一个函数findmax( ),求数组中最大元素及其下标。调用该函数求整型数组a中的最大元素及其下标。