私はこのコードを 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;
}