Professional Documents
Culture Documents
QUESTION 1
INPUT
#include <iostream>
using namespace std;
int main(void)
{
int i, num[10];
OUTPUT
QUESTION 2
INPUT
#include <iostream>
#define ARRAY_SIZE 5
using namespace std;
//FUNCTION PROTOTYPE
int findTotal(int arr[], int size);
int main(void)
{
int numbers[ARRAY_SIZE];
int i, total;
cout << "Enter " << ARRAY_SIZE << " numbers: ";
for (i = 0; i <ARRAY_SIZE; i++)
{
cin >> numbers[i];
}
return 0;
}
int findTotal(int arr[], int size)
{
int i, total;
total = 0;
for (i = 0; i < size; i++)
total += arr[i];
return total;
}
OUTPUT
QUESTION 3
INPUT
#include <iostream>
#define ARRAY_SIZE 5
using namespace std;
//FUNCTION PROTOTYPE
int findLargest(int arr[], int size);
int findSmallest(int arr[], int size);
int main(void)
{
int numbers[ARRAY_SIZE];
int i, largest, smallest;
cout << "Enter " << ARRAY_SIZE << " numbers: ";
for (i = 0; i <ARRAY_SIZE; i++)
{
cin >> numbers[i];
}
return 0;
}
int findLargest(int arr[], int size)
{
int i;
int largest = arr[0];
for (i = 0; i < size; i++)
if (largest < arr[i])
largest = arr[i];
return largest;
}
int findSmallest(int arr[], int size)
{
int i;
int smallest = arr[0];
for (i = 0; i < size; i++)
if (smallest > arr[i])
smallest = arr[i];
return smallest;
}
OUTPUT
QUESTION 4
INPUT
#include <iostream>
#define ARRAY_SIZE 5
using namespace std;
//FUNCTION PROTOTYPE
bool compare(int x[], int y[], int size);
int main(void)
{
int a1[ARRAY_SIZE] = { 1,2,3,4,5 };
int a2[ARRAY_SIZE] = { 1,2,3,4,5 };
int a3[ARRAY_SIZE] = { 1,2,3,4,6 };
return 0;
}
int main(void)
{
int i, count;
int randNo;
cout << "How many times you want to roll a dice? ";
cin >> count;
srand(time(NULL));
cout << "The outcomes of " << count << " rolls are: ";
for (i = 0; i < count; i++)
{
randNo = rand() % 6 + 1;
cout << randNo << " ";
}
cout << endl;
return 0;
}
OUTPUT
QUESTION 6
INPUT
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime> //for time function
#define COUNT 6
using namespace std;
int main(void)
{
int i, count;
int randNo;
int frequency[6];
cout << "How many times you want to roll a dice? ";
cin >> count;
srand(time(NULL));
cout << "The outcomes of " << count << " rolls are: ";
for (i = 0; i < count; i++)
{
randNo = rand() % 6 + 1;
cout << randNo << " ";
frequency[randNo - 1]++;
}
cout << endl;
int main(void)
{
double numbers[10];
int n, i, choice;
get_numbers(numbers, n);
if (choice == 1)
cout << "Average is " << average(numbers, n) << endl;
else if(choice==2)
cout << "Geometric Mean is " << geo_mean(numbers, n) << endl;
else if (choice==3)
cout << "Harmonic Mean is " << geo_mean(numbers, n) << endl;
return 0;
}
void get_numbers(double numbers[], int n)
{
int i;
mean = 0.0; //mean must declare 1.0 and 0.0 to get double type answer
for (i = 0; i < n; i++)
mean += 1.0 / numbers[i];
return n/mean;
}
OUTPUT