【发布时间】:2020-03-31 02:17:26
【问题描述】:
问题:我想编写一个函数来打印从第 m 个元素到第 n 个元素的数组,其中 m
#include <stdio.h>
#include <iostream>
using namespace std;
/* It may be ok or not. If not then what program should write? */
void print(int a[], int n)
{
for(int i=0;; ++i){
if(a[i] == a[n]) break;
printf("%d ",a[i]);
}
}
int main()
{
int a[6] = {1,3,5,6,8,2};
/*We can use to sort this array of 1st n elements*/
sort(a,a+n);
/*if we want to use c function to sort this array 'a' for 1st 4 elements then we write*/
sort(a,a+4);
/*But if we want use to print 1st n elements than what should user defined function looks like?*/
print(a,a+3); //for this function to print 1st 3 elements what should write user defined function
return 0;
}
【问题讨论】:
-
原型可能类似于
void print(int a[], int m, int n)或void print (int * begin, int * end)。可能有数千个其他选项,但这两个非常简单。
标签: arrays c function parameters arguments