Uploaded by I'm not Zhanhao

Practical 08 answer PartAB

advertisement
UECS1643/UECS1653/UECS1004/UECS1104
Practical 8 Answer Guidelines
Part A (Understanding Concepts)
1.
(a) int
arr1[10];
(b) char
arr2[50];
(c) double arr3[15];
2. int numbers[6];
numbers
2
[0]
50
[1]
17
[2]
8
[3]
63
[4]
(a) numbers[0] = 2;
numbers[1] = 50;
numbers[2] = 17;
(b) numbers[3] = 8;
numbers[4] = 63;
numbers[5] = 45;
(c) numbers[4] = numbers[4] - 50;
OR
numbers[4] -= 50;
(d) for (int i = 0; i < 6; i++)
numbers[i] *= 10;
(e) for (int i = 0; i < 6; i++)
cout<< numbers[i] <<endl;
3. output:
4
3
2
1
0
Note:
list1
0
[0]
1
[1]
2
[2]
3
[3]
4
[4]
list2
4
[0]
3
[1]
2
[2]
1
[3]
0
[4]
4. void initialise(char letters[], int size)
{
for (int i = 0; i < size; i++)
letters[i] = '*';
}
To test the function:
#include <iostream>
using namespace std;
1
45
[5]
UECS1643/UECS1653/UECS1004/UECS1104
void initialise(char letters[], int size);
int main(void)
{
char ch_arr[10];
initialise(ch_arr, 10);
for(int i = 0; i < 10; i++)
cout << ch_arr[i];
cout << endl;
return 0;
}
void initialise(char letters[], int size)
{
for (int i = 0; i < size; i++)
letters[i] = '*';
}
Part B (Programming Exercises)
1.
#include <iostream>
using namespace std;
#define ARRAY_SIZE 10
void read
(int numbers[], int size);
void display (int numbers[], int size);
int find_total(int numbers[], int size);
int main(void)
{
int numbers[ARRAY_SIZE], total;
cout << "Enter " << ARRAY_SIZE << " numbers: ";
read(numbers, ARRAY_SIZE);
display(numbers, ARRAY_SIZE);
total = find_total(numbers, ARRAY_SIZE);
cout << "Total is " << total << endl;
return 0;
}
void read(int numbers[], int size)
{
for (int i = 0; i < size; i++)
cin >> numbers[i];
}
void display(int numbers[], int size)
{
cout << "The numbers are: ";
for (int i = 0; i < size; i++)
cout << numbers[i] << " ";
cout << endl;
2
UECS1643/UECS1653/UECS1004/UECS1104
}
int find_total(int numbers[], int size)
{
int total = 0;
for (int i = 0; i < size; i++)
total += numbers[i];
return total;
}
2.
int find_largest(int numbers[], int size)
{
int i, largest = numbers[0];
for (i = 1; i < size; i++)
if (largest < numbers[i])
largest = numbers[i];
return largest;
}
int find_smallest(int numbers[], int size)
{
int i, smallest = numbers[0];
for (i = 1; i < size; i++)
if (smallest > numbers[i])
smallest = numbers[i];
return smallest;
}
3.
bool compare(int x[], int y[], int size)
{
int i = 0;
bool is_equal = true; // assume true
while (i < size && is_equal)
{
if (x[i] != y[i])
is_equal = false;
else
i++;
}
return is_equal;
// set to false
}
4.
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>// for time function
using namespace std;
#define DICE_NUM 6
int read_count();
void generate_freq(int count, int freq[], int size);
void print_freq(int freq[], int size);
int main(void)
{
3
UECS1643/UECS1653/UECS1004/UECS1104
int count = read_count();
int freq[DICE_NUM] = {0};
generate_freq(count, freq, DICE_NUM);
print_freq(freq, DICE_NUM);
return 0;
}
int read_count()
{
int count;
cout << "How many times you want to roll a dice? ";
cin >> count;
return count;
}
void generate_freq(int count, int freq[], int size)
{
int rand_no;
srand(time(NULL));
cout << "The outcomes of " << count << " rolls are: ";
for (int i = 0; i < count; i++)
{
rand_no = rand() % size + 1;
cout << rand_no << " ";
freq[rand_no - 1]++;
}
}
void print_freq(int freq[], int size)
{
cout << "\n\nOutcome\tFrequency\n";
cout << "=======\t=========\n";
for (int i = 0; i < size; i++)
cout << i + 1 << "\t" << setw(2) << freq[i] << endl;
}
4
Download