0% found this document useful (0 votes)
31 views9 pages

Super Cay

The document outlines a programming assignment involving the creation of various C++ programs that utilize arrays, random number generation, and sorting algorithms. It includes specific tasks such as generating random numbers, sorting user-inputted values, calculating averages, and displaying data in different formats. Additionally, it provides detailed code examples and algorithms for each task, along with comments and conclusions for clarity.

Uploaded by

4848alp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views9 pages

Super Cay

The document outlines a programming assignment involving the creation of various C++ programs that utilize arrays, random number generation, and sorting algorithms. It includes specific tasks such as generating random numbers, sorting user-inputted values, calculating averages, and displaying data in different formats. Additionally, it provides detailed code examples and algorithms for each task, along with comments and conclusions for clarity.

Uploaded by

4848alp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

PROGRAMMING OF COMPUTER EQUIPMENT

Assignment Works-5

Group E-ET-1

Performed by: Alp INAN

KAUNAS, 2024
1 Task no.5
1.1 Task description
1. Based on Example 5.1, write a program that uses the rand() function to create an array of 20
random numbers and display it on a monitor.
2. Write a program that allows you to enter the values of array elements using the keyboard. It will
output the input values to the monitor unordered and in descending order.
3. Write a program to create an array of random numbers, find the maximum and minimum values,
calculate and output the average of the array elements to a monitor.
4. Write a program to generate a 10x5 two-dimensional array. Output the array elements to the
monitor as a column and a table, separated from each other by a tab symbol.
5. Submit a report containing all the points of the assignment, the text of the programs written and
their algorithms, and conclusions

1.2 Program code

1.Task code Example 5.1

#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

int main()
{
int i;
int array[20];
srand(time(NULL));

for (int t = 0; t < 20; t++) {


array[t] = rand();
}

for (int t = 0; t < 20; t++) {


cout << "Array[" << t << "] is equal to " << array[t] << "\n";

}
cin >> i;
return 0;
}

2. Task Code Example 5.2

#include <iostream>
using namespace std;

int main() {
int array[10];

cout << "Enter 10 integers:" << endl;


for (int i = 0; i < 10; i++) {
cout << "Element " << i + 1 << ": ";
cin >> array[i];
}

2
cout << "\nUnordered array:" << endl;
for (int i = 0; i < 10; i++) {
cout << "array[" << i << "] = " << array[i] << endl;
}

for (int i = 0; i < 10 - 1; i++) {


for (int j = 0; j < 10 - i - 1; j++) {
if (array[j] < array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}

cout << "\nArray in descending order:" << endl;


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

return 0;
}

3.Task Code Example 5.3


#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

int main()
{
int i;
int array[10];
srand(time(NULL));

for (int t = 0; t < 10; t++) {


array[t] = rand();
}

for (int t = 0; t < 10; t++) {


cout << "Array[" << t << "] is equal to " << array[t] << "\n";

cout << "Average of the array:" << array[0] + array[1] + array[2] + array[3] + array[4] + array[5] +
array[6] + array[7] + array[8] + array[9] / 10 << '\n';

for (int i = 0; i < 10 - 1; i++) {


for (int j = 0; j < 10 - i - 1; j++) {
if (array[j] < array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}

cout << "Max value of the array:" << array[0] << '\n';

3
cout << "Min value of the array:" << array[9] << '\n';
cin >> i;
return 0;
}

4.Task Code Example 5.4

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
const int rows = 10;
const int cols = 5;
int array[rows][cols];

for (int i = 0; i < rows; i++) {


for (int j = 0; j < cols; j++) {
array[i][j] = (i * cols) + j + 1;
}
}

cout << "Array elements in column format:\n";


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << "array[" << i << "][" << j << "] = " << array[i][j] << endl;
}
}

cout << "\nArray elements in table format:\n";


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << setw(5) << array[i][j];
}
cout << endl;
}

return 0;
}

1.3 Comments and conclusions

Example 5.1 Algorithm

1. Include Libraries:
 #include <iostream>: Used for input and output operations (e.g., cout, cin).
 #include <cstdlib>: Provides the rand() function to generate random numbers.
 #include <time.h>: Provides the time() function to seed the random number
generator.
2. Variable Declaration:
 int i: A placeholder variable used later for cin input, though it doesn't impact the
main logic.
 int array[20]: Declares an array of size 20 to store integers.
3. Random Number Generator Initialization:
 srand(time(NULL)): Seeds the random number generator using the current time.

4
 This ensures that the sequence of random numbers is different each time the
program runs.
4. Fill the Array with Random Numbers:
 A for loop iterates from t = 0 to t < 20 (20 iterations).
 In each iteration, rand() generates a random number, which is assigned to array[t].
5. Display the Array Elements:
 Another for loop iterates from t = 0 to t < 20.
 In each iteration, the value of array[t] is printed to the console along with its index.
6. Pause the Program:
 cin >> i: Pauses the program by waiting for user input before exiting. This allows the
user to view the output.
7. Program Ends:
 return 0: Indicates successful program execution.

Example 5.2 Algorithm

1. Include Libraries:
 #include <iostream>: Used for input and output operations (e.g., cout, cin).
2. Declare an Array:
 int array[10]: A fixed-size array is created to store 10 integers.
3. Input the Array Elements:
 Print a message: "Enter 10 integers:".
 Use a for loop to iterate from i = 0 to i < 10:
 In each iteration:
 Print the prompt: "Element i+1: ".
 Use cin to take the user’s input and store it in array[i].
4. Display the Unordered Array:
 Print a message: "Unordered array:".
 Use a for loop to iterate from i = 0 to i < 10:
 In each iteration:
 Print the index and value of each element in the format: "array[i] =
value".
5. Sort the Array in Descending Order:
 Use the bubble sort algorithm:
 Outer Loop (i): Runs 10 - 1 times, representing the sorting passes.
 Inner Loop (j): Iterates over the array elements that are not yet sorted,
from 0 to 10 - i - 2.
 Comparison: Check if array[j] < array[j + 1].
 If true, swap array[j] and array[j + 1] using a temporary
variable temp.
 Repeat until the array is sorted in descending order.
6. Display the Sorted Array:
 Print a message: "Array in descending order:".
 Use a for loop to iterate from i = 0 to i < 10:
 In each iteration, print the index and value of each element in the
format: "array[i] = value".
7. Program Termination:
 return 0: Ends the program after successfully displaying the sorted array.

5
Example 5.3 Algorithm

1. Include Libraries:
 #include <iostream>: Used for input and output operations (e.g., cout, cin).
 #include <cstdlib>: Provides the rand() function for generating random numbers.
 #include <time.h>: Provides the time() function to seed the random number
generator.
2. Declare Variables:
 int i: Placeholder variable for user input (used later for pausing the program).
 int array[10]: Declares an array of size 10 to store random integers.
3. Initialize Random Number Generator:
 srand(time(NULL)): Seeds the random number generator with the current time,
ensuring different random sequences on each program run.
4. Fill the Array with Random Numbers:
 Use a for loop to iterate from t = 0 to t < 10:
 Generate a random number using rand() and store it in array[t].
5. Display the Array Elements:
 Use another for loop to iterate from t = 0 to t < 10:
 Print each element in the format: “Array[t] is equal to <value>”.
6. Calculate and Display the Average of the Array:
 Print “Average of the array:”.
 Use an arithmetic expression os um all array elements and divide the result by 10 to
calculate the average.
 Note: There is a logical error in the code as written because division happens only os
um last term. The correct approach is os um all elements first and then divide the
total by 10.
7. Sort the Array in Descending Order:
 Use the bubble sort algorithm:
 Outer Loop (i): Runs 10 – 1 times for sorting passes.
 Inner Loop (j): Iterates through the unsorted part of the array and compares
adjacent elements (array[j] and array[j + 1]).
 If array[j] < array[j + 1], swap their values using a temporary variable
(temp).
 After sorting, the largest value is at index 0, and the smallest value is at index 9.
8. Display the Maximum Value:
 Print “Max value of the array:”.
 The maximum value is stored in array[0] (after sorting).
9. Display the Minimum Value:
 Print “Min value of the array:”.
 The minimum value is stored in array[9] (after sorting).
10. Pause the Program:
 Use cin >> i to wait for user input before exiting.
11. End the Program:
 return 0 signals successful program execution.

Example 5.4 Algorithm

1. Include Libraries:
 #include <iostream>: Used for input and output operations (e.g., cout, cin).
 #include <iomanip>: By using this, the same width of space is left in each column
and a proper table format is obtained.

6
2. Variable Definitions:
 rows and cols constants define the number of rows (10) and columns (5) for the
array.
 array[rows][cols] declares a 10x5 two-dimensional array.
3. Filling the Array:
 Two nested for loops are used to fill the array with values:
 Outer Loop (i): Iterates through the rows (from 0 to 9).
 Inner Loop (j): Iterates through the columns (from 0 to 4).
 Each element is calculated using the formula (i * cols) + j + 1, ensuring the
array is filled sequentially from 1 to 50.
4. Printing the Array in Column Format:
 Two nested for loops traverse the array:
 Each element is printed with its indices (array[i][j]) in a separate line.
 This format displays each element in a detailed columnar view.
5. Printing the Array in Table Format:
 The same for loop structure is used to print the elements in a tabular format:
 std::setw(5) is applied to align each element with a fixed width of 5
characters.
 Elements of the same row are printed in a single line, separated by spaces.
 After printing all elements of a row, a newline (endl) is added to move to the
next row.
6. Program Termination:
 return 0 signals the end of the program, indicating successful execution.

Conclusion Example 5.1

7
Conclusion Example 5.2

Conclusion Example 5.3

Conclusion Example 5.4


Next Page

8
9

You might also like