You are on page 1of 5

INSTITUTE OF SPACE TECHNOLOGY

ISLAMABAD

Programming language lab


Electrical Engineering Department
Batch-19 Section-B
Lab No 2

Submitted To:
Sir Adnan Zafar
Submitted By:
M Saad Javed
Registration No:
200401013
Task # 1
Generate 10 random values between 1 and 1000 using built-in function “rand()” and store the
values in an array. Pass the array of 10 random values to a function “MinMaxFunction” that can
find the minimum and maximum value in the array and return the minimum and maximum
values in the array to the main function. The values are then printed in the main function.
(Note: rand()%1000 will generate random values from 0 till 999)

Pseudocode:
1: Begin.
2: Write header files of ctime and cstdlib Write the min max function before initializing ist
(minimum) and stars (maximum) in array form.
3: Use for loop to store only ten random values, by using if statement in for loop to stor accordingly.
4: In main function use srand function to take different values at every run
5: The random value will be modulated by 1000 and add 1 in it, that way the rend value will be
between 1 and 1000.
6: Then call the min max function.
7: Output minimum and maximum values.
8: End.

Code:
//M Saad Javed//
//Reg No. 200401013//
#include <iostream> //for input and output
#include<ctime> //for srand function(gives different values everytime when run)
#include<cstdlib> //for rand function
using namespace std;
int MinMaxFunction(int s[10]) //function to find minimum and maximum value
{
int ist = s[1]; //ist represents minimum value
int stars = 0; //stars represents maximum value
for (int m = 0; m < 9; m++) //for loop to store 10 values in array
{
if (s[m] > stars)
{
stars = s[m];
}
if (s[m] < ist)
{
ist = s[m];
}
}
cout << "Maximum in 10 random values that are chosen : " << stars << endl; //printing
maximum value
cout << "Minimum in 10 random values that are chosen : " << ist << endl; //printing
minimum value
return 0;
}
int main()
{
int s[10];
srand(time(0)); //take different random values each time
for (int m = 0; m < 10; m++)
{
s[m] = (rand() % 1000) + 1; //It generates random value 1 and 1000
cout << " the random value in " << m << " place of the array is : " << s[m] <<
endl; // to print array values
}
MinMaxFunction(s); //function calling
return 0;
}

Output:

Link:
https://repl.it/@SaadJaved/Task-No-1-Lab-2
Task #2
Write a C++ program to store values in a two-dimensional array i.e., 3x4 and then write a
function that displays the elements of the array. You should take input in the main function and
then pass the array to a “PrintArray” function. The function will print the array on the display.

Pseudocode:
1: Begin.

2: Initialize global variables row as 3 and col as 4.

3: Initialize the 3x4 array 'arr' in the main function.

4: Call 'PrintArray' function with 'arr' as an argument.

5: In the 'PrintArray' function, run the first loop for rows one at a time till row 3.

6: Run the second loop for columns from 1 to 3 for each row.

7: Print the array.

8: Print a newline after every 3rd column for each row.

9: End.

Code:
//M Saad Javed//
//Reg No.200401013//
#include <iostream>
using namespace std;

const int row = 3; //Declaring row as global variable and initializing it to 3


const int column = 4; //Declaring column as global variable and initializing it to 4

void PrintArray(int array[row][column]) //Function with array as input


{ for (int s = 0; s < row; s++) //Loop from row 1 to 3
{for (int m = 0; m < column; m++) //Loop from column 1 to 3 for row 1, then row 2 and
then row 3
{cout << array[s][m]; //print the array element
cout << " ";
}
cout << endl; //New line after every 3rd column
}
}
int main()
{
cout << "The array is : " << endl;
int array[3][4] = { {7,2,5,0},{5,3,7,1},{9,0,8,2} }; //Passing the 3x4 array values
in arr
PrintArray(array); //Calling function PrintArray with arr as an argument
return 0;

Output:

Link:
https://repl.it/@SaadJaved/Task-No-2-Lab-2

You might also like