Lab No 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Pak-Austria Fachhochschule Institute of Applied

Sciences and Technology, Haripur, Pakistan

DEPARTMENT OF ELECTRICAL ENGINEERING

Data structure and Algorithm Lab Manual

Submitted by:
Name: Muhammad Suleman
Reg# B20F0404EE026

Submitted to:
Name: Rafi Ullah
Designation: LAB Engineer
Date:
October 01, 2021
LAB NO 1
C++ Review
OBJECTIVE:
• To Review the basic concepts of C++.

• To Review Arrays, how to declare, initialize and access 2D and 3D arrays Implement Arrays in C++.

APPARATUS:
The apparatus we used in this lab includes

Operating system (Windows)

DEVC++

Many compilers like IDE are present but we used DEVC++ it is efficient and easy to use.

INTRODUCTION:
This lab is about the basic concepts of C++. That includes datatypes and their categories.
It is about the operators (binary and unary) that they act as assignment and iteration operators.
It also tells the basics of relational, equality, logical operators and that how they used.
It covers the conditional statements includes if else statements and their applicability.
With exception to it also switch statement on expressions is t be reviewed. Loops that include
for, do, do while is also mentioned. And their difference, loop termination, condition and where
to use has been discussed in this lab.
Functions implementation and how to call them has been reviewed.
Arrays implementation that includes how to declare, initialize and access elements of array is
reviewed.
How element is stored in index, and with introduction in loop in arrays , iterations in loops of
array elements.
Then two dimension arrays implementation, declaration, initialization has been reviewed by
example of rows and column.

LAB TASK 1:
You’re given with marks of 10 students in Mathematics, write a program to determine
the grade of each student.

80, 72, 93, 87, 90, 55, 66, 74, 69, 56


Assume:
Grade is A if score is equal and greater than 90 Grade is B+
if score is less than 90 and greater than 81 Grade is B
if score is less than 82 and greater than 71 Grade is C
if score is less than 72 and greater than 66 Grade is D
if score is less than 66 and greater than 59
Grade is F if score is less than 60.

CODE:
#include <iostream>
using namespace std;

int main() {
int score[10]={80, 72, 93, 87, 90, 55, 66, 74, 69, 56},grade;
for (int i=0;i<10;i++)
{
if (score[i]>=90) {

cout<<"Grade of student "<<i<<" IN MATHEMATICS is A"<<endl;


}
else if ((score[i]<90) && (score[i]>81))
{
cout<<"Grade of student "<<i<<" IN MATHEMATICS is B+"<<endl;
}
else if ((score[i]<82) && (score[i]>71))
{
cout<<"Grade of student "<<i<<" IN MATHEMATICS is B"<<endl;
}
else if ((score[i]<72) && (score[i]>=66))
{
cout<<"Grade of student "<<i<<" IN MATHEMATICS is C"<<endl;
}
else if ((score[i]<66) && (score[i]>59))
{
cout<<"Grade of student "<<i<<" IN MATHEMATICS is D"<<endl;
}
else if (score[i]<60)
{
cout<<"Student "<<i<<" FAILED in MATHEMATICS"<<endl;
}
}
return 0;
}

OUTPUT:
LAB TASK 2:
Write a program to ask user to enter 5 floating numbers and find the maximum and minimum of all by
calling min() and max() functions.
CODE:
#include<iostream>
using namespace std;

float min(float a[]){


float mini=a[0];
for(int i=0;i<5;i++)
{
if(a[i]<mini)
{
mini =a[i];
}
}
return mini;
}

float max( float a[]){


float maxi=a[0];
for(int i=0;i<5;i++)
{
if(maxi<=a[i])
{
maxi = a[i];
}
}
return maxi;
}
int main(){
float a[5];
float maximum,minimum;
cout<<"Enter floating numbers:"<<endl;
for (int i=0;i<5;i++)
{
cin>>a[i];
}
maximum=max(a);
cout<<"maximum foating number of all is:"<<endl<<maximum<<endl;
minimum=min(a);
cout<<"Minimum foating number of all is:"<<endl<<minimum<<endl;
}

OUTPUT:

LAB TASK 3:
3. Write a program that shows following output
CODE:
#include <iostream>
using namespace std;

#include <iomanip>
using std::setw;

int main ()

{ char x;
int n[ 10 ];
cout<<"Please enter 10 numbers:"<<endl;
for ( int i = 0; i < 10; i++ )
{
cin>>n[i];
n[ i ] = i + 1;
}
cout << "Element" << setw( 13 ) << "Value" << setw( 13 )<< "Histogram"<<endl;
for ( int j = 0; j < 10; j++ ) {
}
for( int k = 0; k < 10; k++)
{ cout << setw( 7 )<< k << setw( 13 ) << n[ k ]<<setw(13) ;
for(int l = 0; l <= k; l++)
{ cout<<"*";
}
cout<<endl;
}
return 0;
}

OUTPUT:

TASK NO 4:
Write a program that will print multi-subscripted array as shown below using function printArray()
CODE:
#include<iostream>
using namespace std;
void printarr(int a[][3],int b[][3],int c[][3]){
cout<<"Values in array1 by row are"<<endl;
for ( int i = 0; i < 2; i++ ){
for ( int j = 0; j < 3; j++ ) {
cout << a[i][j]<<"\t";}
cout<<"\n";
}

cout<<"Values in array2 by row are"<<endl;


for ( int i = 0; i < 2; i++ ){
for ( int j = 0; j < 3; j++ ){
cout << b[i][j]<<"\t"; }
cout<<"\n";
}

cout<<"Values in array2 by row are"<<endl;


for ( int i = 0; i < 2; i++ ){
for ( int j = 0; j < 3; j++ ){
cout << c[i][j]<<"\t"; }
cout<<"\n";
}
}
int main(){
int a[2][3]={{1,2,3},{4,5,6}};
int b[2][3]={{1,2,3},{4,5,0}};
int c[2][3]={{1,2,0},{4,0,0}};
printarr(a,b,c);
return 0; }

OUTPUT:

ANALYSIS AND RESULTS / CONCUSION:


All the tasks have been performed and results are prepared.
In the first task grades of students has been prepared by using array. Numbers are given in
array just has to guide them according to grade and to print their grades has been done. Loops
has been used in this task. Problems that I faced in this task includes assigning double
conditions in statements and its syntax was not properly setting and it was not giving 5 th and 7th
student. Then that problem has been encountered.
In the 2nd task finding maximum and minimum number by using min() and max() functions
includes loops in both functions and then in main function entering numbers by arrays. Then in
functions by creating maxi and mini float number and then comparing it with value of array
number we entered and then stored that number in particular maxi and mini numbers did the
trick. But problem I faced is calling functions in main it was giving garbage values by max and
min functions. Actually, I was not assigning initial value to maxi and mini numbers. Void
function was not returning value then I used float function and by return results was prepared
by entering 5 floating numbers.
In task 3 printing stars was a difficult because it included two loops one was its row and other
was its magnitude loop. So, when I encountered then stars space setw was not setting properly
it was starting from start not under histogram then that problem I solved by printing numbers,
values in first loop, leaving space for stars and executing stars in next loop.
In task 4 two dimensional arrays is prepared by taking numbers in arrays. Then by printing
those numbers in arrays by using printarr() function and I used 2 loops separately in three
arrays and then printed out according to given task. Problem I faced is using multi arrays in
arguments and it was printing numbers in separate lines then by using “\n” and “\t” in each
loop did the task.

You might also like