0% found this document useful (0 votes)
4 views21 pages

Computer Programming Lab 6 Hammad

The document contains a series of programming tasks related to arrays, structures, and unions in C++. Each task includes objectives, sample programs, and results demonstrating the functionality of the code, such as inputting integers, searching values, and calculating statistics from matrices. The document serves as a lab report for students in the Department of Mechanical Engineering at UET Peshawar.

Uploaded by

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

Computer Programming Lab 6 Hammad

The document contains a series of programming tasks related to arrays, structures, and unions in C++. Each task includes objectives, sample programs, and results demonstrating the functionality of the code, such as inputting integers, searching values, and calculating statistics from matrices. The document serves as a lab report for students in the Department of Mechanical Engineering at UET Peshawar.

Uploaded by

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

Computer Programming.

Uet Peshawar main campus


Department of Mechanical Engineering

Lab Report No# 06 and 07.

Submitted by: Muhammad Nazir


Registration No:. 19pwmec4753
Submitted to:. Dr. Fakhre Alam
Arrays
Task 01
Write a program that inputs any five integers from the user and stores them in an
array. It then displays all values in the array without using loops.

Objective
To help in drawing the array in programming using the array structure.

Program
#include<iostream>
using namespace std;
void main()
{
int arr[5];
cout<<"Enter five integers: "<>arr[0];
cin>>arr[1];
cin>>arr[2];
cin>>arr[3];cin>>arr[4];
cout<<"The values in array are: \n";
cout<<arr[0]<<<arr[1]<<<arr[2]<<<arr[3]<<<arr[4]<<endl;
system(“system>0)
}
Picture

Fig 6.01

Result
The program shows the array of integers given as input and successfully display
them by using array structure.

Task 02
write a program that inputs any five integers from the user and stores them in an
array it then displays all values in the array using loops

Objective
This program uses the loops with one dimensional array to display the integers
stored in it.

Program
#include <iostream>
using namespace std;
void main()
{
int arr[5], i;
for (i = 0; i < 5; i++)
{
cout << "Enter an integer: ";
cin >> arr[i];
}
cout << "The values in array are: \n";
for (i = 0; i < 5; i++)
{
cout << arr[i] << endl;
}
system(" pause > 0 ");
}

Picture

Result
The program shows the integers as input by using array structure and the loops as
mentioned above.
Task 03
write a program that inputs the age of different persons from user it then counts
the number of persons in age group of 50 and 60

Objective
This program allows the user to count the persons of required ages from the
group of the people.

Program
#include <iostream>
using namespace std;
void main()
{
int age[150], i, n, count = 0;
cout << "Enter the number of persons required: ";
cin >> n;
cout << "Enter ages of " << n << " persons. " << endl;
for (i = 0; i < n; i++)
{
cin >> age[i];
if (age[i] >= 50 && age[i] <= 60)
count = count + 1;
}
cout << count << " persons are between 50 and 60.";
system(" pause > 0 ");
}
Picture

Fig 6.03

Result
The program successfully tells how many men between the age of 50 to 60 are in
the given list.

Sequential Search

Task 04
Write a program that initializes an array it inputs a value from the user and
searches the number in the array

Objective
This program helps in the search of anything that is in array and needed to be
find.
Program
#include <iostream>
using namespace std;
void main()
{
int arr[10] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
int i, n, loc = -1;
cout << "Enter value to find: ";
cin >> n;
for (i = 0; i < 10; i++)
if (arr[i] == n)
loc = i;
if (loc == -1)
cout << "Value not found in the array. ";
else
cout << "Value found at index " << loc;
system(" pause > 0 ");
}

Picture
Result
The program shows the value is available in array or not at a present index by
giving input and is successfully executed.

Binary search

Task 05
Write a program that initializes an array of ten integers it inputs an integer from
the user and searches the value in the array using binary search.

Objective
It enable user to execute array with binary search to search the entry at the index.

Program
#include <iostream>
using namespace std;
void main()
{
int arr[10] = { 10,20,30,40,50,60,70,80,90, 100 };
int n, i, mid, start, end, loc;
loc = -1;
start = 0;
end = 9,
cout << "Enter any number to find: ";
cin >> n;
while (start <= end)
{
mid = (start + end) / 2;
if (arr[mid] == n)
{
loc = mid;
break;
}
else if (n < arr[mid])
end = mid - 1;
else
start = mid + 1;
}
if (loc == -1)
cout << n << " not found!" << endl;
else
cout << n << " found at index " << loc << endl;
system(" pause > 0 ");
}
Picture

Fig 6.05

Result
The program executed successfully and displays required result.

Task 06
Write a program that inputs integer values in a 4*4 matrix and displays the sum of diagonal elements of
the matrix

Objective
Displays the sum of the diagonal of the a 4 by 4 matrix.

Program
#include <iostream>

using namespace std;

void main()

int x, y;

int A[4][4], sum = 0;

cout << "Enter the elements of the matrix: " << endl;

for (y = 0; y < 4; y++)

for (x = 0; x < 4; x++)

cout << "Element " << x + 1 << ", " << y + 1 << " : ";

cin >> A[x][y];


}

//Sum of either of the diagonal elements.

for (x = 0; x < 4; x++)

for (y = 0; y < 4; y++)

if (x == y)

sum += A[x][y];

cout << "Sum of the diagonal elements is: " << sum;

system(" pause > 0 ");

Picture

Result
Task 07
Write a program that declares a three dimensional array to store the
temperatures of a month. The temperature is entered for morning, noon and
evening of each day. The first dimension should be used to for three timings of a
day, second dimension should be used for seven days of a week and third
dimension should be used for four week of a month. The program should input
the temperatures and then display the maximum, minimum and average
temperature of the whole month

Objective
To easily display the temperature of the city by average,maximum and minmum.

Program
#include <iostream>

using namespace std;

void main()

int i, j, k, max, min, tot = 0;

float avg;

int temp[3][7][4];

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

for (j = 0; j < 7; j++)

for (k = 0; k < 4; k++)

cout << "Enter temperature: ";

cin >> temp[i][j][k];

max = min = temp[0][0][0];

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

for (j = 0; j < 7; j++)

for (k = 0; k < 4; k++)

tot = tot + temp[i][j][k];

if (temp[i][j][k] > max)

max = temp[i][j][k];

if (temp[i][j][k] < min)

min = temp[i][j][k];

avg = tot / 84;


cout << "Maximum temperature of month: " << max << endl;

cout << "Minimum temperature of month: " << min << endl;

cout << "Average temperature of month: " << avg << endl;

system(" pause > 0 ");

Picture

Result
Task 08
Write a program that declares a structures to store Roll number, Marks,Average
and grade of a student the program should define a structure variable,inputs the
values and then displays these values

Objective
Displays the info of student according to the input given.
Program
#include <iostream>

using namespace std;

struct Student

int rno;

int marks;

float avg;

char grade;

};

void main()

Student s;

cout << "Enter roll no : ";

cin >> s.rno;

cout << "Enter marks: ";

cin >> s.marks;

cout << "Enter average: ";

cin >> s.avg;

cout << "Enter grade: '';

cin >> s.grade;

cout << "You entered the following details: \n";

cout << "Roll No: ''<<s.rno<<endl;

cout << "Average : " << s.avg << endl;

cout << "Grade: " << s.grade << endl;

system(" pause > 0 ");

Result
No error at its run.
Task 09
Write a program that declares a structure to store employee id and salary of an
employee it defines and initializes a structure variable and display it

Objective
Displays the required info according to input.

Program
#include <iostream>

using namespace std;

struct emp

int eid;

long int sal;

};

void main()

emp e = { 20, 108500 };

cout << "Employee ID: " << e.eid << endl;

cout << "Salary: " << e.sal << endl;

system(" pause > 0 ");

}
Picture

Fig 6.08

Result
Executed without error.

Task 09
Write a program that declares a structure to store marks and grade of a student it
defines two structures variables it inputs the values in one variable and assign
that variable in the second variable it finally displays the values of both

Objective
Program
#include <iostream>

using namespace std;

struct Phone

int ncode;

int acode;

long number;

};
void main()

Phone p1, p2 = { 92, 91, 9220083 };

cout << "Enter national code: ";

cin >> p1.ncode;

cout << "Enter area code: ";

cin >> p1.acode;

cout << "Enter phone number: ";

cin >> p1.number;

cout << "Phone Number 1: +";

cout << p1.ncode << "-" << p1.acode << "-" << p1.number << endl;

cout << "Phone Number 2: +";

cout << p2.ncode << "-" << p2.acode << "-" << p2.number << endl;

system(" pause > 0 ");

Picture

Fig 8.09
Result
The program run and displayed the result.

Nested Structure

Task 10
Write a program that uses two structures result and record the result structure
store marks and grade Record structure…………….

Objective
It help to use nested loop with array.

Program
#include <iostream>

using namespace std;

struct Marks

int m;

char g;

};

void main()

Marks a, b;

cout << "Enter marks: ";

cin >> a.m;

cout << "Enter grade: ";

cin >> a.g;

b = a;
cout << "The first record is as follows:\n";

cout << "Marks: " << a.m << endl;

cout << "Grade: " << a.g << endl;

cout << "The second record is as follows:\n";

cout << "Marks: " << b.m << endl;

cout << "Grade : " << b.g << endl;

system(" pause > 0 ");

Picture

Fig 6.10

Result
The program run without error and displayed the desired result as above.

Task 11
Write a program that uses a union to store employee id , name , age and salary
each one at a time the program inputs these parameters and displays the values
Objective
Helps the user to maintain the record of employee with details using array and
nested structure.

Program
#include<iostream>

using namespace std;

union Employee

int Id;

char Name[25];

int Age;

long Salary;

};

void main()

Employee E;

cout << "\nEnter Employee Id : ";

cin >> E.Id;

cout << "Employee Id : " << E.Id;

cout << "\n\nEnter Employee Name : ";

cin >> E.Name;

cout << "Employee Name : " << E.Name;

cout << "\n\nEnter Employee Age : ";

cin >> E.Age;

cout << "Employee Age : " << E.Age;

cout << "\n\nEnter Employee Salary : ";

cin >> E.Salary;

cout << "Employee Salary : " << E.Salary;

system(" pause > 0 ");

}
Picture

Fig 6.11

Result
Successfully executed the program.

You might also like