#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <time.h>
const int randNumSize = 50, numPerline = 8;
void showNum(int randNum[]);
void countOddNum(int randNum[]);
int main()
{ 
int randNum[randNumSize] = {0};
char menu;  srand(time(NULL)); 
do
{  
 for(int n=0;n < randNumSize;n++) 
 { 
  randNum[n] = rand()%40 + 10;   
 }   
showNum(randNum);   
countOddNum(randNum);   
cout << "Do you want to random again (Y/N)";   
cin >> menu;  
}
while(menu == 'Y');   
return 0;
}
void showNum(int randNum[])
{  
cout << "Show  random Array  " << randNumSize << " numbers :" << endl;   
for(int i=0, showed=0; i < randNumSize; i++)
{
showed++ ;   
cout << setw(5) << randNum[i];   
if(showed == numPerline)
{
cout << endl;   
showed = 0;  
}   
}
}
void countOddNum(int randNum[])
{
cout << "\n\nOdd numbers are  :" << endl; 
for(int i=0, Oddnum=0, sumOdd=0, showed=0; i < randNumSize; i++)
{
if(randNum[i]%2 == 1)
{ 
Oddnum++;   
showed++;   
sumOdd += randNum[i];   
cout << setw(5) << randNum[i];   
if(showed == numPerline)
{     
cout << endl;     
showed = 0;   
}   
} 
} 
cout << "\n\nOdd number = " << Oddnum << " Sum = " << sumOdd << endl;
}
 
ไม่มีความคิดเห็น:
แสดงความคิดเห็น