You are on page 1of 12

PART B

QUESTION 1
INPUT
#include <iostream>
using namespace std;

int main(void)
{
int i, num[10];

cout << "Enter 10 number: ";


for (int i = 0; i < 10; i++)
{
cin >> num[i];
}

cout << "The numbers are: ";


for (int i = 0; i < 10; i++)
{
cout << num[i] << " ";
}
cout << endl;
return 0;
}

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];
}

total= findTotal(numbers, ARRAY_SIZE);


cout << "The Total is " <<total<<endl;

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];
}

largest = findLargest(numbers, ARRAY_SIZE);


cout << "The largest is " << largest << endl;
smallest = findSmallest(numbers, ARRAY_SIZE);
cout << "The smallest is " << smallest << endl;

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 };

if (compare(a1, a2, ARRAY_SIZE))


cout << "arrays a1 and a2 are equal\n";
else
cout << "arrays a1 and a2 are not equal\n";

if (compare(a1, a3, ARRAY_SIZE))


cout << "arrays a1 and a3 are equal\n";
else
cout << "arrays a1 and a3 are not equal\n";

return 0;
}

bool compare(int x[], int y[], int size)


{
int i;
bool isEqual = true; //assume true
i = 0;
while (i < size && isEqual)
{
if (x[i] != y[i])
isEqual = false; //set to false
else
i++;
}
return isEqual;
}
OUTPUT
QUESTION 5
INPUT
#include <iostream>
#include <cstdlib>
#include <ctime> //for time function
using namespace std;

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];

for (i = 0; i < 6; i++)


frequency[i] = 0;

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;

cout << "\nOutcome\tFrequency\n";


cout << "=======\t========\n";
for (i = 0; i < 6; i++)
cout << i + 1 << "\t" << setw(2) << frequency[i] << endl;
return 0;
}
OUTPUT
PART D
QUESTION 1
INPUT
#include <iostream>
#include <cmath>
using namespace std;

void get_numbers(double numbers[], int n);


double average(double numbers[], int n);
double geo_mean(double numbers[], int n);
double har_mean(double number[], int n);

int main(void)
{
double numbers[10];
int n, i, choice;

cout << "How many numbers to process?";


cin >> n;

get_numbers(numbers, n);

cout << "Do you want:\n";


cout << "1. Arithmetic Avergae\n";
cout << "2. Geometric Mean\n";
cout << "3.Harmonic Mean\n";
cout << "Enter your choice (1-3): ";
cin >> choice;

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;

cout << "Enter the numbers to process: ";


for (i = 0; i < n; i++)
cin >> numbers[i];
}
double average(double numbers[], int n)
{
int i;
double average;

average = 0.0; //to make the average calculated as double type


for (i = 0; i < n; i++)
average += numbers[i];
return average / n;
}
double geo_mean(double numbers[], int n)
{
int i;
double mean;
mean = 1.0; //put 1 also can because the numbers is double so the ans will be
double also
for (i = 0; i < n; i++)
mean *= numbers[i];
return pow(mean, 1.0 / n);
}
double har_mean(double numbers[], int n)
{
int i;
double mean;

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

You might also like