我用 C++ 写了这段代码,我曾经getchar()
在控制台上运行过,但我没有看到使用该函数有任何效果,代码如下:
#include<iostream>
#include<stdio.h>//to pause console screen
using namespace std;
//function prototypes
int getSmallest(int*);
int getOccurrence(int, int*);
int main(){
int a[7], counter=0, smallest;
cout<<"Please enter 7 integers"<<endl;
while(counter!=7){
cin>>a[counter];
counter++;
}
smallest=getSmallest(a);
cout<<"The smallest number is "<<smallest<<"; it apears "<<getOccurrence(smallest,a)<<" times"<<endl;
getchar();//to pause console screen
return 0;
}
int getSmallest(int*x){
int count=0, smallest=*x;
//loop till last item in array
while(count!=7){
if(*x<smallest)
smallest=*x;
count++;
x++;
}
return smallest;
}
int getOccurrence(int smallest, int* address){
int count=0,occ=0;
//loop till last item in array
while(count!=7){
if(*address==smallest)
occ++;
count++;
address++;
}
return occ;
}